您的位置:首页 > 其它

hdu 1072 Nightmare (广搜)

2013-06-11 16:27 351 查看
// Time 0ms, Memory 356K
#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
int n,m,p[9][9],sx,sy,dx[]={0,1,0,-1},dy[]={1,0,-1,0};
struct point
{
int x,y,time,step;
point(int x=0,int y=0,int time=0,int step=0):x(x),y(y),time(time),step(step){}
};
int bfs()
{
point s(sx,sy,6,0),t;
queue<point>q;
int nx,ny,i;
q.push(s);
while(!q.empty())
{
s=q.front();q.pop();
for(i=0;i<4;i++)
{
nx=s.x+dx[i];ny=s.y+dy[i];
t=point(nx,ny,s.time-1,s.step+1);
if(t.x<0 || t.x>=n || t.y<0 || t.y>=m || !p[nx][ny] || t.time==0) continue;
if(p[nx][ny]==3) return t.step;
if(p[nx][ny]==4)
{
t.time=6;p[nx][ny]=0;
}
q.push(t);
}
}
return -1;
}
int main()
{
int i,j,c;
cin>>c;
while(c--)
{
scanf("%d%d",&n,&m);
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
scanf("%d",&p[i][j]);
if(p[i][j]==2)
{
sx=i;sy=j;p[i][j]=0;
}
}
}
printf("%d\n",bfs());
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ACM hdu C