您的位置:首页 > 其它

zoj 1649 BFS

2014-10-20 19:13 274 查看
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=649

我觉得这题写的很好

以下为我的代码

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

#include<queue>

#include<math.h>

#include<iostream>

#include<algorithm>

using namespace std;

const int INF=55555555;

const int sizen=500;

struct ele

{

int x;

int y;

}t,s;

char Map[sizen][sizen];

int mapp[sizen][sizen];

int d[4][2]={1,0,-1,0,0,1,0,-1};

void bfs(int x,int y,int n,int m)

{

int i;

queue<ele>p;

t.x=x;

t.y=y;

p.push(t);

while(p.size())

{

t=p.front();

p.pop();

for(i=0;i<4;i++)

{

x=t.x+d[i][0];

y=t.y+d[i][1];

if(x>=0&&x<n&&y>=0&&y<m&&Map[x][y]!='#')

{

if(Map[x][y]=='.'||Map[x][y]=='a')

if(mapp[x][y]>mapp[t.x][t.y]+1)

{

//printf("%d %d\n",x,y);

mapp[x][y]=mapp[t.x][t.y]+1;

s.x=x;

s.y=y;

p.push(s);

}

if(Map[x][y]=='x')

if(mapp[x][y]>mapp[t.x][t.y]+2)

{

mapp[x][y]=mapp[t.x][t.y]+2;

s.x=x;

s.y=y;

p.push(s);

}

}

}

}

}

int main()

{

int sx,sy;

int ex,ey;

int n,m;

int i,j;

while(scanf("%d%d",&n,&m)!=EOF)

{

for(i=0;i<n;i++)

scanf("%s",Map[i]);

for(i=0;i<n;i++)

for(j=0;j<m;j++)

{

if(Map[i][j]=='r')

sx=i,sy=j;

mapp[i][j]=INF;

if(Map[i][j]=='a')

ex=i,ey=j;

}

mapp[sx][sy]=0;

bfs(sx,sy,n,m);

if(mapp[ex][ey]!=INF)

printf("%d\n",mapp[ex][ey]);

else

printf("Poor ANGEL has to stay in the prison all his life.\n");

}

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: