1 3 3 4 20 0 1 1 1 0 0 1 1 0 1 1 1 1 1 1 1 1 0 0 1 0 1 1 1 0 0 0 0 0 1 1 0 0 1 1 0
11
这题我TM逗比的犯那种逗比错误。。忘记打return 0;了 。。。不知道是不是不小心删了。。。
代码其实没什么好说的。。简单的BFS。。
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<vector> #include<queue> #include<cmath> using namespace std; const int maxn = 51; const int INF = 0x3f3f3f3f; int map[maxn][maxn][maxn];//地图 int vist[maxn][maxn][maxn];//标记数组 int a, b, c;//x,y,z int mintime;//魔王回来的时间 int ans; int dx[]= {1,-1,0,0,0,0}; int dy[]= {0,0,0,0,-1,1}; int dz[]= {0,0,-1,1,0,0}; int xx, yy, zz; struct point { int x, y, z; int time; }; queue<point>q; int bfs() { while( !q.empty() ) { point temp = q.front(); q.pop(); if( temp.x == a-1 && temp.y == b-1 && temp.z == c-1 && temp.time<=mintime) return temp.time; for(int i=0; i<6; i++) { xx = temp.x + dx[i]; yy = temp.y + dy[i]; zz = temp.z + dz[i]; if( xx>=0 && xx<a && yy>=0 && yy<b && zz>=0 && zz<c && map[xx][yy][zz]!=1 && !vist[xx][yy][zz] ) { point next; next.x = xx; next.y = yy; next.z = zz; next.time = temp.time + 1; vist[xx][yy][zz] = 1; q.push( next ); } } } return -1; } int main() { int cas; scanf("%d", &cas); while( cas-- ) { while( !q.empty() ) q.pop(); memset(map, 0, sizeof(map)); memset(vist, 0, sizeof(vist)); scanf("%d%d%d%d", &a, &b, &c, &mintime); for(int i=0; i<a; i++) for(int j=0; j<b; j++) for(int k=0; k<c; k++) scanf("%d", &map[i][j][k]); if( map[a-1][b-1][c-1]==1 ) { printf("-1\n"); continue; } if( a==1 && b==1 && c==1) { printf("0\n"); continue; } point start; start.x = 0; start.y = 0; start.z = 0; start.time = 0; q.push( start ); vist[0][0][0] = 1; ans = bfs(); printf("%d\n", ans); } return 0; }
HDU 1253:胜利大逃亡(简单三维BFS),布布扣,bubuko.com
原文:http://blog.csdn.net/u013487051/article/details/38141565