Luo Gu p1605-- classic Maze dfs

https://www.luogu.org/problemnew/show/P1605

In this title's review dfs

Given an N * M squares maze, T-maze at the barrier, not through the barrier. Given the start point and end point, Q: After a maximum of 1 per box, how many from the start point to the end point coordinate coordinates the program. In the maze to move up and down about four ways, you can only move one square. No obstacles to ensure data starting point.

Title Description

Input and output formats

Input formats:

 

The first row N, M and T, N row, M being a column, T is the total number of disorders. The second line starting coordinates SX, SY, ending coordinate FX, FY. Then the coordinates of the point T lines, each behavioral disorders.

 

Output formats:

 

Given the start point and end point, and asked after each square up to 1 times the total number of programs from the starting point to the end point coordinate coordinates.

 

Sample input and output

Input Sample # 1:  Copy
2 2 1
1 1 2 2
1 2
Output Sample # 1:  Copy
1

Explanation

[Data] scale

1≤N,M≤5

 

The most basic dfs title, Maze.

And initially there was a little error in judgment when the program without first determining termination conditions, will count write in the front.

If the end point is an obstacle if not terminated but count.

error code:

#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=100005;
int vis[10][10];
int n,m,t;
int ans=0;
int x,y,x2,y2,xt,yt;
void dfs(int x,int y){
    if(x==x2&&y==y2){
        ans++;
        return ;
    }
    if(x>n||x<1||y>m||y<1) return ;
    if(vis[x][y]==1) return ;
    vis[x][y]=1;
    dfs(x+1,y);
    dfs(x-1,y);
    dfs(x,y+1);
    dfs(x,y-1);
    vis[x][y]=0;
}
int main(int argc, char * argv[]) 
{
    std::ios::sync_with_stdio(false);
    cin>>n>>m>>t;
    cin>>x>>y>>x2>>y2;
    for(int i=0;i<t;i++){
        cin>>xt>>yt;
        vis[xt][yt]=1;
    }
    dfs(x,y);
    cout<<ans<<endl;
    return 0;
}

// 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=100005;
int vis[10][10];
int n,m,t;
int ans=0;
int x,y,x2,y2,xt,yt;
void dfs(int x,int y){
    if(x>n||x<1||y>m||y<1) return ;
    if(vis[x][y]==1) return ;
    if(x==x2&&y==y2){
        ans++;
        return ;
    }
    vis[x][y]=1;
    dfs(x+1,y);
    dfs(x-1,y);
    dfs(x,y+1);
    dfs(x,y-1);
    vis[x][y]=0;
}
int main(int argc, char * argv[]) 
{
    std::ios::sync_with_stdio(false);
    cin>>n>>m>>t;
    cin>>x>>y>>x2>>y2;
    for(int i=0;i<t;i++){
        cin>>xt>>yt;
        vis[xt][yt]=1;
    }
    dfs(x,y);
    cout<<ans<<endl;
    return 0;
}

顺便复习一遍dfs的套路:

//DFS框架

int next[4][2]={
{0,1},  //向右走
{1,0},    //向下走 
{0,-1}, //向左走 
{-1,0}, //向上走 
};   ///定义一个方向数组

void dfs(int x,int y,int step){
    if(x==p && y==q){  ///判断是否到达位置 
        if(step<min)
        min=step;  //更新最小值
        return ; 
    }
    for(k=0;k<=3;k++){
        tx=x+next[k][0];
        ty=y+next[k][1];
        if(tx<1 || tx>n ||ty<1 || ty>m)  continue;  // 判断是否越界
        if(a[tx][ty]==0 && book[tx][ty]==0){
            book[tx][ty]=1;
            dfs(tx,ty,step+1);
            book[tx][ty]=0;
        } 
    } 
    return 0;
}

Guess you like

Origin www.cnblogs.com/wushengyang/p/11166127.html