关注
一点一点分析的话还是能做出来的 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>
点赞
相关推荐
点赞 评论 收藏
分享
牛客热帖
更多
正在热议
更多
# 面试问题记录 #
23603次浏览 389人参与
# 面试经验谈 #
15045次浏览 231人参与
# 京东TGT #
30904次浏览 152人参与
# 职场新人生存指南 #
335032次浏览 7187人参与
# 面试吐槽bot #
2987次浏览 36人参与
# 异地恋该为对方跳槽吗 #
24840次浏览 121人参与
# 硬件人更看重稳定还是高薪 #
39979次浏览 205人参与
# 对妈妈没说出口的话 #
8122次浏览 223人参与
# 假如我穿越到了妈妈的18岁 #
434次浏览 21人参与
# 硬件人秋招的第一个offer #
66216次浏览 1082人参与
# 机械求职避坑tips #
41665次浏览 355人参与
# 妈妈治愈了你哪些脆皮时刻 #
3342次浏览 84人参与
# 机械人,你的第一份感谢信是谁给的 #
22565次浏览 295人参与
# 视觉/交互/设计招聘信息汇总 #
10047次浏览 595人参与
# 租房找室友 #
28643次浏览 148人参与
# 不考虑转正,实习多久合适 #
24824次浏览 119人参与
# 新凯来求职进展汇总 #
34036次浏览 90人参与
# 滴滴工作体验 #
23938次浏览 123人参与
# 上班苦还是上学苦呢? #
214092次浏览 1288人参与
# 请用你的专业向妈妈表白 #
2080次浏览 34人参与
# 硬件人你反向读研了吗 #
40439次浏览 608人参与