最少步数(dfs + bfs +bfs优化)
最少步数
- 描述
-
这有一个迷宫,有0~8行和0~8列:
1,1,1,1,1,1,1,1,1
1,0,0,1,0,0,1,0,1
1,0,0,1,1,0,0,0,1
1,0,1,0,1,1,0,1,1
1,0,0,0,0,1,0,0,1
1,1,0,1,0,1,0,0,1
1,1,0,1,0,1,0,0,1
1,1,0,1,0,0,0,0,1
1,1,1,1,1,1,1,1,10表示道路,1表示墙。
现在输入一个道路的坐标作为起点,再如输入一个道路的坐标作为终点,问最少走几步才能从起点到达终点?
(注:一步是指从一坐标点走到其上下左右相邻坐标点,如:从(3,1)到(4,1)。)
- 输入
- 第一行输入一个整数n(0<n<=100),表示有n组测试数据;
随后n行,每行有四个整数a,b,c,d(0<=a,b,c,d<=8)分别表示起点的行、列,终点的行、列。 - 输出
- 输出最少走几步。
- 样例输入
-
2
3 1 5 7
3 1 6 7 - 样例输出
-
12
11题解:dfs带回溯;找最小步数;还可以用广搜BFS,以及用优先队列优化;
- 代码:
-
#include<stdio.h>
#include<string.h>
#define MIN(x,y) x<y?x:y
const int MAXN=;
const int INF=<<;
int map[MAXN][MAXN]={
{,,,,,,,,},
{,,,,,,,,},
{,,,,,,,,},
{,,,,,,,,},
{,,,,,,,,},
{,,,,,,,,},
{,,,,,,,,},
{,,,,,,,,},
{,,,,,,,,}
};
int disx[]={,-,,};
int disy[]={,,-,};
int a,b,c,d,min;
void dfs(int x,int y,int t){int nx,ny;
if(x==c&&y==d){
min=MIN(min,t);
return ;
}
for(int i=;i<;i++){
nx=x+disx[i];ny=y+disy[i];
if(t+<min&&!map[nx][ny]){
map[nx][ny]=;
dfs(nx,ny,t+);
map[nx][ny]=;
}
}
return ;
}
int main(){
int T;
/* for(int x=0;x<9;x++){
for(int y=0;y<9;y++)printf("%d ",map[x][y]);
puts("");
}*/
scanf("%d",&T);
while(T--){min=INF;
scanf("%d%d%d%d",&a,&b,&c,&d);
map[a][b]=;
dfs(a,b,);
map[a][b]=;
printf("%d\n",min);
}
return ;}广搜:
#include<stdio.h>
#include<queue>
#include<string.h>
using namespace std;
const int INF=0xfffffff;
int disx[]={,,-,};
int disy[]={,,,-};
struct Node{
int nx,ny,step;
};
queue<Node>dl;
Node a,b;
int x,y,ex,ey,T,mi;
int map[][];
void bfs(){
map[x][y]=;
a.nx=x;a.ny=y;a.step=;
dl.push(a);
while(!dl.empty()){
a=dl.front();
dl.pop();
map[a.nx][a.ny]=;
if(a.nx==ex&&a.ny==ey){
if(a.step<mi)mi=a.step;
map[ex][ey]=;
}
for(int i=;i<;i++){
b.nx=a.nx+disx[i];b.ny=a.ny+disy[i];b.step=a.step+;
if(!map[b.nx][b.ny]&&b.step<=mi&&b.nx>=&&b.ny>=&&a.nx<&&b.ny<)dl.push(b);
}
}
}
int main(){
scanf("%d",&T);
while(T--){int m[][]={
{,,,,,,,,},
{,,,,,,,,},
{,,,,,,,,},
{,,,,,,,,},
{,,,,,,,,},
{,,,,,,,,},
{,,,,,,,,},
{,,,,,,,,},
{,,,,,,,,}
};
memcpy((int *)map,(int *)m,sizeof(m[][])*);
scanf("%d%d%d%d",&x,&y,&ex,&ey);
mi=INF;
bfs();
printf("%d\n",mi);
}
return ;
}#include<stdio.h>
#include<queue>
#include<string.h>
using namespace std;
const int INF=0xfffffff;
int disx[]={,,-,};
int disy[]={,,,-};
struct Node{
int nx,ny,step;
friend bool operator < (Node a,Node b){
return a.step > b.step;
}
};
priority_queue<Node>dl;
Node a,b;
int x,y,ex,ey,T,mi;
int map[][];
void bfs(){
map[x][y]=;
a.nx=x;a.ny=y;a.step=;
dl.push(a);
while(!dl.empty()){
a=dl.top();
dl.pop();
map[a.nx][a.ny]=;
if(a.nx==ex&&a.ny==ey){
if(a.step<mi)mi=a.step;
map[ex][ey]=;
}
for(int i=;i<;i++){
b.nx=a.nx+disx[i];b.ny=a.ny+disy[i];b.step=a.step+;
if(!map[b.nx][b.ny]&&b.step<=mi&&b.nx>=&&b.ny>=&&a.nx<&&b.ny<)dl.push(b);
}
}
}
int main(){
scanf("%d",&T);
while(T--){int m[][]={
{,,,,,,,,},
{,,,,,,,,},
{,,,,,,,,},
{,,,,,,,,},
{,,,,,,,,},
{,,,,,,,,},
{,,,,,,,,},
{,,,,,,,,},
{,,,,,,,,}
};
memcpy((int *)map,(int *)m,sizeof(m[][])*);
scanf("%d%d%d%d",&x,&y,&ex,&ey);
mi=INF;
bfs();
printf("%d\n",mi);
}
return ;
}
最少步数(dfs + bfs +bfs优化)的更多相关文章
- 最少步数(bfs)
最少步数 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描述 这有一个迷宫,有0~8行和0~8列: 1,1,1,1,1,1,1,1,1 1,0,0,1,0,0,1,0,1 ...
- 最少步数(bfs)
最少步数 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描述 这有一个迷宫,有0~8行和0~8列: 1,1,1,1,1,1,1,1,1 1,0,0,1,0,0,1,0,1 ...
- dfs和bfs的区别
详见转载博客:https://www.cnblogs.com/wzl19981116/p/9397203.html 1.dfs(深度优先搜索)是两个搜索中先理解并使用的,其实就是暴力把所有的路径都搜索 ...
- [转帖]dfs和bfs
dfs和bfs https://www.cnblogs.com/wzl19981116/p/9397203.html 1.dfs(深度优先搜索)是两个搜索中先理解并使用的,其实就是暴力把所有的路径都搜 ...
- 有关dfs、bfs解决迷宫问题的个人见解
可以使用BFS或者DFS方法解决的迷宫问题! 题目如下: kotori在一个n*m迷宫里,迷宫的最外层被岩浆淹没,无法涉足,迷宫内有k个出口.kotori只能上下左右四个方向移动.她想知道有多少出口是 ...
- 图论相关知识(DFS、BFS、拓扑排序、最小代价生成树、最短路径)
图的存储 假设是n点m边的图: 邻接矩阵:很简单,但是遍历图的时间复杂度和空间复杂度都为n^2,不适合数据量大的情况 邻接表:略微复杂一丢丢,空间复杂度n+m,遍历图的时间复杂度为m,适用情况更广 前 ...
- 【DFS与BFS】洛谷 P1135 奇怪的电梯
题目:奇怪的电梯 - 洛谷 (luogu.com.cn) 因为此题数据范围较小,有dfs及bfs等多种做法. DFS 比较正常的dfs,注意vis数组一定要回溯,不然会漏情况 例如这个数据 11 1 ...
- Clone Graph leetcode java(DFS and BFS 基础)
题目: Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. ...
- 数据结构(12) -- 图的邻接矩阵的DFS和BFS
//////////////////////////////////////////////////////// //图的邻接矩阵的DFS和BFS ////////////////////////// ...
随机推荐
- MassMutual Interview Questions
Company MassMutual Date 30/09/15 Location Boston, MA Position Application Developer It's not a codin ...
- LeeCode(Database)-Duplicate Emails
Write a SQL query to find all duplicate emails in a table named Person. +----+---------+ | Id | Emai ...
- MySQL无法重启问题解决Warning: World-writable config file ‘/etc/my.cnf’ is ignored
MySQL无法重启问题解决Warning: World-writable config file ‘/etc/my.cnf’ is ignored
- 【C++基础之十四】函参的缺省
可能会有这么一个函数,在大部分的情况下,我们不用给它传递参数,但在某些特殊情况下,我们需要给它传递参数,那怎么办呢? 简单啊,写两个一样的方法,一个带参,一个不带参... 这样也太没水准了.来点高端的 ...
- ESCAPE用法
ESCAPE用法1.使用 ESCAPE 关键字定义转义符: 在模式中,当转义符置于通配符之前时,该通配符就解释为普通字符. 2.ESCAPE 'escape_character' 允许在字符串中搜索通 ...
- C/C++中经常使用的字符串处理函数和内存字符串函数
一. 字符处理函数 1. 字符处理函数:<ctype.h> int isdigit(int ch) ;//是否为数字,即ch是否是0-9中的字符 int ...
- mysql源码安装(5.1)
下载mysql源码包并解压.wget http://dev.mysql.com/get/Downloads/MySQL-5.1/mysql-5.1.73.tar.gztar -zxvf mysql-5 ...
- .net web api 一
web api可以提供方便简单可靠的web服务,可以大量的用于不需要提供复杂的soap协议的环境,以简单明了的形式返回数据,在不太复杂的环境中web api可以做为wcf等重级web服务的一种可替代方 ...
- JQ 操作样式,背景切换
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 表设计与SQL优化
1. 说说分区表的主要好处是什么,为什么会有这些好处. 分区功能能够将表.索引或索引组织表进一步细分为段,这些数据库对象的段叫做分区.每个分区有自己的名称,还可以选择自己的存储特性. 从数据库管理员的 ...