Rakutani AT1350 depth-first search

Rakutani AT1350 depth-first search

Luo Gu Portal

Translation of the meaning of problems

Takahashi living cell is rectangular, it is divided into a lattice. Mr. Takahashi wanted to go fish shop from home, Mr. Takahashi can walk every one around him before and after the four grid, but can not go sideways, not out of the district.

Now given by:

s: On behalf of Mr. Takahashi's home

g: Is the fish shop

.: On behalf of the road

#: On behalf Wall

Mr. Takahashi can not pass through walls.

Input: a first row of each cell input n (1 <= n <= 500), m (1 <= m <= 500) representative of the length and width of the cell, next n lines each m characters, described lattice.

Output: If Mr. Takahashi can reach the fish shop, output "Yes", otherwise a "No".

Sample input and output

Input # 1 copy

Output # 1 copy

Input # 2 Copy

Output # 2 Copy

Input # 3 Copy

Output # 3 Copy

Enter # 4 Copy

Output # 4 Copy

Input # 5 Copy

Output # 5 Copy

answer:

It should be regarded as common application of deep search - map to traverse the template title.

This map is traversed from konjac own name. On behalf of a class of relatively common problem, similar to Maze, a traversal of the matrix. This type of exercise is to use the basic search (deep search and achieve wide search just is not the same, and ultimately achieve the purpose is the same) to resolve. Then this solution to a problem you talk about doing detailed idea of ​​this problem and the basic implementation of deep search.

First, we should define the depth of the search have to understand. However, this embodiment is defined based on a depth first traversal of the tree and may be more easily understood by everyone. So there should be a lot of little friends are like this, like konjac Mongolia circle: This problem no plans no trees, and deep search have relations?

This requires a paradigm shift: construction of the search tree .

The so-called search tree, is to at first glance can not solve the problem found with a deep abstraction into a tree, not to say deep search is depth-first traversal of the tree and of what? I put this question into a diagram, not to solve it?

Well, we started the abstract:

A map, for each point (that is, each coordinate matrix), which has four options to go: up and down. So, we can be abstracted into a each node has four child nodes of Fig. (Of course, the boundary nodes and walls are excluded)

With this idea, you can search the deep.

Idea is as follows: input matrix, mark the starting point.

Deep search from the starting point, is determined using the direction and the array, the search through the graph can reach the point marked in the marker.

If the end point with a marker on the output is whether otherwise.

Out of the code :( a sort of a small detail: Note characters read, here is cin, not necessarily read by scanf come)

#include<cstdio>
#include<iostream>
using namespace std;
const int maxn=510;
int n,m;
char map[maxn][maxn];
bool v[maxn][maxn];
int dx[]={0,0,0,-1,1};
int dy[]={0,1,-1,0,0};
int a,b,c,d;
void dfs(int x,int y)
{
    v[x][y]=1;
    for(int i=1;i<=4;i++)
    {
        int xx=x+dx[i];
        int yy=y+dy[i];
        if(xx<1 || xx>n || yy<1 || yy>m || map[xx][yy]=='#' || v[xx][yy])
            continue;
        dfs(xx,yy);
    }
}
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
        {
            cin>>map[i][j];
            if(map[i][j]=='s')
                a=i,b=j;
            if(map[i][j]=='g')
                c=i,d=j;
        }
    dfs(a,b);
    if(v[c][d])
    {
        printf("Yes");
        return 0;
    }
    else
    {
        printf("No");
        return 0;
    }
}

Guess you like

Origin www.cnblogs.com/fusiwei/p/11700051.html