Nightmare hdu 1072

#include <stdio.h>
#include
<string.h>
#include
<stdlib.h>
#include
<queue>
#include
<algorithm>

using namespace std;

int T, N, M, a, b, c, d, f1;
int map[10][10];
int xx[ ]= {0, 1, -1, 0};
int yy[ ]= {1, 0, 0, -1};
int hash[10][10];

struct node
{
int x, y, t, n;
/*bool operator < (const node &A) const
{
return A.t < t;
}
*/
}Nd;

int judge( int x, int y)
{
if ( x <= 0 || x > N || y <= 0 || y > M )
return 0;
return 1;
}

void BFS( )
{
int i, j, x1, x2, y1, y2, t1, t2;
queue
<node>q;
f1
= 0;
Nd.x
= a, Nd.y = b, Nd.t = 6, Nd.n = 0;
q.push(Nd);
memset(hash,
0, sizeof(hash));
hash[a][b]
= 6;
while( !q.empty( ))
{
Nd
= q.front( );
q.pop( );

x1
= Nd.x, y1 = Nd.y, t1 = Nd.t, t2 = Nd.n;
//printf("%d %d %d %d\n",x1, y1, t1, t2);
if (x1 == c && y1 == d )
{
f1
= 1;
printf(
"%d\n",t2);
break;
}
for (i = 0; i < 4; i++)
{
x2
= x1 + xx[i];
y2
= y1 + yy[i];

Nd.x
= x2, Nd.y = y2, Nd.t = t1 - 1, Nd.n = t2 + 1;
if (judge(x2,y2) && map[x2][y2] != 0 && Nd.t > hash[x2][y2] )
{
if (map[x2][y2] == 4) {
Nd.t
= 6;
hash[x2][y2]
= 6;
q.push(Nd);
}
else
{
hash[x2][y2]
= Nd.t;
q.push(Nd);
}
}
}
}
}





int main( )
{
int i, j;

scanf(
"%d",&T);
while (T--)
{
scanf(
"%d%d",&N, &M);
for(i = 1; i <= N; i++)
for(j = 1; j <= M; j++) {
scanf(
"%d",&map[i][j]);
if (map[i][j] == 2)
{
a
= i;
b
= j;
}
if (map[i][j] == 3)
{
c
= i;
d
= j;
}
}
//printf("%d %d %d %d\n",a, b, c, d);
BFS( );
if( !f1)
puts(
"-1");
}
return 0;
}

posted on 2011-08-09 20:19  more think, more gains  阅读(136)  评论(0编辑  收藏  举报

导航