标签:des style http os io for art ar
4 4 SX.. XX.. .... 1..D 4 4 S.X1 .... ..XX ..XD 0 0
-1 9
思路:因为地图会变化,结构体里面要加一个数组保存地图,标记的话就标记根据地图的坐标和当前手中的炸药数量来标记,但是同样状态的标记可能会有不同的走法,所以用整形变量来存,并且设定一个状态可以访问的阀值,这里设为20。
#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
struct S{
int x,y,num,step;
char mp[8][8];
bool operator<(const S & p) const
{
return step>p.step;
}
}t;
int nxt[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
char s[8][9];
int vis[8][8][577];
int main()
{
int n,m,i,j,sx,sy,ex,ey;
while(~scanf("%d%d",&n,&m) && n)
{
priority_queue<S>que;
for(i=0;i<n;i++) scanf("%s",s[i]);
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(s[i][j]=='S')
{
s[i][j]='.';
sx=i,sy=j;
}
else if(s[i][j]=='D')
{
s[i][j]='.';
ex=i,ey=j;
}
}
}
memset(vis,0,sizeof vis);
t.x=sx;
t.y=sy;
for(i=0;i<n;i++) for(j=0;j<m;j++) t.mp[i][j]=s[i][j];
t.step=0;
t.num=0;
vis[sx][sy][0]++;
que.push(t);
while(!que.empty())
{
t=que.top();
if(t.x==ex && t.y==ey)
{
printf("%d\n",t.step);
break;
}
for(i=0;i<4;i++)
{
t.step++;
t.x+=nxt[i][0];
t.y+=nxt[i][1];
if(t.x>=0 && t.x<n && t.y>=0 && t.y<m && vis[t.x][t.y][t.num]<20)
{
if(t.mp[t.x][t.y]=='.')
{
vis[t.x][t.y][t.num]++;
que.push(t);
}
else if(t.mp[t.x][t.y]=='X' && t.num>0)
{
vis[t.x][t.y][t.num]++;
t.step++;
t.num--;
t.mp[t.x][t.y]='.';
vis[t.x][t.y][t.num]++;
que.push(t);
}
else if(t.mp[t.x][t.y]>='1' && t.mp[t.x][t.y]<='9')
{
vis[t.x][t.y][t.num]++;
t.num+=t.mp[t.x][t.y]-'0';
t.mp[t.x][t.y]='.';
vis[t.x][t.y][t.num]++;
que.push(t);
}
}
t=que.top();
}
que.pop();
}
if(que.empty()) printf("-1\n");
}
}
HDU-2128-Tempter of the Bone II(BFS),布布扣,bubuko.com
HDU-2128-Tempter of the Bone II(BFS)
标签:des style http os io for art ar
原文地址:http://blog.csdn.net/faithdmc/article/details/38351749