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. https 生成秘钥

    #生成一个RSA秘钥 openssl genrsa -des3 -out a_com.key 1024 #生成一个证书请求openssl req -new -key a_com.key -out a_ ...

  2. os.path.basename()

    返回path最后的文件名.如果path以/或\结尾,那么就会返回空值.即os.path.split(path)的第二个元素. >>> import os >>> p ...

  3. 阿里云CentOs7上安装GitLab

    一.安装 基本上可以根据官网的教程来安装:https://www.gitlab.com.cn/installation/#centos-7 只不过我们暂时没有邮件服务器,所以postfix没有安装. ...

  4. php 取某一日期的前一天

    代码为: $date = “2009-01-01”;$time = strtotime($date) – 3600*24;echo date(‘Y-m-d’,$time); 或者一句:echo dat ...

  5. MFC入门--显示静态图片及调用本地软件

    MFC是微软开发的基础类库,主要用来开发图形界面应用程序,在学习中,我们要验证算法好坏,一般需要对结果进行可视化. OpenCV是计算机视觉中的开源算法库,集成了很多先进算法,现在想将MFC与Open ...

  6. 2019-8-30-PowerShell-通过-WMI-获取系统安装的驱动

    title author date CreateTime categories PowerShell 通过 WMI 获取系统安装的驱动 lindexi 2019-08-30 08:58:39 +080 ...

  7. JAVA-第一课 环境的配置

    首先 我们需要 下载java的开发工具包 jdk  jdk 的下载地址::http://www.oracle.com/technetwork/java/javase/downloads/index.h ...

  8. 3.pycharm spark配置

        pycharm 内的环境变量配置     选择相应的spark程序文件的对应的配置信息       PYSPARK_PYTHON:python的安装路径   PYTHONPATH:spark安 ...

  9. csp-s模拟测试56Merchant, Equation,Rectangle题解

    题面:https://www.cnblogs.com/Juve/articles/11619002.html merchant: 二分答案,贪心选前m大的 但是用sort复杂度不优,会T掉 我们只是找 ...

  10. slam课程

    美国宾夕法尼亚大学最近有录制一套 无人机视觉定位导航相关的视频课程,2019年3月份在YouTube上更新完毕,质量非常高,名字叫Robotics,视频课程列表:https://www.youtube ...