关注
一点一点分析的话还是能做出来的 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>
点赞
相关推荐
点赞 评论 收藏
转发
投递字节跳动等公司10个岗位 > 字节跳动工作体验
点赞 评论 收藏
转发
昨天 09:15
哈尔滨工业大学 电子信息类 点赞 评论 收藏
转发
牛客热帖
正在热议
# 牛友的五一计划 #
22027次浏览 419人参与
# 晒一晒我的offer #
2843641次浏览 50067人参与
# 牛客帮帮团来啦!有问必答 #
407588次浏览 7897人参与
# 市场营销面经 #
1371次浏览 71人参与
# 非技术岗薪资爆料 #
9687次浏览 201人参与
# 如何看待offer收割机的行为 #
195716次浏览 3001人参与
# 正在春招的你,也参与了去年秋招吗? #
123881次浏览 1627人参与
# 产品实习,你更倾向大公司or小公司 #
31433次浏览 492人参与
# 简历中的项目经历要怎么写 #
382499次浏览 6412人参与
# 在国企工作的人,躺平了吗? #
73484次浏览 892人参与
# 互联网公司评价 #
63009次浏览 891人参与
# 第一次面试 #
18837次浏览 293人参与
# 国企和大厂硬件兄弟怎么选? #
28148次浏览 537人参与
# 2022届毕业生现状 #
286026次浏览 4103人参与
# 硬件人的春招flag #
14624次浏览 199人参与
# 浅聊一下我实习的辛苦费 #
71203次浏览 645人参与
# 你更愿意参加线上面试还是线下面试? #
7212次浏览 101人参与
# 女生做医疗销售有前景吗 #
3944次浏览 50人参与
# 提前批真的不会影响正式批吗 #
18006次浏览 224人参与
# 华为求职进展汇总 #
444310次浏览 4452人参与