1091. Acute Stroke (30)
2017-10-24 11:46
316 查看
这题题目看半天看不懂。
只能网上去搜。
原来就是一个三维空间,用一个三位数组代替。
问你有多少个直接相连的1。这团1的总数如果超过阈值,就计数。
非常熟悉dfs的我10分钟就写完了。
郁闷的是提交发现,居然两个段错误。
确认数组下标没问题后,突然想到可能是递归层数过多。
改用bfs。
(PS:感觉好多题DFS不行但BFS可以,以后我得多用用BFS了)
只能网上去搜。
原来就是一个三维空间,用一个三位数组代替。
问你有多少个直接相连的1。这团1的总数如果超过阈值,就计数。
非常熟悉dfs的我10分钟就写完了。
郁闷的是提交发现,居然两个段错误。
确认数组下标没问题后,突然想到可能是递归层数过多。
改用bfs。
(PS:感觉好多题DFS不行但BFS可以,以后我得多用用BFS了)
#include <bits/stdc++.h> using namespace std; int arr[2000][2000][70]; bool test[2000][2000][70]; struct node{ int pai; int lie; int gao; node(int x, int y, int z):pai(x), lie(y), gao(z){ }; }; int res; int pai, lie, gao, limit; int main(void) { cin >> pai >> lie >> gao >> limit; int i, j, k; int re = 0; for (i = 0; i < gao; i++) { for (j = 0; j < pai; j++) { for (k = 0; k < lie; k++) { scanf("%d", &arr[j][k][i]); } } } queue<node> sup; for (i = 0; i < gao; i++) { for (j = 0; j < pai; j++) { for (k = 0; k < lie; k++) { if (arr[j][k][i]) { arr[j][k][i] = 0; res = 0; node start(j, k, i); sup.push(start); while (!sup.empty()) { node temp = sup.front(); sup.pop(); res++; int tpai = temp.pai; int tlie = temp.lie; int tgao = temp.gao; if (arr[tpai+1][tlie][tgao] == 1 && tpai + 1 < pai) { node nd(tpai+1, tlie, tgao); arr[tpai+1][tlie][tgao] = 0; sup.push(nd); } if (arr[tpai-1][tlie][tgao] == 1 && tpai - 1 >= 0) { node nd(tpai-1, tlie, tgao); arr[tpai-1][tlie][tgao] = 0; sup.push(nd); } if (arr[tpai][tlie+1][tgao] == 1 && tlie+1 < lie) { node nd(tpai, tlie+1, tgao); arr[tpai][tlie+1][tgao] = 0; sup.push(nd); } if (arr[tpai][tlie-1][tgao] == 1 && tlie-1 >= 0) { node nd(tpai, tlie-1, tgao); arr[tpai][tlie-1][tgao] = 0; sup.push(nd); } if (arr[tpai][tlie][tgao+1] == 1 && tgao+1 < gao) { node nd(tpai, tlie, tgao+1); arr[tpai][tlie][tgao+1] = 0; sup.push(nd); } if (arr[tpai][tlie][tgao-1] == 1 && tgao-1 >= 0) { node nd(tpai, tlie, tgao-1); arr[tpai][tlie][tgao-1] = 0; sup.push(nd); } } if (res >= limit) re += res; } } } } cout << re; }
相关文章推荐
- 1091. Acute Stroke (30)
- PAT (Advanced Level) 1091. Acute Stroke (30) 3维DFS(10M数据时栈溢出)、3维BFS(AC)
- 1091. Acute Stroke (30) -- DFS(三维空间)
- 【PAT】1091. Acute Stroke (30)
- 1091. Acute Stroke (30)
- 1091. Acute Stroke (30)
- 1091. Acute Stroke (30)
- 1091. Acute Stroke (30)
- PAT(A) - 1091. Acute Stroke (30)
- 1091. Acute Stroke (30)
- 1091. Acute Stroke (30)(bfs)
- PAT 1091. Acute Stroke (30)
- PAT (Advanced Level) Practise 1091 Acute Stroke (30)
- 1091. Acute Stroke (30)【搜索】——PAT (Advanced Level) Practise
- 1091. Acute Stroke (30)-PAT甲级真题(广度优先搜索)
- 1091. Acute Stroke (30)
- PAT甲题题解-1091. Acute Stroke (30)-BFS
- PAT(Advance) 1091. Acute Stroke (30)
- solution Of 1091. Acute Stroke (30)
- 1091. Acute Stroke (30)