讨论 / 困惑啊啊啊啊
mlz000 2012-05-26 05:25:00
点我顶贴 收藏 删除
哪里错了额

#include<cstdio>

#include<iostream>

#include<algorithm>

#include<cstring>

#include<queue>

using namespace std;

const int N=1005;

char s[N][N];

bool v[N][N];

int x[5]={0,1,-1,0,0};

int y[5]={0,0,0,1,-1};

int i,j,n;

int x1,y1,x2,y2,ans;

struct data

{

int x;

int y;

int num;

};

void bfs(data s)

{

ans=99999999;

data re,now;

queue<data> q;

q.push(s);

v[s.x][s.y]=false;

while(!q.empty())

{

re=q.front();

q.pop();

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

{

now.x=re.x+x[i];

now.y=re.y+y[i];

if(now.x>=0 && now.x<n && now.y>=0 &&now.y<n && v[now.x][now.y])

{

q.push(now);

v[now.x][now.y]=false;

now.num=re.num+1;

if(now.x==x2 && now.y==y2) ans=now.num;

}

}

}

}

int main()

{

scanf("%d",&n);

memset(v,true,sizeof(v));

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

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

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

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

if(s[i][j]=='1') v[i][j]=false;

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

x1--;y1--;x2--;y2--;

data s;

s.x=x1;

s.y=y1;

s.num=0;

bfs(s);

printf("%d",ans);

//system("pause");

return 0;

}

查看更多回复
提交回复