标签:cst cpp iostream pre names bool span pop \n
在洛谷上被卡了一个点开了O2才过= =
bfs即可,为方便存储,把所有坐标+500
#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
const int N=1005,dx[]={-1,1,0,0},dy[]={0,0,-1,1};
int n,sx,sy;
bool a[N][N],v[N][N];
struct qwe
{
int x,y,b;
qwe(int X=0,int Y=0,int B=0)
{
x=X,y=Y,b=B;
}
};
int read()
{
int r=0,f=1;
char p=getchar();
while(p>‘9‘||p<‘0‘)
{
if(p==‘-‘)
f=-1;
p=getchar();
}
while(p>=‘0‘&&p<=‘9‘)
{
r=r*10+p-48;
p=getchar();
}
return r*f;
}
bool ok(int x,int y)
{
return x>=0&&y>=0&&x<=1000&&y<=1000&&!v[x][y]&&!a[x][y];
}
int main()
{
sx=read()+500,sy=read()+500,n=read();
for(int i=1;i<=n;i++)
{
int x=read()+500,y=read()+500;
a[x][y]=1;
}
queue<qwe>q;
q.push(qwe(500,500,0));
v[500][500]=1;
while(!q.empty())
{
int x=q.front().x,y=q.front().y,b=q.front().b;
if(x==sx&&y==sy)
{
printf("%d\n",b);
break;
}
q.pop();
v[x][y]=0;
for(int i=0;i<4;i++)
if(ok(x+dx[i],y+dy[i]))
q.push(qwe(x+dx[i],y+dy[i],b+1)),v[x+dx[i]][y+dy[i]]=1;
}
return 0;
}
bzoj 1627: [Usaco2007 Dec]穿越泥地【bfs】
标签:cst cpp iostream pre names bool span pop \n
原文地址:https://www.cnblogs.com/lokiii/p/8987243.html