hdu 1253
地址:http://acm.hdu.edu.cn/showproblem.php?pid=1253
题意:中文……
mark:bfs,wa了无数次啊,,细节处理的不好,没注意走不出去的情况。可能有两种,第一种根本无法走到门,第二种门本来就是死路。
代码:
#include <stdio.h> #include <string.h> int s[130000][3],d[50][50][50],h[50][50][50],a,b,c,t; void bfs() { int front = 0, rear = 1; int aa,bb,cc,aaa,bbb,ccc,i; int tab[6][3] = {1,0,0,-1,0,0,0,1,0,0,-1,0,0,0,1,0,0,-1}; s[0][0] = s[0][1] = s[0][2] = 0; while(front != rear) { aa = s[front][0]; bb = s[front][1]; cc = s[front++][2]; if(d[aa][bb][cc] > t) {d[a-1][b-1][c-1] = -1; return ;} if(aa == a-1 && bb == b-1 && cc == c-1) return ; for(i = 0; i < 6; i++) { aaa = aa+tab[i][0]; bbb = bb+tab[i][1]; ccc = cc+tab[i][2]; if(aaa >= 0 && aaa < a && bbb >= 0 && bbb < b && ccc >= 0 && ccc < c && !d[aaa][bbb][ccc] && !h[aaa][bbb][ccc]) { s[rear][0] = aaa; s[rear][1] = bbb; s[rear++][2] = ccc; d[aaa][bbb][ccc] = d[aa][bb][cc]+1; } } } d[a-1][b-1][c-1] = -1; return ; } int main() { // freopen("in.txt", "r", stdin); int i,j,k,p; scanf("%d", &p); while(p-- && 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", &h[i][j][k]); if(h[a-1][b-1][c-1] == 1) {printf("-1\n"); continue;} memset(d, 0, sizeof(d)); bfs(); if(d[a-1][b-1][c-1] <= t) printf("%d\n", d[a-1][b-1][c-1]); else printf("-1\n"); } return 0; }