hdu 1253

地址:http://acm.hdu.edu.cn/showproblem.php?pid=1253

题意:中文。。。

mark:三维BFS水过。其实和二维没多大区别。

代码:

# include <stdio.h>
# include <string.h>


# define REP(i,a) for(i = 0 ; i < a ; i++)


int q[55*55*55][3] ;
int graph[55][55][55] ;
int step[55][55][55] ;
int a, b, c, t ;
int tab[6][3] = {{0, 0, 1}, {0, 0, -1},
{0, 1, 0}, {0, -1, 0},
{1, 0, 0}, {-1, 0, 0}} ;

void bfs ()
{
int front = 0, rear = 1 ;
int x, y, z, xx, yy, zz ;
int i ;
memset (step, -1, sizeof(step)) ;
q[0][0] = q[0][1] = q[0][2] = 0 ;
step[0][0][0] = 0 ;
while (front != rear)
{
x = q[front][0], y = q[front][1], z = q[front][2] ;
front++ ;
if (x == a-1 && y == b-1 && z == c-1) return ;
if (step[x][y][z] >= t) continue ;
for (i = 0 ; i < 6 ; i++)
{
xx = x + tab[i][0] ;
yy = y + tab[i][1] ;
zz = z + tab[i][2] ;
if (xx < 0 || xx >= a) continue ;
if (yy < 0 || yy >= b) continue ;
if (zz < 0 || zz >= c) continue ;
if (step[xx][yy][zz] != -1) continue ;
if (graph[xx][yy][zz]==1) continue ;
step[xx][yy][zz] = step[x][y][z] + 1 ;
q[rear][0] = xx, q[rear][1] = yy, q[rear][2] = zz ;
rear++ ;
}
}
}


int main ()
{
int T, i, j, k ;
scanf ("%d", &T) ;
while (T--)
{
scanf ("%d%d%d%d", &a, &b, &c, &t) ;
REP(i,a) REP(j,b) REP(k,c)
scanf ("%d", &graph[i][j][k]) ;
bfs () ;
printf ("%d\n", step[a-1][b-1][c-1]) ;
}
return 0 ;
}



原文地址:https://www.cnblogs.com/lzsz1212/p/2316241.html