http://acm.hdu.edu.cn/showproblem.php?pid=2102
复习了一遍队列,借鉴了网上的一些不错的方法,学习了!
![15103834_woYm.gif](https://i-blog.csdnimg.cn/blog_migrate/c2b43fa84704baea424aefb6a07e9288.gif)
![15103835_tf2A.gif](https://i-blog.csdnimg.cn/blog_migrate/29989860e2727cce1ff6ad17b21b45c8.gif)
#include < iostream >
#include < cstdio >
#include < queue >
#define M 11
using namespace std;
char map[ 2 ][M][M];
int set [ 2 ][M][M];
int Dir[ 4 ][ 2 ] = {{ 1 , 0 },{ 0 , 1 },{ - 1 , 0 },{ 0 , - 1 }};
int n,m,t;
int mx,my,mz;
int i,j,k;
struct Maze
{
int x,y,z;
int step;
};
Maze P , S;
void Init()
{
for (i = 0 ; i < 2 ; ++ i)
for (j = 0 ; j < n; ++ j)
for (k = 0 ; k < m; ++ k)
set [i][j][k] = 0x7fffffff ;
set [ 0 ][ 0 ][ 0 ] = 0 ;
}
void BFS()
{
queue < Maze > Q;
P.x = 0 ;
P.y = 0 ;
P.z = 0 ;
P.step = 0 ;
Q.push(P);
Init();
while ( ! Q.empty())
{
P = Q.front();
Q.pop();
if (P.x == mx && P.y == my && P.z == mz && P.step <= t)
{
printf( " YES\n " );
return ;
}
for ( i = 0 ; i < 4 ; ++ i)
{
S = P;
S.x = S.x + Dir[i][ 0 ];
S.y = S.y + Dir[i][ 1 ];
S.step ++ ;
if (S.x >= 0 && S.y >= 0 && S.x < n && S.y < m && map[S.z][S.x][S.y] != ' * ' && S.step <= t)
{
if (map[S.z][S.x][S.y] == ' # ' )
{
S.z = ( ! S.z);
if (map[S.z][S.x][S.y] != ' * ' )
{
if (map[S.z][S.x][S.y] != ' # ' && set [S.z][S.x][S.y] >= S.step)
{
set [S.z][S.x][S.y] = S.step;
Q.push(S);
}
}
}
else
{
if ( set [S.z][S.x][S.y] >= S.step)
{
set [S.z][S.x][S.y] = S.step;
Q.push(S);
}
}
}
}
}
printf( " NO\n " );
}
int main()
{
int ii,jj,kk;
int w;
scanf( " %d " , & w);
while (w -- )
{
scanf( " %d%d%d " , & n, & m, & t);
for (ii = 0 ; ii < 2 ; ++ ii)
{
for (jj = 0 ; jj < n ; ++ jj )
{
scanf( " %s " , & map[ii][jj]);
for (kk = 0 ; kk < m ; ++ kk )
{
if (map[ii][jj][kk] == ' P ' )
{
mz = ii;mx = jj;my = kk;
}
}
}
}
BFS();
}
return 0 ;
}