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. iOS开发之SceneKit框架--SCNAction.h

    1.SCNAction简介 主要负责节点SCNNode的属性,实现node的渐变.移动.出现.消失.实现动画等. 2.相关API 节点的移动(earthNode的初始坐标(5,0,0)) //从当前位 ...

  2. latex ctex 的section不能写中文, /href

    问题描述:再使用超链接 /href 后发现section{}不能写入中文,以前是好使的,经过查询验证,需要在引导区里加入 \hypersetup{CJKbookmarks=true} 即可恢复正常.

  3. LightOJ-1138-Trailing Zeroes (III)-二分+求N!末尾0

    You task is to find minimal natural number N, so that N! contains exactly Q zeroes on the trail in d ...

  4. 跨数据库查询——dblink

    现在本地建一个dblink Create database link create public database link DBLINKTEST (名称) connect to MGP(用户名) i ...

  5. 2019-9-2-win10-uwp-弹起键盘不隐藏界面元素

    title author date CreateTime categories win10 uwp 弹起键盘不隐藏界面元素 lindexi 2019-09-02 12:57:38 +0800 2018 ...

  6. unity3d入门 Demo 学习记录

    闲来学习一下 unity3d 的Demo,记录如下. 官方 Demo,名字为 Roll-A-Ball,如图 场景比较简单,包含地面.玩家精灵.主摄像机.墙壁.可拾取的方块.分数为示 text.平行光源 ...

  7. 自定义Jquery:ajax,get,post方法

    var myAjax = { request: function(url, type, data, callback) { $.ajax(url, { type: type, data: data, ...

  8. Python全栈开发:socket

    Socket socket通常也称作"套接字",用于描述IP地址和端口,是一个通信链的句柄,应用程序通常通过"套接字"向网络发出请求或者应答网络请求. sock ...

  9. 迅雷 API 接口说明文档 -调用迅雷自动下载

    我们可以利用迅雷提供的开放API接口来自动下载文件.详细的接口说明大家可以看下面的. 先要说明一下的就是 迅雷的API接口是用 .com 来调用的 首先就是脚本了,各种语言写法不同,我这里提供用vbs ...

  10. C++ 系列:Boost Thread 编程指南

    转载自:http://www.cppblog.com/shaker/archive/2011/11/30/33583.html 作者: dozbC++ Boost Thread 编程指南0 前言1 创 ...