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. 不想用ubuntu了,换个系统manjaro - change

    # 下载 https://mirrors.tuna.tsinghua.edu.cn/osdn/storage/g/m/ma/manjaro/xfce/ # usb启动盘 rufus # 中文字体 pa ...

  2. rabbit-c编译 vs2013

    需要openssl的库

  3. 【LeetCode】按 tag 分类索引 (900题以下)

    链表:https://www.cnblogs.com/zhangwanying/p/9797184.html (共34题) 数组:https://www.cnblogs.com/zhangwanyin ...

  4. [web 安全]逻辑漏洞之密码重置

    原文:http://wooyun.jozxing.cc/static/drops/web-5048.html 密码找回逻辑一.用户凭证(密码找回的凭证太弱,暴力破解)1.当当网任意用户密码修改漏洞(h ...

  5. 程序员要注意!现在是RSS复兴的时候了

    一般来说,现代网络不乏恐怖,从无所不在的网络黑客到所有信息平台,再到各大平台的评论系统.不幸的是,我们建立的这个互联网并没有什么灵丹妙药.但任何人都厌倦了黑箱算法,控制你在网上看到的东西,一直存在但始 ...

  6. Java常用类库API之数字处理工具类

    数字处理工具类BigDecimal和DecimalFormat Java提供的java.text.DecimalFormat类,帮助我们用最快的速度将数据格式化为我们想要的样子.例如,取两位小数 im ...

  7. 19.go语言基础学习(下)——2019年12月16日

    2019年12月16日16:57:04 5.接口 2019年11月01日15:56:09 5.1 duck typing 1. 2. 接口 3.介绍 Go 语言的接口设计是非侵入式的,接口编写者无须知 ...

  8. Linux学习-基于CentOS7的ProxySQL实现读写分离

    一.实验环境 主机:3台,一台ProxySQL(192.168.214.37),两台主从复制,master(192.168.214.17),slave(192.168.214.27) 系统:CentO ...

  9. SystemUI分析

    SystemUI是安卓的一个系统APP,负责的内容有系统通知栏,状态栏,最近应用程序,锁屏,壁纸,屏保,系统对话框,截屏,录屏等功能. Apk的路径位于/system/priv-app,源码code位 ...

  10. ECS主动运维事件--让你HOLD住全场 (二)

    背景 数月前,我们推出了新的功能:ECS主动运维事件--让你HOLD住全场 https://yq.aliyun.com/articles/573782?spm=a2c4e.11155435.0.0.7 ...