https://ac.nowcoder.com/acm/contest/993/F

题意:从(0,0)到X , Y最少要走几步,其中有一些点是泥坑不能走。

思路:bfs注意:该题坐标会出现负数,所以标记数组要统一加500转化为正数。或则直接用map标记。

#include <iostream>
#include <iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <vector>
#include <queue>
#include <set>
using namespace std;
const int N = ;
int x[] , y[] , vis[][];
int len1 , len2 , dir[][] = {{ , } , {- , }, { , } ,{ , -}};
int mx , my , p; struct Node{
int x, y , ans ;
Node(int x = , int y = , int ans = ){
this->x = x ;
this->y = y ;
this->ans = ans ;//记录步数
}
}n[]; bool check(int x , int y)
{
if(!vis[x + N][y + N] && x + <= && x + >= && y + <= && y + >= )
return true ;
else
return false ;
} int bfs(int x , int y)
{
queue<Node>q;
q.push(Node(x , y , ));
vis[x + N][y + N] = ;
Node temp , step ;
while(!q.empty())
{ temp = q.front();
q.pop() ;
if(temp.x == mx && temp.y == my)
{
return temp.ans ;
}
for(int i = ; i < ; i++)
{
step.x = temp.x + dir[i][];
step.y = temp.y + dir[i][];
step.ans = temp.ans + ;
if(check(step.x , step.y))
{
vis[step.x + N][step.y + N] = ;
q.push(Node(step));
}
} } } void init()
{
memset(vis , , sizeof(vis));
} int main()
{
while(~scanf("%d%d%d" , &mx , &my , &p))
{
init() ;
for(int i = ; i < p ; i++)
{
scanf("%d%d" , &n[i].x , &n[i].y);
vis[n[i].x + N][n[i].y + N] = ;
}
cout << bfs( , ) <<endl ; } return ;
}
#include <iostream>
#include <iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <vector>
#include <queue>
#include <set>
using namespace std;
const int N = ;
int x[] , y[] , vis[][];
int len1 , len2 , dir[][] = {{ , } , {- , }, { , } ,{ , -}};
int mx , my , p; struct Node{
int x , y , step ;
}; void bfs()
{
queue<Node>q;
q.push({ , , });
vis[][] = ;
while(!q.empty())
{
Node temp = q.front();
q.pop() ;
int x = temp.x , y = temp.y , s = temp.step ;
if(x == mx + && y == my + )
{
printf("%d\n" , s);
break ;
}
for(int i = ; i < ; i++)
{
int tx = x + dir[i][];
int ty = y + dir[i][];
int ts = s + ;
if(tx < || tx > || ty < || ty > )
{
continue ;
}
if(vis[tx][ty])
continue ;
q.push({tx , ty , ts});
vis[tx][ty] = ;
} } } void init()
{
memset(vis , , sizeof(vis));
} int main()
{
while(~scanf("%d%d%d" , &mx , &my , &p))
{
init() ;
for(int i = ; i < p ; i++)
{
int a , b ;
scanf("%d%d" , &a , &b);
vis[a + N][b + N] = ;
}
bfs(); } return ;
}
 

