思路:easy BFS

//By SiriusRen
#include <queue>
#include <cstdio>
#include <algorithm>
using namespace std;
queue<pair<int,int> >q;
int x,y,jyx,jyy,n,xx[]={1,-1,0,0},yy[]={0,0,1,-1},vis[1555][1555];
bool map[1555][1555];
bool check(int X,int Y){
return X>=0&&X<=1000&&Y>=0&&Y<=1000&&!map[X][Y];
}
int main(){
scanf("%d%d%d",&x,&y,&n);
x+=500,y+=500;
for(int i=1;i<=n;i++){
scanf("%d%d",&jyx,&jyy);
map[jyx+500][jyy+500]=1;
}
vis[500][500]=1;
q.push(make_pair(500,500));
while(!q.empty()){
int dx=q.front().first,dy=q.front().second;q.pop();
if(vis[x][y]){printf("%d\n",vis[x][y]-1);return 0;}
for(int i=0;i<4;i++){
int tx=dx+xx[i],ty=dy+yy[i];
if(!vis[tx][ty]&&check(tx,ty)){
vis[tx][ty]=vis[dx][dy]+1;
q.push(make_pair(tx,ty));
}
}
}
puts("-1");
}

POJ 3626 BFS的更多相关文章

  1. poj 3249(bfs+dp或者记忆化搜索)

    题目链接:http://poj.org/problem?id=3249 思路:dp[i]表示到点i的最大收益,初始化为-inf,然后从入度为0点开始bfs就可以了,一开始一直TLE,然后优化了好久才4 ...

  2. poj 2395 bfs/记录路径

    http://poj.org/problem?id=2935 Basic Wall Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submi ...

  3. Dungeon Master POJ - 2251(bfs)

    对于3维的,可以用结构体来储存,详细见下列代码. 样例可以过,不过能不能ac还不知道,疑似poj炸了, #include<iostream> #include<cstdio> ...

  4. Q - 迷宫问题 POJ - 3984(BFS / DFS + 记录路径)

    Q - 迷宫问题 POJ - 3984 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, ...

  5. POJ 2251 BFS(简单)

    一道三维的BFS Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 24003 Accepted: 9 ...

  6. poj 3026 bfs+prim Borg Maze

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9718   Accepted: 3263 Description The B ...

  7. poj 3635(bfs+优先队列)

    题目链接:http://poj.org/problem?id=3635 思路:本题主要运用的还是贪心思想,由于要求st->ed的最小花费,那么每经过一个城市,能不加油就尽量不加油,用dp[i][ ...

  8. Prime Path(POJ 3126 BFS)

    Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15325   Accepted: 8634 Descr ...

  9. Red and Black(poj 1979 bfs)

    Red and Black Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 27891   Accepted: 15142 D ...

随机推荐

  1. Leetcode-Best Time to Buy and Sell Stock -java

    题目: Say you have an array for which the ith element is the price of a given stock on day i. If you w ...

  2. 在 Win8.1 上安装 Dedup

    install-package Microsoft-Windows-ServerCore-FullServer-Package~31bf3856ad364e35~amd64~~6.3.9600.163 ...

  3. 13款用于拍摄全景照片的iOS应用

    全景图是一种大画幅.用来展示尽量多的周围环境的照片,甚至能够展示一个球状的完整空间,让观赏者直接“站在”摄影师的位置,在照片里将该环境一览无余.全景照片能够以最直观的方式向人们展示和记录一个美丽风景的 ...

  4. Linux下PHP开启Oracle支持(oci8)

    使用php的常见问题是:编译php时忘记加入某扩展,后来想加入扩展,可是由于安装php后又装了一些东西如PEAR等,不想删除文件夹重装,那么此时就须要自己又一次添加某模块支持了,Linux操作系统下能 ...

  5. vncserverpassword改动

    前几天去客户现场,客户说有測试库.Linux下的,帮忙给新建一个数据库,我这么热心的人.是吧 那就開始吧. 一般使用vnc搞图形安装.熟练的打开vnc.输入password,报错!!我愣了几秒,忽然反 ...

  6. Can not Stop-Computer in powershell 6.0

    1 PS C:\Program Files\PowerShell\6.0.0-beta.6> Stop-ComputerStop-Computer : Failed to stop the co ...

  7. 【DNN 系列】 添加模块后不显示

    添加模块后不显示分为几个原因 1.检查.dnn文件是否填写正确,要和对应的页面文件对应上 我有一步是这这个名称地方我填上了 就不显示了.这里需要注意,VIEW 的名城是不需要写的 2.重写文件 实体操 ...

  8. tensorboard 使用

    TensorBoard是TensorFlow 的可视化工具.主要为了更方便用户理解 TensorFlow 程序.调试与优化,用户可以用 TensorBoard 来展现 TensorFlow 图像,绘制 ...

  9. HDU-2045 不容易系列之(3)—— LELE的RPG难题 找规律&递推

    题目链接:https://cn.vjudge.net/problem/HDU-2045 找规律 代码 #include <cstdio> long long num[51][2]; int ...

  10. [USACO07DEC]道路建设Building Roads

    题目:洛谷P2872.POJ3625. 题目大意:给你n个点的坐标,有些点已经有边连通,现在要你连上剩下的所有点,求这些边的最小长度是多少(不包括原来的边). 解题思路:最小生成树,把所有边处理出来, ...