hdu 1253 bfs

来源:互联网 发布:阿里云幕布申请 编辑:程序博客网 时间:2024/05/07 13:34

这个题啊 WA了N次 Time out了N次

继续加油!!


#include<iostream>#include<cstdio>#include<queue>using namespace std;#define MAXN 55int a, b, c, t, castle[MAXN][MAXN][MAXN],vist[MAXN][MAXN][MAXN];int rx[] = { 1, -1, 0, 0, 0, 0 };int ry[] = { 0, 0, 1, -1, 0, 0 };int rz[] = { 0, 0, 0, 0, 1, -1 };int scheck(int ix, int iy, int iz){if (ix < 0 || iy < 0 || iz < 0 || ix >= a || iy >= b || iz >= c || castle[ix][iy][iz] == 1 || vist[ix][iy][iz] == 1){return 0;}return 1;}int abs(int x,int a){if (x - a < 0)return a - x;return x - a;}struct node{int x, y, z, step;};int bfs(){queue<node> q;node m, n;int i, j, k,s,ss;m.x = 0;m.y = 0;m.z = 0;m.step = 0;vist[0][0][0] = 1;q.push(m);while (!q.empty()){n = q.front();q.pop();for (s = 0; s < 6; s++){m.x = n.x + rx[s];m.y = n.y + ry[s];m.z = n.z + rz[s];m.step = n.step;if (scheck(m.x,m.y,m.z) == 1){m.step++;vist[m.x][m.y][m.z] = 1;if (m.x == a - 1 && m.y == b - 1 && m.z == c - 1 && m.step <= t){return m.step;}if (abs(m.x, a-1) + abs(m.y, b-1) + abs(m.z, c-1) + m.step > t){continue;}q.push(m);}}}return -1;}int main(){freopen("TestDate.txt", "r", stdin);int test,i,j,k;scanf("%d", &test);while (test--){scanf("%d %d %d %d", &a, &b, &c, &t);for (i = 0; i < a; i++){for (j = 0; j < b; j++){for (k = 0; k < c; k++){scanf("%d", &castle[i][j][k]);}}}if (castle[a - 1][b - 1][c - 1] == 1){cout << "-1" << endl;continue;}memset(vist, 0, sizeof(vist));cout << bfs() << endl;}return 0;}


0 0
原创粉丝点击