您的位置:首页 > 其它

1091. Acute Stroke 解析

2017-05-05 17:03 162 查看
求相连的体积,体积必须大于t才被计算在内。

猛地一看题目还有点懵。。

我觉得应该是一个BFS的三维扩展问题,然后用BFS的思想去解题就很好处理了。

直接开了个三维的数组,好担心超时。但是可以过~啦啦啦啦啦~~~~~

#include <iostream>
#include <vector>
#include <queue>

using namespace std;

int m, n, l, t;
int v[65][1300][130];
bool isVis[65][1300][130];
int sum = 0;
struct Node {
int l, m, n;
Node(int _l, int _m, int _n) :l(_l), m(_m), n(_n) {};
};

int BFS(int ll, int mm, int nn) {
int vol = 0;
queue <Node> q;
q.push(Node(ll, mm, nn));
isVis[ll][mm][nn] = true;
vol++;
while (!q.empty()) {
Node top = q.front();
q.pop();
int tl = top.l, tm = top.m, tn = top.n;
//上层
tl = top.l - 1, tm = top.m, tn = top.n;
if (tl >= 0 && !isVis[tl][tm][tn] && v[tl][tm][tn] == 1) {
isVis[tl][tm][tn] = true;
vol++;
q.push(Node(tl, tm, tn));
}
//下层
tl = top.l + 1, tm = top.m, tn = top.n;
if (tl <= l && !isVis[tl][tm][tn] && v[tl][tm][tn] == 1) {
isVis[tl][tm][tn] = true;
vol++;
q.push(Node(tl, tm, tn));
}
//同层 上
tl = top.l, tm = top.m - 1, tn = top.n;
if (tm >= 0 && !isVis[tl][tm][tn] && v[tl][tm][tn] == 1) {
isVis[tl][tm][tn] = true;
vol++;
q.push(Node(tl, tm, tn));
}

//同层 下
tl = top.l, tm = top.m + 1, tn = top.n;
if (tm <= m && !isVis[tl][tm][tn] && v[tl][tm][tn] == 1) {
isVis[tl][tm][tn] = true;
vol++;
q.push(Node(tl, tm, tn));
}

//同层 左
tl = top.l, tm = top.m, tn = top.n - 1;
if (tn >= 0 && !isVis[tl][tm][tn] && v[tl][tm][tn] == 1) {
isVis[tl][tm][tn] = true;
vol++;
q.push(Node(tl, tm, tn));
}

//同层 右
tl = top.l, tm = top.m, tn = top.n + 1;
if (tn <= n && !isVis[tl][tm][tn] && v[tl][tm][tn] == 1) {
isVis[tl][tm][tn] = true;
vol++;
q.push(Node(tl, tm, tn));
}

}

return vol;
}

void TravelBFS() {
for (int i = 0; i < l; i++) {
for (int j = 0; j < m; j++) {
for (int k = 0; k < n; k++) {
if (!isVis[i][j][k] && v[i][j][k] == 1) {
int tempSum = BFS(i, j, k);
if (tempSum >= t)
sum += tempSum;
}
}
}
}
cout << sum << endl;
}

int main() {
cin >> m >> n >> l >> t;

for (int i = 0; i < l; i++) {
for (int j = 0; j < m; j++) {
for (int k = 0; k < n; k++) {
cin >> v[i][j][k];
isVis[i][j][k] = false;
}
}
}

TravelBFS();

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  PAT