luogu P1238 Walking the Maze--DFS Template Good (Water) Question

Topic description

There is a maze of m*n grids (meaning there are m rows and n columns), in which there are walkable and unwalkable ones. If you use 1 to indicate that you can walk, and 0 to indicate that you cannot walk, the m*n data is read into the file. And the starting point and ending point (the starting point and the ending point are both described by two data, representing the row number and column number of this point respectively). Now you are asked to program to find all feasible roads, requiring that there are no repeated points in the road you walk, and you can only walk in four directions, up, down, left, and right. If none of the paths are feasible, output the corresponding information (use -l to indicate no path).

Priority order: top left and bottom right

Input and output format

Input format:

The first line is the two numbers m, n (1<m, n<15), the next is the data consisting of 1 and 0 in m rows and n columns, and the last two rows are the start and end points.

 

Output format:

All feasible paths are described in the form of (x, y) when describing a point. Except for the starting point, all other paths must use "一>" to indicate the direction.

If there is no feasible way, output -1.

 

Input and output example

Input example #1: 
5 6
1 0 0 1 0 1
1 1 1 1 1 1
0 0 1 1 1 0
1 1 1 1 1 0
1 1 1 0 1 1
1 1
5 6
Sample output #1: 
(1,1)->(2,1)->(2,2)->(2,3)->(2,4)->(2,5)->(3,5)->(3,4)->(3,3)->(4,3)->(4,4)->(4,5)->(5,5)->(5,6)
(1,1)->(2,1)->(2,2)->(2,3)->(2,4)->(2,5)->(3,5)->(3,4)->(4,4)->(4,5)->(5,5)->(5,6)
(1,1)->(2,1)->(2,2)->(2,3)->(2,4)->(2,5)->(3,5)->(4,5)->(5,5)->(5,6)
(1,1)->(2,1)->(2,2)->(2,3)->(2,4)->(3,4)->(3,3)->(4,3)->(4,4)->(4,5)->(5,5)->(5,6)
(1,1)->(2,1)->(2,2)->(2,3)->(2,4)->(3,4)->(3,5)->(4,5)->(5,5)->(5,6)
(1,1)->(2,1)->(2,2)->(2,3)->(2,4)->(3,4)->(4,4)->(4,5)->(5,5)->(5,6)
(1,1)->(2,1)->(2,2)->(2,3)->(3,3)->(3,4)->(2,4)->(2,5)->(3,5)->(4,5)->(5,5)->(5,6)
(1,1)->(2,1)->(2,2)->(2,3)->(3,3)->(3,4)->(3,5)->(4,5)->(5,5)->(5,6)
(1,1)->(2,1)->(2,2)->(2,3)->(3,3)->(3,4)->(4,4)->(4,5)->(5,5)->(5,6)
(1,1)->(2,1)->(2,2)->(2,3)->(3,3)->(4,3)->(4,4)->(3,4)->(2,4)->(2,5)->(3,5)->(4,5)->(5,5)->(5,6)
(1,1)->(2,1)->(2,2)->(2,3)->(3,3)->(4,3)->(4,4)->(3,4)->(3,5)->(4,5)->(5,5)->(5,6)
(1,1)->(2,1)->(2,2)->(2,3)->(3,3)->(4,3)->(4,4)->( 4,5)->(5,5)->(5,6) 

is relatively simple for those who have already learned to search. A little problem is to output the position of each step.
  Simple, it can be done with a structure containing two int variables, output if established, return if not established;
DFS board:
#include<bits/stdc++.h>
using namespace std;
int dx[5]={0,-1,0,1};
int dy[5]={-1,0,1,0};
struct haha{
    int x,y;
}road[1000];
int vis[1000][1000];
int m,n,a[30][30],qix,qiy,zx,zy,k;
void dfs(int x,int y,int bu){
    if(x==zx&&y==zy){
        k=1;
        printf("(%d,%d)",qix,qiy);
        for(int i=1;i<bu;i++){
            printf("->(%d,%d)",road[i].x,road[i].y);            
        }
        cout<<"\n";
        return ;
    }
    for(int i=0;i<4;i++){
        int cur=x+dx[i];
        int str=y+dy[i];
        if(cur>=1&&cur<=m&&a[cur][str]==1&&str>=1&&str<=n&&!vis[cur][str]){
            vis[cur][str]=1;
            road[bu].x=x+dx[i];
            road[bu].y=y+dy[i];
            dfs(road[bu].x,road[bu].y,bu+1);
            road[bu].x-=dx[i];
            road[bu].y-=dy[i];
            vis[cur][str]=0;
        }
    }
}
int main()
{
    cin>>m>>n;
    for(int i=1;i<=m;i++){
        for(int j=1;j<=n;j++){
            cin>>a[i][j];
        }
    }
    cin>>qix>>qiy>>zx>>zy;
    vis[qix][qiy]=1;
    dfs(qix,qiy,1);
    if(k==0)cout<<"-1";
}

 

此为个人略解,转载请标明出处:http://www.cnblogs.com/rmy020718/p/8835498.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324404385&siteId=291194637