uva352 The Seasonal War-python
2016-04-05 14:09
453 查看
计算连通块,dfs
MAXN = 26 dirs = [[1, 0], [0, 1], [-1, 0], [0, -1], [1, 1], [1, -1], [-1, 1], [-1, -1]] cnt = 0 def dfs(x, y, m, n): flag[x][y] = 1 for d in dirs: nx = x + d[0] ny = y + d[1] if 0 <= nx < m and 0 <= ny < n and pic[nx][ny] == '1' and flag[nx][ny] == 0: dfs(nx, ny, m, n) while True: nn = raw_input() if not nn.isdigit(): break n = int(nn) flag = [[0 for col in range(MAXN)] for row in range(MAXN)] pic = [] ans = 0 cnt += 1 for i in range(n): pic.append(raw_input()) for i in range(n): for j in range(n): if pic[i][j] == '1' and flag[i][j] == 0: dfs(i, j, n, n) ans += 1 print("Image number "+str(cnt)+" contains "+str(ans)+" war eagles.")
相关文章推荐
- python学习过程-集合和for循环
- 利用python下载网页到本地(python3)
- [LeetCode]题解(python):133-Clone Graph
- K近邻分类算法实现 in Python
- ArcMap中用python的split方法提取字段的值
- Python:random模块
- python小项目1
- python内建函数sorted方法概述
- python内建函数sorted方法概述
- Ilya and Escalator-python
- Python:正则表达式模块
- python进阶
- python路径相关的函数os.listdir(dirname):列出dirname下的目录和文件
- python os.path模块常用方法详解
- python实现并行爬虫
- python多线程threading
- python(7)– 类的反射
- 源码安装python后,运行某些模块提示缺少libpython2.7.so
- python接口测试(二)
- numpy.linalg.eig() 计算矩阵特征向量