您的位置:首页 > 其它

hdu - 1072 Nightmare(bfs)

2015-05-28 19:32 405 查看
http://acm.hdu.edu.cn/showproblem.php?pid=1072

遇到Bomb-Reset-Equipment的时候除了时间恢复之外,必须把这个点做标记不能再走,不然可能造成死循环,也得不到最优解。

#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
struct point
{
int x,y,time,step;
};
point s;
int n,m;
int maze[15][15];
int dir[4][2]={{-1,0},{1,0},{0,1},{0,-1}};

void bfs()
{
queue<point>que;
que.push(s);
maze[s.x][s.y]=0;
while(!que.empty())
{
point t=que.front(); que.pop();
// printf("%d %d %d %d\n",t.x,t.y,t.step,t.time);
if(maze[t.x][t.y]==3&&t.time>0) {printf("%d\n",t.step);return;}
if(t.time==1) continue;
for(int i=0;i<4;i++)
{
s.x=t.x+dir[i][0],s.y=t.y+dir[i][1];
if(s.x>=0&&s.x<n&&s.y>=0&&s.y<m&&maze[s.x][s.y]!=0)
{
if(maze[s.x][s.y]==4)
{
s.time=6;
maze[s.x][s.y]=0;
}
else s.time=t.time-1;
s.step=t.step+1;
que.push(s);
}
}
}
printf("-1\n");
}
int main()
{
//freopen("a.txt","r",stdin);
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
{
scanf("%d",&maze[i][j]);
if(maze[i][j]==2)
{
s.x=i;
s.y=j;
s.step=0;s.time=6;
}
}
bfs();
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: