bfs(最短路径)
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(最短路径)的更多相关文章
- 【POJ3182】The Grove BFS 最短路径周围
意甲冠军:给定一个N*M图.,间'X'代表树木(树木必须汇集到森林,非分离),然后,'.'它代表的空间.'*'它代表的起点.现在它需要从起点.一圈,最后回到起点,所经过最少点数. 题目中给的'+'就是 ...
- [POJ 3984] 迷宫问题(BFS最短路径的记录和打印问题)
题目链接:http://poj.org/problem?id=3984 宽度优先搜索最短路径的记录和打印问题 #include<iostream> #include<queue> ...
- 推箱子小游戏《格鲁的实验室》13关 - bfs最短路径
下载了一款推箱子小游戏,第13关的时候怎么也破不了最佳纪录(最少步数是9而我们最好的方案是10步),因为数据比较小(6*8的方阵),所以写了个BFS来找最短路. 游戏的目标是把小黄人推到黄色球,小绿人 ...
- bfs(最短路径)
http://poj.org/problem?id=3278 Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submis ...
- DFS和BFS
BFS 代码步骤: 1.写出每个点和每个点的邻接点的对应关系 2.方法参数:传一个对应关系和起始点 3.创建一个队列,然后每次都移除第一个,然后把移除的邻接点添加进去,打印取出的第一个,然后循环,一直 ...
- 算法导论—无向图的遍历(BFS+DFS,MATLAB)
华电北风吹 天津大学认知计算与应用重点实验室 最后改动日期:2015/8/22 无向图的存储方式有邻接矩阵,邻接链表,稀疏矩阵等. 无向图主要包括双方面内容,图的遍历和寻找联通分量. 一.无向图的遍历 ...
- POJ 3083 Children of the Candy Corn (DFS + BFS)
POJ-3083 题意: 给一个h*w的地图. '#'表示墙: '.'表示空地: 'S'表示起点: 'E'表示终点: 1)在地图中仅有一个'S'和一个'E',他们为位于地图的边墙,不在墙角: 2)地图 ...
- 数据结构之二叉搜索树、AVL自平衡树
前言 最近在帮公司校招~~ 所以来整理一些数据结构方面的知识,这些知识呢,光看一遍理解还是很浅的,看过跟动手做过一遍的同学还是很容易分辨的哟~ 一直觉得数据结构跟算法,就好比金庸小说里的<九阳神 ...
- 算法与数据结构基础 - 图(Graph)
图基础 图(Graph)应用广泛,程序中可用邻接表和邻接矩阵表示图.依据不同维度,图可以分为有向图/无向图.有权图/无权图.连通图/非连通图.循环图/非循环图,有向图中的顶点具有入度/出度的概念. 面 ...
- 关于图算法 & 图分析的基础知识概览
网址:https://learning.oreilly.com/library/view/graph-algorithms-/9781492060116/ 你肯定没有读过这本书,因为这本书的发布日期是 ...
随机推荐
- 采用pacemaker+corosync实现postgresql双机热备、高可用方案
环境说明 参照上章已完成postgresql流复制配置,并关闭postgres服务. su - postgres pg_ctl -D /data/postgresql/data/ stop -m fa ...
- Robot Framework 源码阅读 day1 run.py
robot里面run起来的接口主要有两类 run_cli def run_cli(arguments): """Command line execution entry ...
- Maya2014下载安装与激活
目录 1. 更多推荐 2. 下载地址 2.1. OneDrive 2.2. 其他下载地址 3. 激活步骤 1. 更多推荐 其他Maya版本的下载与激活:https://www.cnblogs.com/ ...
- dotnet ef执行报错, VS 2019发布时配置项中的Entity Framework迁移项显示不出来
VS 2019发布时配置项中的Entity Framework迁移项显示不出来 dotnet ef dbcontext list --json “无法执行,因为找不到指定的命令或文件.可能的原因包括: ...
- wannafly25 E 01串
链接 wannafly25 E 01串 给出一个\(01\)串,有两种操作,操作一是将某一个位置的数字修改,操作二是询问某一个区间,将这个区间看做\(1\)个二进制数,可以随意加减\(2\)的幂次,问 ...
- MongoDB Compass管理工具下载、安装和使用
内容来自:https://jingyan.baidu.com/article/925f8cb884f6f8c0dce0565a.html ,https://blog.csdn.net/bg101775 ...
- URLEncode解决url中有特殊字符的问题
问题:图片上传后的url地址中有&等特殊字符,页面传到后端时被自动处理成了& 解决:前端对url进行URLEncode,后端收到后进行URLDecode 总结:需要在请求u ...
- DELPHI FMX 同时使用LONGTAP和TAP
在应用到管理图标时,如长按显示删除标志,单击取消删除标志.在FMX的手势管理中,只有长按LONGTAP,点击TAP则是单独的事件,不能在同事件中管理.在执行LONGTAP后,TAP也会被触发,解决方 ...
- HashMap测试程序2
package com.iotek.map; import java.util.HashMap;import java.util.Map; public class HashMapDemo2 { /* ...
- 树状数组(一维&二维函数)
//一维 void add(int x,int p) { while(x<=n)t[x]+=p,x+=(x&(-x)); } int query(int x) { int ans=0; ...