就一个i,j的变量的范围写错了,让我多交了两次,郁闷加粗心啊!不过也是,现在的错多了以后就有经验了,可以避免了!
#include<iostream>
using namespace std;
typedef struct fun
{
int x,y;
int pre;
}rr;
fun s[1000100];
bool a[1100][1100];
int sign[4][2]={0,-1,0,1,-1,0,1,0};
int bx,by,Base=0,leef=1;
void Bfs()
{
int i,j,x,y;
s[0].x=500;
s[0].y=500;
s[0].pre=-1;
a[500][500]=true;
while(1)
{
for(i=0; i<4; i++)
{
x=s[Base].x+sign[i][0];
y=s[Base].y+sign[i][1];
if(a[x][y]==true)
continue;
s[leef].x=x;
s[leef].y=y;
s[leef].pre=Base;
a[x][y]=true;
if(x==bx && y==by)
{
int sum=0;
j=leef;
while(s[j].pre!=-1)
{
sum++;
j=s[j].pre;
}
cout<<sum<<endl;
return ;
}
leef++;
}
Base++;
}
}
int main()
{
int n,b,c,i,j;
cin>>bx>>by>>n;
bx+=500;
by+=500;
for(i=0; i<=1010; i++)
for(j=0; j<=1010; j++)
a[i][j]=false;
for(i=0; i<n; i++)
{
cin>>b>>c;
a[500+b][500+c]=true;//代表有墙的
}
Bfs();
return 0;
}