Hdu2102 (dfs) Search questions
I won't say much about the meaning of this question. I can see that many people use bfsA, And then I use dfs to do the same thing as other deep searches. I just need to pay attention to some pruning in it. Otherwise, it will easily time out.
# Include
# Include
# Include
Using namespace std;
Char map [5] [15] [15];
Int mark [5] [15] [15], t, n, m, flash;
Int dir [4] [2] = {, 0,-, 0 };
Int dfs (int x, int y, int z, int tt)
{
Int I, xx, yy, zz;
If (map [x] [y] [z] = 'P ')
{
Flash = 1;
Return 0;
}
Else if (map [x] [y] [z] = '#')
{
If (x = 0) xx = 1;
Else xx = 0;
Yy = y;
Zz = z;
If (mark [xx] [yy] [zz] = 0 & map [xx] [yy] [zz]! = '*')
{
Mark [xx] [yy] [zz] = 1;
Dfs (xx, yy, zz, tt );
Mark [xx] [yy] [zz] = 0;
}
}
Else
{
For (I = 0; I <4; I ++)
{
Xx = x;
Yy = y + dir [I] [0];
Zz = z + dir [I] [1];
If (yy <0 | yy> = n | zz <0 | zz> = m) continue;
If (tt + 1> t) continue;
If (mark [xx] [yy] [zz] = 0 & map [xx] [yy] [zz]! = '*')
{
Mark [xx] [yy] [zz] = 1;
Dfs (xx, yy, zz, tt + 1 );
Mark [xx] [yy] [zz] = 0;
}
}
}
Return 0;
}
Int main ()
{
Int T, I, j;
Scanf ("% d", & T );
While (T --)
{
Scanf ("% d", & n, & m, & t );
For (I = 0; I Scanf ("% s", map [0] [I]);
For (I = 0; I Scanf ("% s", map [1] [I]);
Map [0] [0] [0] = '.';
Memset (mark, 0, sizeof (mark ));
Mark [0] [0] [0] = 1;
Flash = 0;
Dfs (0, 0, 0 );
If (flash) printf ("YES \ n ");
Else printf ("NO \ n ");
}
Return 0;
}