现在的位置: 首页 > 综合 > 正文

1627: [Usaco2007 Dec]穿越泥地 (BFS)

2018年04月25日 ⁄ 综合 ⁄ 共 903字 ⁄ 字号 评论关闭
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#define inf 0x7fffffff
using namespace std;
inline int read(){
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
struct data{
	int x,y;
}q[1000001];
const int xx[4]={-1,1,0,0},yy[4]={0,0,1,-1};
int x,y,n,s[1010][1010];
bool mp[1010][1010];
inline bool jud(int x,int y){
	if(x<0||y<0||x>1000||y>1000||mp[x][y]||s[x][y]!=-1)return 0;
	else return 1;
}
inline void bfs(){
	int t=0,w=1;s[500][500]=0;q[0]=(data){500,500};
	while(t!=w){
		data now=q[t++];
		for(int i=0;i<4;i++){
			if(jud(now.x+xx[i],now.y+yy[i])){
				q[w++]=(data){now.x+xx[i],now.y+yy[i]};
				s[now.x+xx[i]][now.y+yy[i]]=s[now.x][now.y]+1;
			}
		}
		if(s[x][y]!=-1){printf("%d",s[x][y]);return;}
	}
}
int main(){
	memset(s,-1,sizeof(s));
	x=read()+500;y=read()+500;n=read();
	for(int i=1;i<=n;i++)
		mp[read()+500][read()+500]=1;
	bfs();
	return 0;
}

抱歉!评论已关闭.