讨论 / 195题求大神解救啊
medivh5long 2013-04-07 20:33:00
点我顶贴 收藏 删除
#include<cstdio>

#include<cstring>

#include<cmath>

#include<cstdlib>

#include<algorithm>

#include<iostream>

using namespace std;

const int maxn=2010;

const int dx[]={-1,1,0,0};

const int dy[]={0,0,-1,1};

int n,m;

char map[maxn][maxn];

bool h[maxn][maxn]={0};

int l=0,r=0;

struct state

{

int x,y;

int step;

}start,end,q[maxn*maxn];

void push(state pos)

{

if(h[pos.x][pos.y])return;

q[r++]=pos;

h[pos.x][pos.y]=true;

}

state pop()

{

return q[l++];

}

void readdata()

{

scanf("%d%d",&n,&m);

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

{

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

scanf("%d",&map[i][j]);

}

int x1,y1,x2,y2;

scanf("%d%d",&x1,&y1);

start.x=x1-1;

start.y=y1-1;

start.step=0;

push(start);

scanf("%d%d",&x2,&y2);

end.x=x2-1;

end.y=y2-1;

if(x1==x2&&y1==y2)

{

printf("0\n");

exit(0);

}

}

state expand(state cs,int i)

{

cs.x+=dx[i];

cs.y+=dy[i];

cs.step+=1;

return cs;

}

bool check(state cs)

{

if(cs.x<0 || cs.x>=m || cs.y<0 ||cs.y>=n)return false;

if(map[cs.x][cs.y]==1)return false;

if(cs.x==end.x&&cs.y==end.y)

{

printf("%d\n",cs.step);

exit(0);

}

return true;

}

void work()

{

while(l<r)

{

state cs=pop();

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

{

state ns=expand(cs,i);

if(check(ns))push(ns);

}

}

printf("No Answer!\n");

}

int main()

{

readdata();

work();

return 0;

}

虽然说是个很简单的BFS题。但是总是有一组数据过不了。用双向的也不行。。。求破。。。

查看更多回复
提交回复