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. python基础--匿名函数

    def calc(x): return x+1 a=calc(10) print(calc) print(lambda x:x+1)#lambda函数的内存地址,利用函数名可以作为值传递给变量 b=l ...

  2. bzoj3809 Gty的二逼妹子序列 & bzoj3236 [Ahoi2013]作业 莫队+分块

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=3809 https://lydsy.com/JudgeOnline/problem.php?id ...

  3. css--图片整合(精灵图)

    图片整合(精灵图) 精灵图的优点: 减少图片的字节 减少了网页的http请求,从而大大的提高了页面的性能 解决了网页设计师在图片命名上的困扰,只需对一张集合的图片上命名就可以了,不需要对每一个小元素进 ...

  4. fast

    # connect timeout in seconds# default value is 30sconnect_timeout=30 # network timeout in seconds# d ...

  5. 匈牙利算法&模板O(mn)HDU2063

    #include<cstdio> #include<cstring> #define maxn 510 using namespace std; int k,g,b,x,y,a ...

  6. 【HDOJ6630】permutation 2(递推)

    题意:给定x,y,n,有标号从1到n的n个数组,求合法的排列个数模998244353使得 1:p[1]=x 2:p[n]=y 3:相邻两项的差的绝对值<=2 n<=1e5 思路: #inc ...

  7. Windows无法启动MapGIS DataStorage Service服务

    但是启动又启动不了,查看属性 发现计算机服务器确实少了该文件目录.. 可能是不小心删除了? 之前确实有删过一些文件 下次直接把.net禁止就可以了,不用删除,不然不小心删除了其它服务.. 参考文献:h ...

  8. [java] [error] java.lang.OutOfMemoryError: unable to create new native thread

    前言 最近公司的服务器出现了oom的报错,经过一番排查,终于找到了原因.写下这篇博客是为了记录下查找的过程,也是为了帮助那些跟我门遇到的情况相同的人可以更快的寻找到答案. 环境 系统:linux(ce ...

  9. PHP 判断是否为手机端访问

    /* * 判断是否为手机端 */function check_wap(){ // 如果有HTTP_X_WAP_PROFILE则一定是移动设备 if (isset ($_SERVER['HTTP_X_W ...

  10. xxxxxxxxxxxxxxxxxxx

    <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Conten ...