DFS plus BFS之“Children of the Candy Corn”
Posted 薛定谔的Submit
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了DFS plus BFS之“Children of the Candy Corn”相关的知识,希望对你有一定的参考价值。
题目大意:
给两个数 m n ;
在输入一个 n * m 的地图。
问从 S 走到 E 优先向左和优先向右还有最短路径分别有多少步。
样例:(# 代表墙 , . 代表路)
2
8 8
########
# . . . . . .#
# .#### .#
# .#### .#
# .#### .#
# .#### .#
# . . .# . .#
#S#E####
9 5
#########
# .# .# .# .#
S . . . . . . .E
# .# .# .# .#
#########
————————
37 5 5
17 17 9
解题思路:
向左向右用DFS。
要注意朝向,例如当前朝向向北,就要从西向南向东搜索。
最短路径可以用BFS优先队列求。
一定要注意输入的地图 长宽 是相反的。
AC代码:
1 import java.util.*; 2 3 public class Main{ 4 5 static char map[][] = new char[50][50]; 6 static int mark[][] = new int[50][50]; 7 static int cnt[][] = new int[50][50]; 8 static int m; 9 static int n; 10 static int op_x; 11 static int op_y; 12 static int ed_x; 13 static int ed_y; 14 static int temp; 15 static int bfs_ans; 16 17 static int L[][] = {{0,1},{1,0},{0,-1},{-1,0}}; 18 static int R[][] = {{0,-1},{1,0},{0,1},{-1,0}}; 19 20 static int dfs_L(int x,int y,int step){ 21 if(x == ed_x && y == ed_y) return step + 1; 22 if(x < 1 || x > n || y < 1 || y > m || map[x][y] == ‘#‘) return 0; 23 temp = (temp + 3) % 4; 24 int out = 0; 25 int flag = 0; 26 while(flag == 0){ 27 out = dfs_L(x + L[temp][0],y + L[temp][1],step + 1); 28 if(out > 0) break; 29 temp = (temp + 1) % 4; 30 } 31 return out; 32 } 33 34 static int dfs_R(int x,int y,int step){ 35 if(x == ed_x && y == ed_y) return step + 1; 36 if(x < 1 || x > n || y < 1 || y > m || map[x][y] == ‘#‘) return 0; 37 temp = (temp + 3) % 4; 38 int out = 0; 39 int flag = 0; 40 while(flag == 0){ 41 out = dfs_R(x + R[temp][0],y + R[temp][1],step + 1); 42 if(out > 0) break; 43 temp = (temp + 1) % 4; 44 } 45 return out; 46 } 47 48 static void bfs(int x,int y){ 49 Queue<Integer> que = new LinkedList<Integer>(); 50 que.offer(x); que.offer(y); 51 int t1; int t2; 52 while(que.size() != 0){ 53 t1 = que.peek(); que.poll(); 54 t2 = que.peek(); que.poll(); 55 if(mark[t1][t2] == -1) {continue;} 56 if(t1 == ed_x && t2 == ed_y){ 57 bfs_ans = cnt[t1][t2]; 58 return ; 59 } 60 mark[t1][t2] = -1; 61 if(t1 >= 1 && t1 <= n && t2 >= 1 && t2 <= m){ 62 if(t1 >= 2 && map[t1 - 1][t2] != ‘#‘){ 63 que.offer(t1 - 1); 64 que.offer(t2); 65 cnt[t1 - 1][t2] = cnt[t1][t2] + 1; 66 } 67 if(t1 <= n - 1 && map[t1 + 1][t2] != ‘#‘){ 68 que.offer(t1 + 1); 69 que.offer(t2); 70 cnt[t1 + 1][t2] = cnt[t1][t2] + 1; 71 } 72 if(t2 >= 2 && map[t1][t2 - 1] != ‘#‘){ 73 que.offer(t1); 74 que.offer(t2 - 1); 75 cnt[t1][t2 - 1] = cnt[t1][t2] + 1; 76 } 77 if(t2 <= m - 1 && map[t1][t2 + 1] != ‘#‘){ 78 que.offer(t1); 79 que.offer(t2 + 1); 80 cnt[t1][t2 + 1] = cnt[t1][t2] + 1; 81 } 82 } 83 } 84 return ; 85 } 86 87 public static void main(String[] args){ 88 Scanner sc = new Scanner(System.in); 89 int t = sc.nextInt(); 90 while(t > 0){ 91 m = sc.nextInt(); 92 n = sc.nextInt(); 93 String enter = sc.nextLine(); 94 for(int i = 1;i <= n;i ++){ 95 String in = sc.nextLine(); 96 for(int j = 1;j <= m;j ++){ 97 map[i][j] = in.charAt(j - 1); 98 if(map[i][j] == ‘S‘){op_x = i;op_y = j;} 99 if(map[i][j] == ‘E‘){ed_x = i;ed_y = j;} 100 } 101 } 102 for(int i = 1;i <= m;i ++){ 103 for(int j = 1;j <= n;j ++){ 104 mark[j][i] = 0; 105 cnt[j][i] = 0; 106 } 107 } 108 System.out.print(dfs_L(op_x,op_y,0) + " "); 109 System.out.print(dfs_R(op_x,op_y,0) + " "); 110 bfs(op_x,op_y); 111 System.out.println(bfs_ans + 1); 112 113 t --; 114 } 115 } 116 }
以上是关于DFS plus BFS之“Children of the Candy Corn”的主要内容,如果未能解决你的问题,请参考以下文章