一道ACM题目,内存一直超限
RT:http://acm.hdu.edu.cn/showproblem.php?pid=1253
以下是我的代码DFS:
#include<iostream>
#include<queue>
using namespace std;
const int MAX = 50;
int map[MAX][MAX][MAX];
bool mark[MAX][MAX][MAX];
int A, B, C, T;
struct Dot{
int a;
int b;
int c;
int t;
};
queue<Dot> Q;
void BFS(){
while (Q.empty() == false){
Dot dot = Q.front();
int a = dot.a;
int b = dot.b;
int c = dot.c;
int t = dot.t;
if (a == A - 1 && b == B - 1 && c == C - 1 && !mark[a][b][c]){
mark[a][b][c] = true;
if (t <= T)
cout << t << endl;
else
cout << -1 << endl;
return;
}
if (t > T){
cout << -1 << endl;
return;
}
mark[a][b][c] = true;
Dot temp;
if (a > 0 && !mark[a - 1][b][c] && map[a - 1][b][c] == 0){
temp.t = t + 1;
temp.a = a - 1;
temp.b = b;
temp.c = c;
Q.push(temp);
}
if (a<A-1&&!mark[a + 1][b][c] && map[a + 1][b][c] == 0){
temp.t = t + 1;
temp.a = a + 1;
temp.b = b;
temp.c = c;
Q.push(temp);
}
if (b > 0 && !mark[a][b - 1][c] && map[a][b - 1][c] == 0){
temp.t = t + 1;
temp.a = a;
temp.b = b-1;
temp.c = c;
Q.push(temp);
}
if (b<B-1&&map[a][b + 1][c] == 0 && !mark[a][b + 1][c]){
temp.t = t + 1;
temp.a = a ;
temp.b = b+1;
temp.c = c;
Q.push(temp);
}
if (c > 0 && !mark[a][b][c - 1] && map[a][b][c - 1] == 0){
temp.t = t + 1;
temp.a = a;
temp.b = b;
temp.c = c-1;
Q.push(temp);
}
if (c<C-1&&map[a][b][c + 1] == 0 && !mark[a][b][c + 1]){
temp.t = t + 1;
temp.a = a;
temp.b = b;
temp.c = c + 1;
Q.push(temp);
}
Q.pop();
}
}
int main(){
int n;
while (scanf_s("%d", &n) != EOF){
scanf_s("%d%d%d%d", &A, &B, &C, &T);
for (int i = 0; i < A; i++)
{
for (int j = 0; j < B; j++)
{
for (int k = 0; k < C; k++)
{
map[i][j][k] = 0;
// cin >> map[i][j][k];
scanf_s("%d", &map[i][j][k]);
mark[i][j][k] = 0;
}
}
}
while (!Q.empty()){
Q.pop();
}
Dot d;
d.a = 0;
d.b = 0;
d.c = 0;
d.t = 0;
map[0][0][0] = 0;
Q.push(d);
BFS();
if (!mark[A - 1][B - 1][C - 1])
cout << -1 << endl;
}
return 0;
}
然后是网上的答案:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <queue>
using namespace std;
bool mark[50][50][50];
bool m[50][50][50];
int t;
int go[][3] =
{
1, 0, 0,
-1, 0, 0,
0, 1, 0,
0, -1, 0,
0, 0, 1,
0, 0, -1
};
struct e
{
int x, y, z;
int t;
};
queue <e> q;
int bfs(int a, int b, int c)
{
while (!q.empty())
{
e now;
now = q.front();
q.pop();
if (now.t >= t) {
return -1;
}
for (int i = 0; i<6; i++)
{
int nx = now.x + go[i][0];
int ny = now.y + go[i][1];
int nz = now.z + go[i][2];
if (nx<0 || nx >= a || ny<0 || ny >= b || nz<0 || nz >= c) continue;
if (mark[nx][ny][nz] == 1) continue;
if (m[nx][ny][nz] == 1) continue;
m[nx][ny][nz] = 1;
e tmp;
tmp.x = nx;
tmp.y = ny;
tmp.z = nz;
tmp.t = now.t + 1;
if (tmp.t <t) {
q.push(tmp);
}
mark[nx][ny][nz] == 1;
if (nx == a - 1 && ny == b - 1 && nz == c - 1)
return tmp.t;
}
}
return -1;
}
int main()
{
int a, b, c;
int k;
while (scanf_s("%d", &k) != eof)
{
while (k--)
{
scanf_s("%d%d%d%d", &a, &b, &c, &t);
for (int i = 0; i<a; i++)
{
for (int j = 0; j<b; j++)
{
for (int k = 0; k<c; k++)
{
scanf_s("%d", &m[i][j][k]);
mark[i][j][k] = 0;
}
}
}
while (!q.empty()) q.pop();
e tmp;
tmp.x = tmp.y = tmp.z = tmp.t = 0;
q.push(tmp);
mark[0][0][0] = 1;
int rec = bfs(a, b, c);
if (rec <= t && rec != -1) printf("%d\n", rec);
else printf("-1\n");
}
}
return 0;
}
为什么网上的可以编译,我的一直内存超限啊????