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. 新金融ABS如何做?听听这几十家券商、互金高管的经验之谈

    新金融ABS如何做?听听这几十家券商.互金高管的经验之谈 2016-11-24 零壹财经 ID:36104743 时间:2016年11月初 地点:北京东城区南湾子3号院(场地提供方:无讼.天同律师事务 ...

  2. https 生成秘钥

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

  3. Python 字符串_python 字符串截取_python 字符串替换_python 字符串连接

    Python 字符串_python 字符串截取_python 字符串替换_python 字符串连接 字符串是Python中最常用的数据类型.我们可以使用引号('或")来创建字符串. 创建字符 ...

  4. Linux负载均衡利器(LVS)

    LVS是什么? LVS是Linux Virtual Server的简写,意即Linux虚拟服务器,是一个虚拟的服务器集群系统.本项目在1998年5月由章文嵩博士成立,是中国国内最早出现的自由软件项目之 ...

  5. [Neo4j] 添加算法插件包

    下载graph-algorithms-algo-xxx.jar包,我下的是3.5.3.1,放到neo4j目录的plugins文件夹下 修改 conf目录下的配置文件 neo4j.conf ,加一行: ...

  6. Python全栈开发:进度条

    import sys import time for i in range(31): # 清空打印内容 sys.stdout.write("\r") # 控制输出样式 sys.st ...

  7. C++: Type conversions

    1.static_cast     static_cast可以转换相关联的类,可以从子类转换成父类.也能从父类转向子类,但是如果转换的父类指针(或者父类引用)所指向的对象是完整的,那么是没有问题:但是 ...

  8. Excel skill: 如何替换换行符,以及如何把一格转换成多行/多列

    http://blog.sciencenet.cn/blog-508298-695290.html 增加一辅助列,用替换函数替换掉软回车.比如A列是数据,从A1开始,则插入B列,B1输入公式=REPL ...

  9. Python 变量与数据类型

    1.变量命名规则: 变量名只能是字母,数字和下划线的任意组合 变量名第一个字符不能是数字 变量名区分大小写,大小写字母被认为是两个不同的字符 特殊关键字不能命名为变量名 2.数值的运算 print ( ...

  10. C++Builder 常用String

    关于AnsiSting的使用大全(1) arrow: Ansistring 转 char 代码: void __fastcall TForm1::Button1Click(TObject *Sende ...