File tree Expand file tree Collapse file tree 3 files changed +54
-1
lines changed Expand file tree Collapse file tree 3 files changed +54
-1
lines changed Original file line number Diff line number Diff line change
1
+ # -*- coding:UTF-8 -*-
2
+
3
+ # Lake Counting
4
+
5
+ # data input
6
+ m = 12
7
+ n = 10
8
+ input_arr = [
9
+ 'W........WW.' ,
10
+ '.WWW.....WWW' ,
11
+ '....WW...WW.' ,
12
+ '.........WW.' ,
13
+ '.........W..' ,
14
+ '..W......W..' ,
15
+ '.W.W.....WW.' ,
16
+ 'W.W.W.....W.' ,
17
+ '.W.W......W.' ,
18
+ '..W.......W.'
19
+ ]
20
+ arr = [ [] for i in range (len (input_arr ))]
21
+ for i in range (len (input_arr )):
22
+ for j in range (len (input_arr [i ])):
23
+ arr [i ].append (str (input_arr [i ][j ]))
24
+
25
+ def DFS (i , j ):
26
+ arr [i ][j ] = '.'
27
+ for dx in range (- 1 , 2 ):
28
+ for dy in range (- 1 , 2 ):
29
+ newx = i + dx
30
+ newy = j + dy
31
+ if newx >= 0 and newy >= 0 and newx < n and newy < m and arr [newx ][newy ]== 'W' :
32
+ DFS (newx , newy )
33
+ return
34
+ pass
35
+
36
+ def main ():
37
+ count = 0
38
+ for x in range (0 ,n - 1 ):
39
+ for y in range (0 , m - 1 ):
40
+ if arr [x ][y ] == 'W' :
41
+ DFS (x , y )
42
+ count += 1
43
+ print count
44
+ pass
45
+
46
+ if __name__ == '__main__' :
47
+ main ()
Original file line number Diff line number Diff line change 1
1
# Algorithm_Python_Demo
2
- fundamental algorithm demo by Python
2
+ 《挑战程序设计竞赛(第二版)》
3
+ No.1 初级篇
4
+ 穷竭搜索
5
+ DFS:从一个状态开始,不断移动状态,直到无法移动,然后退回到上一状态,继续转移其他状态,不断重复,直到找到最终解。递归函数
6
+ BFS
7
+ No.2 中级篇
8
+ No.3 高级篇
You can’t perform that action at this time.
0 commit comments