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. Spark2.0集成Hive操作的相关配置与注意事项

    前言 已完成安装Apache Hive,具体安装步骤请参照,Linux基于Hadoop2.8.0集群安装配置Hive2.1.1及基础操作 补充说明 Hive中metastore(元数据存储)的三种方式 ...

  2. MVC项目集成swagger

    1.创建WebAPI项目解决方案 2.使用nuget引入Swashbuckle包 引入Swashbuckle包后App_Start文件夹下会多出一个SwaggerConfig文件 3.添加接口注释 项 ...

  3. Taro -- 获取用户手机号

    1. 安装 Taro 脚手架工具 安装 Taro 开发工具 @tarojs/cli 使用 npm 或者 yarn 全局安装 $ npm install -g @tarojs/cli $ yarn gl ...

  4. SICP 习题解 第二章

    计算机程序的构造和解释习题解答 Structure and Interpretation os Computer Programs Exercises Answer 第二章 构造数据抽象 练习2.17 ...

  5. poj 2187 Beauty Contest(平面最远点)

    Beauty Contest Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 24431   Accepted: 7459 D ...

  6. VPS 安装MySQL

    目前Centos下默认支持的数据库是MariaDB,MariaDB是mysql的增强版本,由于mysql被Oracle收购之后,mysql之父担心之后mysql会变成闭源的软件,就又开发了这个版本,支 ...

  7. c# 反射获取属性值 TypeUtils

    using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using Sy ...

  8. Anacond的介绍

    Anacond的介绍 Anaconda指的是一个开源的Python发行版本,其包含了conda.Python等180多个科学包及其依赖项. 因为包含了大量的科学包,Anaconda 的下载文件比较大( ...

  9. 51nod 1028 大数乘法 V2 【FFT模板题】

    题目链接 模板题.. #include<bits/stdc++.h> using namespace std; typedef int LL; typedef double db; nam ...

  10. 【CF1257D】Yet Another Monster Killing Problem【贪心】

    题意:给定一些怪物,每天可以选一个勇士进去打怪,每个勇士每天只能打不超过si个怪物,每个勇士只能打能力值≤pi的怪物,问最少多少天打完所有怪物 题解:贪心,每天尽可能多的去打怪,那么存一个对于长度为i ...