AcWing:1113. 红与黑
深度优先遍历 / 广度优先遍历
AC Code
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59import java.util.*; import static java.lang.System.out; public class Main{ static int ans = 0; public static void main(String[] args) { Scanner in = new Scanner(System.in); while(true) { int r = in.nextInt(); int c = in.nextInt(); if(r == 0 && c == 0) break; char[][] cs = new char[c][r]; for(int i = 0; i < c; i++) { String tmp = in.next(); //out.println(tmp); cs[i] = tmp.toCharArray(); } for(int i = 0; i < c; i++) { for(int j = 0; j < r; j++) { if(cs[i][j] == '@') { //起点 dfs(cs, i, j); break; } } } out.println(ans); ans = 0; } } public static void dfs(char[][] cs, int i, int j){ ans++; cs[i][j] = '1'; // 上下左右 if(i + 1 < cs.length && cs[i + 1][j] == '.') dfs(cs, i + 1, j); if(i - 1 >= 0 && cs[i - 1][j] == '.') dfs(cs, i -1, j); if(j + 1 < cs[0].length && cs[i][j + 1] == '.') dfs(cs, i, j + 1); if(j - 1 >= 0 && cs[i][j - 1] == '.') dfs(cs, i, j - 1); } }
最后
以上就是背后树叶最近收集整理的关于1113. 红与黑 ( dfs / bfs )的全部内容,更多相关1113.内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复