要双休的烤冷面很胆小 level
获赞
20
粉丝
4
关注
9
看过 TA
247
西安电子科技大学
2023
测试开发
IP属地:浙江
暂未填写个人简介
私信
关注
天地无霜:import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Third { static int M, N, minDist = Integer.MAX_VALUE; static int[][] parArea; static int direc1[] = {-1, 1, 0, 0}, direc2[] = {0, 0, 1, -1}; static List<Integer[]> res = null; public static void main(String[] args) { Scanner cin = new Scanner(System.in); M = cin.nextInt(); N = cin.nextInt(); int i1 = cin.nextInt(); int j1 = cin.nextInt(); parArea = new int[M + 1][N + 1]; boolean[][] visited = new boolean[M + 1][N + 1]; for (int i = 1; i < M + 1; i++) { for (int j = 1; j < N + 1; j++) { parArea[i][j] = cin.nextInt(); } } List<Integer[]> path = new ArrayList<>(); dfs(i1, j1, path, 1, visited); if (res == null) { System.out.print(-1 + " " + -1); return; } for (int i = 0; i < res.size(); i++) { System.out.print(res.get(i)[0] + " " + res.get(i)[1]); if (i < res.size() - 1) System.out.print(" "); } } public static void dfs(int curI, int curJ, List<Integer[]> path, int dist, boolean[][] visited) { if (parArea[curI][curJ] == 3 || parArea[curI][curJ] == 2) return; if (parArea[curI][curJ] == 1) { path.add(new Integer[]{curI, curJ}); if (dist < minDist) { res = new ArrayList<>(path); minDist = dist; }else if (dist == minDist &;&; path.get(path.size() - 1)[0] < res.get(res.size() - 1)[0]) { res = new ArrayList<>(path); } path.remove(path.size() - 1); return; } visited[curI][curJ] = true; path.add(new Integer[]{curI, curJ}); for (int i = 0; i < 4; i++) { int nextI = curI + direc1[i], nextJ = curJ +direc2[i]; if (nextI >= 1 &;&; nextI <= M &;&; nextJ >= 1 &;&; nextJ <= N &;&; !visited[nextI][nextJ]) { if ((i == 2 || i == 3) dfs(nextI, nextJ, path, dist + 1, visited); } } path.remove(path.size() - 1); visited[curI][curJ] = false; } }
投递华为等公司10个岗位 互联网求职圈
0 点赞 评论 收藏
分享
关注他的用户也关注了:
牛客网
牛客企业服务