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: 复制

2 2 1
1 1 2 2
1 2
输出样例#1: 复制

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的更多相关文章

  1. 洛谷P1605 迷宫 (DFS)

    题目背景 迷宫 [问题描述] 给定一个N*M方格的迷宫,迷宫里有T处障碍,障碍处不可通过.给定起点坐标和 终点坐标,问: 每个方格最多经过1次,有多少种从起点坐标到终点坐标的方案.在迷宫 中移动有上下 ...

  2. 洛谷P1605 迷宫【dfs】

    题目背景 迷宫 [问题描述] 给定一个N*M方格的迷宫,迷宫里有T处障碍,障碍处不可通过.给定起点坐标和 终点坐标,问: 每个方格最多经过1次,有多少种从起点坐标到终点坐标的方案.在迷宫 中移动有上下 ...

  3. 洛谷 P1605 迷宫

    题目链接 https://www.luogu.org/problemnew/show/P1605 题目背景 迷宫 题目描述 给定一个N*M方格的迷宫,迷宫里有T处障碍,障碍处不可通过.给定起点坐标和 ...

  4. 洛谷—— P1605 迷宫

    P1605 迷宫 题目背景 迷宫 [问题描述] 给定一个N*M方格的迷宫,迷宫里有T处障碍,障碍处不可通过.给定起点坐标和 终点坐标,问: 每个方格最多经过1次,有多少种从起点坐标到终点坐标的方案.在 ...

  5. 洛谷P1605 迷宫——S.B.S.

    题目背景 迷宫 [问题描述] 给定一个N*M方格的迷宫,迷宫里有T处障碍,障碍处不可通过.给定起点坐标和 终点坐标,问: 每个方格最多经过1次,有多少种从起点坐标到终点坐标的方案.在迷宫 中移动有上下 ...

  6. 洛谷P1605 迷宫

    迷宫 题目链接 这道题就是一道简单的dfs计方案数qwq. 我的思路是把表初始化为1,再将障碍改为0,因为在全局定义中数组会直接初始化为0,所以就少去了对边界的特判. next数组加循环可以减少代码量 ...

  7. 洛谷P1605 迷宫 深度搜索 模板!

    题目背景 给定一个N*M方格的迷宫,迷宫里有T处障碍,障碍处不可通过.给定起点坐标和终点坐标,问: 每个方格最多经过1次,有多少种从起点坐标到终点坐标的方案.在迷宫中移动有上下左右四种方式,每次只能移 ...

  8. (Java实现) 洛谷 P1605 迷宫

    题目背景 迷宫 [问题描述] 给定一个N*M方格的迷宫,迷宫里有T处障碍,障碍处不可通过.给定起点坐标和 终点坐标,问: 每个方格最多经过1次,有多少种从起点坐标到终点坐标的方案.在迷宫 中移动有上下 ...

  9. 洛谷 p1605 迷宫问题 详解

    题解:dfs搜索 #include <iostream> #include <algorithm> #include <cstring> #include < ...

随机推荐

  1. (转) Mac下面的SecureCRT(附破解方案) 更新到最新的7.3.7

    Mac下面的SecureCRT(附破解方案) 更新到最新的7.3.7 转自 http://blog.csdn.net/skykingf/article/details/17450561 http:// ...

  2. 树链剖分(模板) 洛谷P3384

    #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #d ...

  3. JVM规范

  4. 无法启动此程序,因此计算机中丢失VCRUNTIME140.dll。

    在mysql-8.0.12-winx64创建data文件夹 在cmd终端 初始化 MYSQL: mysqld --initialize-insecure MySQL加入Windows服务:mysqld ...

  5. Spring中AOP的实现

    Spring中整合了AOP的功能,虽然有不足,没有专门做AOP框架的那么完美,但是用一用感觉还是不错的 一些概念: AOP 面向切面编程 aspect 切面/切面类(我个人认为一个真正被解耦的程序,切 ...

  6. xcart小数点位数

    xcart小数点的位数默认是2位,有时候需要根据需要更改位数:一开始以为把数据库中的数据类型的位数更改过后,就能生效,结果发现xcart在程序中作了限制,只能是2位.那么只能通过更改程序的方式来更改了 ...

  7. linux 服务 启动 关闭 列表

    ##查看服务在每个级别的运行状态 chkconfig --list httpd           0:关闭  1:关闭  2:关闭  3:关闭  4:关闭  5:启用  6:关闭 bluetooth ...

  8. [JZOJ5233] 【GDOI模拟8.5】概率博弈

    题目 题目大意 给你一棵树,这棵树上的所有叶子节点的权值是随机的排列. 两个人博弈,从根开始,轮流往下走. 先手希望权值最大,后手希望权值最小. 问期望的最终结果. 思考历程 这场比赛实在是太丧心病狂 ...

  9. CSS - 选择器相关

    1. 标签选择器 /* 标签选择器 : 会将样式作用在当前网页所有的指定标签上 标签名 { 样式名1: 样式值1; 样式名2: 样式值2; ...... } */ table { width: 300 ...

  10. Codeforces Round #258 (Div. 2)E - Devu and Flowers

    题意:n<20个箱子,每个里面有fi朵颜色相同的花,不同箱子里的花颜色不同,要求取出s朵花,问方案数 题解:假设不考虑箱子的数量限制,隔板法可得方案数是c(s+n-1,n-1),当某个箱子里的数 ...