关注
一点一点分析的话还是能做出来的 import java.util.ArrayList; import java.util.Scanner; public class Main{ public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) { String operators = scanner.nextLine(); int rowsAndColumns = scanner.nextInt(); int row = rowsAndColumns / 10; int column = rowsAndColumns % 10; String[][] board = new String[row][column]; ArrayList<path> snake = new ArrayList<>(); for (int i = 0; i < board.length; i++) { String temp = scanner.next(); for (int j = 0; j < board[0].length; j++) { if("H".equals(temp.substring(j))){ snake.add(new Path(i,j,"L")); } board[i][j] = temp.substring(j,j+1); } } for (int i = 0; i < operators.length(); i++) { char operator = operators.charAt(i); if(operator == 'U'){ snake.get(0).setDirection("U"); }else if(operator == 'D'){ snake.get(0).setDirection("D"); }else if(operator == 'L'){ snake.get(0).setDirection("L"); }else if(operator == 'R'){ snake.get(0).setDirection("R"); }else { //G //移动 move(board,snake); } } } } public static void move(String[][] board,ArrayList<path> snake){ Path head = snake.get(0); String direction = head.getDirection(); int row = 0; int column = 0; if(direction.equals("U")){ row = head.getRow() - 1; column = head.getColumn(); }else if (direction.equals("L")){ row = head.getRow(); column = head.getColumn()-1; }else if (direction.equals("R")){ row = head.getRow(); column = head.getColumn()+1; }else if (direction.equals("D")){ row = head.getRow()+1; column = head.getColumn(); } //越界,游戏结束 if(row > board.length-1 || column > board[0].length-1){ System.out.println(snake.size()); return; } //碰到自己了 for (int i = 1; i < snake.size()-1; i++) { if(row == snake.get(i).getRow() && column == snake.get(i).getColumn()){ System.out.println(snake.size()); return; } } //碰到食物了 if(board[row][column].equals("F")){ snake.add(0,new Path(row,column,direction)); return; } //正常运动 snake.add(0,new Path(row,column,direction)); snake.remove(snake.size()-1); } } class Path{ int row; int column; String direction; public Path(int row, int column, String direction) { this.row = row; this.column = column; this.direction = direction; } public int getRow() { return row; } public void setRow(int row) { this.row = row; } public int getColumn() { return column; } public void setColumn(int column) { this.column = column; } public String getDirection() { return direction; } public void setDirection(String direction) { this.direction = direction; } }</path></path>
点赞
相关推荐
孩子我想要offer:发笔试后还没笔试把我挂了,然后邮箱一直让我测评没测,后面不知道干嘛又给我捞起来下轮笔试,做完测评笔试又挂了😅
点赞 评论 收藏
分享
点赞 评论 收藏
分享
牛客热帖
更多
正在热议
更多
# 工作中出现了XX情况正常吗 #
2481次浏览 35人参与
# 公司福利里最没用的一项是啥 #
1439次浏览 39人参与
# 回顾今年你干过的最“勇”的一件事 #
1862次浏览 31人参与
# 一人推荐一个值得做的项目 #
1255次浏览 21人参与
# 工作前VS工作后,你的心态变化 #
1509次浏览 18人参与
# 高薪高压 vs 低薪wlb,你怎么选? #
1954次浏览 27人参与
# 毕业季,给职场新人一些建议 #
160809次浏览 2353人参与
# 如何用一句话描述你的职业 #
27522次浏览 176人参与
# 你被哪些公司挂了? #
106133次浏览 655人参与
# 说说你知道的学历厂 #
366695次浏览 1312人参与
# 机械校招之路总结 #
106418次浏览 2035人参与
# 找工作,行业重要还是岗位重要? #
83053次浏览 1665人参与
# 如果没找到工作,考公是你的退路吗 #
58057次浏览 434人参与
# 找工作如何保持松弛感? #
124325次浏览 1448人参与
# 牛友们的论文几号送审 #
61193次浏览 831人参与
# 如何准备秋招 #
67743次浏览 841人参与
# 你在职场上见过哪些“水货”同事 #
21461次浏览 150人参与
# 华勤技术工作体验 #
12158次浏览 23人参与
# 刚入职就____,这样正常吗? #
117736次浏览 657人参与
# 你怎么评价今年的春招? #
146725次浏览 1402人参与
