您的位置:首页 > 其它

1091. Acute Stroke (30)

2017-10-24 11:46 316 查看
这题题目看半天看不懂。

只能网上去搜。

原来就是一个三维空间,用一个三位数组代替。

问你有多少个直接相连的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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: