思路: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. Ubuntu 16.04 Chrome浏览器安装flash player插件

    1:官网下载插件  flash palyer lash_player_npapi_linux_debug.x86_64.tar.gz 2:解压 提取 libpepflashplayer.so 3:手动 ...

  2. Linux常用软件tree,autojump,lrzsz安装

    tree安装 1.获取压缩文件 wget http://mama.indstate.edu/users/ice/tree/src/tree-1.7.0.tgz 2.解压缩 tar -zxvf tree ...

  3. Mysql 日期型,索引查询的问题

    问题: 表中,有一个日期字段WorkDate(Date YYYY-MM-DD格式),现在我把它建成了索引,在检索条件时,WorkDate='YYYY-MM-DD' 时,用EXPLAIN分析,能看到使用 ...

  4. [Javascript] Required function arguments in Javascript

    In Javascript, all function arguments are optional by default. That means if you ever forget to pass ...

  5. jquery-easyui的datagrid在checkbox多选时,行选中不正确应,去除高亮的解决方法

    jquery-easyui的datagrid在checkbox多选时,行选中不正确应,去除高亮的解决方法 工作中用到一个具有多选功能的easyui-datagrid在处理cell的点击事件时,不同 ...

  6. Connect to MongoDB

    https://docs.mongodb.com/getting-started/csharp/client/ MongoDB C# Driver is the officially supporte ...

  7. nyoj--12--喷水装置(二)(区间覆盖问题+贪心)

    喷水装置(二) 时间限制:3000 ms  |  内存限制:65535 KB 难度:4 描述 有一块草坪,横向长w,纵向长为h,在它的橫向中心线上不同位置处装有n(n<=10000)个点状的喷水 ...

  8. 13.C语言隐藏黑窗口

    //预编译,linker链接,Windows模式 #pragma comment(linker,"/subsystem:\"windows\" /entry:\" ...

  9. java操作文件创建、删除

    java操作文件创建.删除: package test; import java.io.File; import java.io.IOException; import org.slf4j.Logge ...

  10. 安卓通过Json注册登录

    对于刚开始做安卓的来说,可能一个好的Demo比什么都来得快,但是最近在做安卓登录注册的时候,发现基本找不到我想要的东西,无奈只好硬着头皮做,好在不负付出,终于搞定,也算是给自己一个交待. 从结构上说, ...