洛谷p1605--迷宫 经典dfs
https://www.luogu.org/problemnew/show/P1605
用这种题来复习一下dfs
给定一个N*M方格的迷宫,迷宫里有T处障碍,障碍处不可通过。给定起点坐标和终点坐标,问: 每个方格最多经过1次,有多少种从起点坐标到终点坐标的方案。在迷宫中移动有上下左右四种方式,每次只能移动一个方格。数据保证起点上没有障碍。
题目描述
输入输出格式
输入格式:
第一行N、M和T,N为行,M为列,T为障碍总数。第二行起点坐标SX,SY,终点坐标FX,FY。接下来T行,每行为障碍点的坐标。
输出格式:
给定起点坐标和终点坐标,问每个方格最多经过1次,从起点坐标到终点坐标的方案总数。
输入输出样例
说明
【数据规模】
1≤N,M≤5
最基本的dfs题目,走迷宫。
一开始还出现了一点错误,在判断方案的时候没有先判断终止条件,将计数写在了最前面。
如果终点是障碍物的话没有终止而是计数。
错误代码:
#include <stdio.h>
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <string>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <ctime>
#include <vector>
#include <fstream>
#include <list>
#include <iomanip>
#include <numeric>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define me0(s) memset(s,0,sizeof(s))
#define me1(s) memese(s,1,sizeof(s))
const int inf = 0x3f3f3f3f;
const int N=;
int vis[][];
int n,m,t;
int ans=;
int x,y,x2,y2,xt,yt;
void dfs(int x,int y){
if(x==x2&&y==y2){
ans++;
return ;
}
if(x>n||x<||y>m||y<) return ;
if(vis[x][y]==) return ;
vis[x][y]=;
dfs(x+,y);
dfs(x-,y);
dfs(x,y+);
dfs(x,y-);
vis[x][y]=;
}
int main(int argc, char * argv[])
{
std::ios::sync_with_stdio(false);
cin>>n>>m>>t;
cin>>x>>y>>x2>>y2;
for(int i=;i<t;i++){
cin>>xt>>yt;
vis[xt][yt]=;
}
dfs(x,y);
cout<<ans<<endl;
return ;
} // 3 3 2
// 1 1 3 3
// 2 2
// 3 3
// 0 //这组样例应该输出0,而上面代码输出的2
正确代码:
#include <stdio.h>
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <string>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <ctime>
#include <vector>
#include <fstream>
#include <list>
#include <iomanip>
#include <numeric>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define me0(s) memset(s,0,sizeof(s))
#define me1(s) memese(s,1,sizeof(s))
const int inf = 0x3f3f3f3f;
const int N=;
int vis[][];
int n,m,t;
int ans=;
int x,y,x2,y2,xt,yt;
void dfs(int x,int y){
if(x>n||x<||y>m||y<) return ;
if(vis[x][y]==) return ;
if(x==x2&&y==y2){
ans++;
return ;
}
vis[x][y]=;
dfs(x+,y);
dfs(x-,y);
dfs(x,y+);
dfs(x,y-);
vis[x][y]=;
}
int main(int argc, char * argv[])
{
std::ios::sync_with_stdio(false);
cin>>n>>m>>t;
cin>>x>>y>>x2>>y2;
for(int i=;i<t;i++){
cin>>xt>>yt;
vis[xt][yt]=;
}
dfs(x,y);
cout<<ans<<endl;
return ;
}
顺便复习一遍dfs的套路:
//DFS框架 int next[][]={
{,}, //向右走
{,}, //向下走
{,-}, //向左走
{-,}, //向上走
}; ///定义一个方向数组 void dfs(int x,int y,int step){
if(x==p && y==q){ ///判断是否到达位置
if(step<min)
min=step; //更新最小值
return ;
}
for(k=;k<=;k++){
tx=x+next[k][];
ty=y+next[k][];
if(tx< || tx>n ||ty< || ty>m) continue; // 判断是否越界
if(a[tx][ty]== && book[tx][ty]==){
book[tx][ty]=;
dfs(tx,ty,step+);
book[tx][ty]=;
}
}
return ;
}
洛谷p1605--迷宫 经典dfs的更多相关文章
- 洛谷P1605 迷宫 (DFS)
题目背景 迷宫 [问题描述] 给定一个N*M方格的迷宫,迷宫里有T处障碍,障碍处不可通过.给定起点坐标和 终点坐标,问: 每个方格最多经过1次,有多少种从起点坐标到终点坐标的方案.在迷宫 中移动有上下 ...
- 洛谷P1605 迷宫【dfs】
题目背景 迷宫 [问题描述] 给定一个N*M方格的迷宫,迷宫里有T处障碍,障碍处不可通过.给定起点坐标和 终点坐标,问: 每个方格最多经过1次,有多少种从起点坐标到终点坐标的方案.在迷宫 中移动有上下 ...
- 洛谷 P1605 迷宫
题目链接 https://www.luogu.org/problemnew/show/P1605 题目背景 迷宫 题目描述 给定一个N*M方格的迷宫,迷宫里有T处障碍,障碍处不可通过.给定起点坐标和 ...
- 洛谷—— P1605 迷宫
P1605 迷宫 题目背景 迷宫 [问题描述] 给定一个N*M方格的迷宫,迷宫里有T处障碍,障碍处不可通过.给定起点坐标和 终点坐标,问: 每个方格最多经过1次,有多少种从起点坐标到终点坐标的方案.在 ...
- 洛谷P1605 迷宫——S.B.S.
题目背景 迷宫 [问题描述] 给定一个N*M方格的迷宫,迷宫里有T处障碍,障碍处不可通过.给定起点坐标和 终点坐标,问: 每个方格最多经过1次,有多少种从起点坐标到终点坐标的方案.在迷宫 中移动有上下 ...
- 洛谷P1605 迷宫
迷宫 题目链接 这道题就是一道简单的dfs计方案数qwq. 我的思路是把表初始化为1,再将障碍改为0,因为在全局定义中数组会直接初始化为0,所以就少去了对边界的特判. next数组加循环可以减少代码量 ...
- 洛谷P1605 迷宫 深度搜索 模板!
题目背景 给定一个N*M方格的迷宫,迷宫里有T处障碍,障碍处不可通过.给定起点坐标和终点坐标,问: 每个方格最多经过1次,有多少种从起点坐标到终点坐标的方案.在迷宫中移动有上下左右四种方式,每次只能移 ...
- (Java实现) 洛谷 P1605 迷宫
题目背景 迷宫 [问题描述] 给定一个N*M方格的迷宫,迷宫里有T处障碍,障碍处不可通过.给定起点坐标和 终点坐标,问: 每个方格最多经过1次,有多少种从起点坐标到终点坐标的方案.在迷宫 中移动有上下 ...
- 洛谷 p1605 迷宫问题 详解
题解:dfs搜索 #include <iostream> #include <algorithm> #include <cstring> #include < ...
随机推荐
- (34)C#异常
一.异常的层次结构 二.异常格式 异常的一般格式 try { //可能会抛出异常的代码 } catch { //发现错误后会运行这里面的代码 } finally { //写不论是否出现异常都执行的代码 ...
- 缓冲(cache)和缓存(buffer)
缓存: 指把常用数据存储到可以快速获取的区域,以备重复利用 一般叫做cache. 缓存能提高效率 缓冲: 是指在数据流转过程中,不同层次速度不一致时,利用缓冲区来缓解上下层之间速率问题(性能差异) 一 ...
- mysql重点,表查询操作和多表查询
表单查询 1. 完整的查询语句语法 select distinct(* or 字段名 or 四则运算 )from 表名 where 条件 group by 条件 having 条件 order by ...
- Hibernate数据保存操作方法的原理对比
Interface Session All Superinterfaces: Serializable All Known Subinterfaces: EventSource, Session Al ...
- shell常用命令及正则辅助日志分析统计
https://www.cnblogs.com/wj033/p/3451618.html 正则日志分析统计 3 grep 'onerror' v3-0621.log | egrep -v '(\d ...
- springboot-配置多数据源之番外篇(分包实现)
场景: 随着业务发展,系统连接多数据库成为常态,继前面AOP的实现方式之后,这里记录一下分包实现的方式. 实现: 1.pom.xml <?xml version="1.0" ...
- 10_springmvc JSON数据交互
一.JSON数据交互 json数据格式在接口调用中.html页面中较常用,json格式比较简单,解析还比较方便.比如:webservice接口,传输json数据. 二.springmvc进行数据交互 ...
- Java开发系列-文件上传
概述 Java开发中文件上传的方式有很多,常见的有servlet3.0.common-fileUpload.框架.不管哪种方式,对于文件上传的本质是不变的. 文件上传的准备 文件上传需要客户端跟服务都 ...
- 跳过爱奇艺优酷vip
1.google chrome浏览器 2.下载插件安装 Tampermonkey https://pan.baidu.com/s/1qvRQD2UO6gPHogjtSUBwUw 将 ...
- 读《深入PHP 面向对象、模式与实践》笔记
1. include() 和require() 语句的不同在于它们如何处理错误.使用require()调用文件发生错误时,将会停止整个程序;调用include()时遇到相同的错误,则会生成警告并停止执 ...