ZOJ-3110
来源:互联网 发布:oracle数据库入门 编辑:程序博客网 时间:2024/06/06 19:05
说白了就是一道连通块搜索,前天好像做过一个二维的,这题是三维的,没什么难度,我用的BFS,看大牛题解说是用DFS,没想到怎么做。最后那个输出格式有点坑,本来结果早就算出来了,还格式化了半天。。话说我怎么越看我的python代码越丑,怎么看都不习惯,还是C的代码比较清爽,可惜这题用到队列,懒的用C搞了
import sysdef get_adjs(src, i, j, l, m, p, k, code): r = [] if i + 1 < m and src[i + 1][j][l] == code: r.append([i + 1, j, l]) if i - 1 >= 0 and src[i - 1][j][l] == code: r.append([i - 1, j, l]) if j + 1 < p and src[i][j + 1][l] == code: r.append([i, j + 1, l]) if j - 1 >= 0 and src[i][j - 1][l] == code: r.append([i, j - 1, l]) if l + 1 < k and src[i][j ][l + 1] == code: r.append([i, j , l + 1]) if l - 1 >= 0 and src[i][j ][l - 1] == code: r.append([i, j , l - 1]) return rdef bfs(src, i, j, l, m, p, k): code = src[i][j][l] src[i][j][l] = '*' queue = [[i, j, l]] total = 0 while(len(queue) != 0): curr = queue[0] del queue[0] total += 1 for adj in get_adjs(src, curr[0], curr[1], curr[2], m, p, k, code): src[adj[0]][adj[1]][adj[2]] = '*' queue.append([adj[0], adj[1], adj[2]]) return total stdin = sys.stdincount = 0while(True): line = stdin.readline() if not line: break count += 1 pkm = line.strip().split() p = int(pkm[0]) k = int(pkm[1]) m = int(pkm[2]) src = [] for i in range(0, m): temp = [] for j in range(0, p): temp.append(list(stdin.readline().strip())) src.append(temp) stdin.readline() res = {'a':[], 'b':[], 'c':[], 'd':[]} for i in range(0, m): for j in range(0, p): for l in range(0, k): if src[i][j][l] != '*': res[src[i][j][l]].append(bfs(src, i, j, l, m, p, k)) if count != 1: print print 'Case %d:' % (count) abcd = ['a', 'b', 'c', 'd'] for cc in abcd: s = cc for i in sorted(res[cc], reverse=True): s += ' ' + str(i) print s
0 0
- ZOJ-3110
- ZOJ
- ZOJ
- ZOJ
- ZOJ
- ZOJ
- ZOJ
- ZOJ
- ZOJ
- ZOJ
- ZOJ
- ZOJ
- ZOJ
- ZOJ
- ZOJ
- ZOJ
- ZOJ
- ZOJ
- WPF之DataGrid应用--几个例子
- DFT与FFT
- jQuery学习
- UVa 133
- 自定义的NSSegmentedControl
- ZOJ-3110
- C/S+P2P网络模型(二)--上传&下载文件
- 泛型的概念和使用
- python实现一个P2P文件发布
- 树 递归测试
- python中@property装饰器的用法
- SpringMVC(二)——流程控制
- android之应用程序安装位置application install location
- Java 采用MD5进行加密和解密