bfs(最短路径)的更多相关文章

  1. 【POJ3182】The Grove BFS 最短路径周围

    意甲冠军:给定一个N*M图.,间'X'代表树木(树木必须汇集到森林,非分离),然后,'.'它代表的空间.'*'它代表的起点.现在它需要从起点.一圈,最后回到起点,所经过最少点数. 题目中给的'+'就是 ...

  2. [POJ 3984] 迷宫问题(BFS最短路径的记录和打印问题)

    题目链接:http://poj.org/problem?id=3984 宽度优先搜索最短路径的记录和打印问题 #include<iostream> #include<queue> ...

  3. 推箱子小游戏《格鲁的实验室》13关 - bfs最短路径

    下载了一款推箱子小游戏,第13关的时候怎么也破不了最佳纪录(最少步数是9而我们最好的方案是10步),因为数据比较小(6*8的方阵),所以写了个BFS来找最短路. 游戏的目标是把小黄人推到黄色球,小绿人 ...

  4. bfs(最短路径)

    http://poj.org/problem?id=3278 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submis ...

  5. DFS和BFS

    BFS 代码步骤: 1.写出每个点和每个点的邻接点的对应关系 2.方法参数:传一个对应关系和起始点 3.创建一个队列,然后每次都移除第一个,然后把移除的邻接点添加进去,打印取出的第一个,然后循环,一直 ...

  6. 算法导论—无向图的遍历(BFS+DFS,MATLAB)

    华电北风吹 天津大学认知计算与应用重点实验室 最后改动日期:2015/8/22 无向图的存储方式有邻接矩阵,邻接链表,稀疏矩阵等. 无向图主要包括双方面内容,图的遍历和寻找联通分量. 一.无向图的遍历 ...

  7. POJ 3083 Children of the Candy Corn (DFS + BFS)

    POJ-3083 题意: 给一个h*w的地图. '#'表示墙: '.'表示空地: 'S'表示起点: 'E'表示终点: 1)在地图中仅有一个'S'和一个'E',他们为位于地图的边墙,不在墙角: 2)地图 ...

  8. 数据结构之二叉搜索树、AVL自平衡树

    前言 最近在帮公司校招~~ 所以来整理一些数据结构方面的知识,这些知识呢,光看一遍理解还是很浅的,看过跟动手做过一遍的同学还是很容易分辨的哟~ 一直觉得数据结构跟算法,就好比金庸小说里的<九阳神 ...

  9. 算法与数据结构基础 - 图(Graph)

    图基础 图(Graph)应用广泛,程序中可用邻接表和邻接矩阵表示图.依据不同维度,图可以分为有向图/无向图.有权图/无权图.连通图/非连通图.循环图/非循环图,有向图中的顶点具有入度/出度的概念. 面 ...

  10. 关于图算法 & 图分析的基础知识概览

    网址:https://learning.oreilly.com/library/view/graph-algorithms-/9781492060116/ 你肯定没有读过这本书,因为这本书的发布日期是 ...

随机推荐

  1. 使用JSONP,jQuery的ajax跨域获取json数据

    网上找了很多资料,写的不错,推荐下: 1.深入浅出JSONP--解决ajax跨域问题 (http://www.cnblogs.com/chopper/archive/2012/03/24/240394 ...

  2. openstack stein部署手册 4. glance

    # 建立数据库用户及权限 create database glance; grant all privileges on glance.* to glance@'localhost' identifi ...

  3. Jupyter配置工作路径

    在修改之前,C:\Users\Administrator\ .jupyter 目录下面只有一个“migrated”文件. 打开命令窗口(运行->cmd),进入python的Script目录下输入 ...

  4. Linux性能优化从入门到实战:11 内存篇:内存泄漏的发现与定位

      用户空间内存包括多个不同的内存段,比如只读段.数据段.堆.栈以及文件映射段等.但会发生内存泄漏的内存段,只有堆和文件映射段中的共享内存.   内存泄漏的危害非常大,这些忘记释放的内存,不仅应用程序 ...

  5. python常用模块学习3

    # # dic='{"name":"tang"}' # # f=open("hello",'w') # # f.write(dic) # # ...

  6. Wannafly挑战赛27 C蓝魔法师

    链接Wannafly挑战赛27 C蓝魔法师 给出一棵树,求有多少种删边方案,使得删后的图每个连通块大小小于等于\(k\),\(n,k\leq 2*10^3\) 假设我们正在考虑\(i\)这个子树,那么 ...

  7. 37行代码构建无状态组件通信工具-让恼人的Vuex和Redux滚蛋吧!

    状态管理的现状 很多前端开发者认为,Vuex和Redux是用来解决组件间状态通信问题的,所以大部分人仅仅是用于达到状态共享的目的.但是通常Redux是用于解决工程性问题的,用于分离业务与视图,让结构更 ...

  8. RMQ HelloWorld

    原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11791681.html Project Directory Maven Dependency < ...

  9. [luogu] P3809 【模板】后缀排序 (SA)

    板子,照着题解打的倍增版. #include <iostream> #include <cstdio> #include <cstring> using names ...

  10. C语言版本学生信息管理系统

    仍然有一些小bug,后续会发布OC完善版的图书馆管理系统,欢迎批评指正. #include <stdio.h> void menu_choose(); typedef struct { i ...