Wikioi 1215 maze

Source: Internet
Author: User

 

 

1215 maze 6 people are recommended to add questions to favorites

 

Description Description

In the N * n maze, "#" is the wall, "." is the path, "S" is the start point, and "e" is the end point. A total of four directions can be taken. Go to the (n-1, n-1) "E") position in the upper-left corner (0, 0) "S"). You can use the general rule to output yes. If not, no is output.

Input description Input description

The first input behavior is an integer m, indicating the number of maze. Then, the first behavior of each maze data is an integer n (n ≤ 16), which indicates the length of the sides of the maze. The next n rows contain N characters in each line and are separated by spaces.

Output description Output description

The output contains m rows. If the maze corresponding to each row can go, yes is output; otherwise, no is output.

Sample Input Sample Input
1  7  s...##.  .#.....  .......  ..#....  ..#...#  ###...#  ......e
Sample output Sample output
YES

 

Simply put, this question is simply explained by the DFS and then expanded to the surrounding area.

#include<stdio.h>#include<math.h>char a[1001][1001],ans=0;int directx[5]={0,0,0,-1,1},vis[1001][1001];int directy[5]={0,-1,1,0,0};void dfs(int i,int j,int n){    if(i<=0||i>n||j<=0||j>n)return ;    if(vis[i][j]==1||a[i][j]==‘#‘)return ;    if(a[i][j]==‘e‘){ans=a[i][j];    printf("YES");        }    vis[i][j]=1;    for(int x=1;x<=4;x++)    dfs(i+directx[x],j+directy[x],n) ;}int main(){    int b,i,z,o,j,n;    scanf("%d",&z);   for(o=1;o<=z;o++)   {       scanf("%d\n",&n);       for(i=1;i<=n;i++)       for(j=1;j<=n;j++)       {           if(j<n)           scanf("%c",&a[i][j]);           else scanf("%c\n",&a[i][j]);       }       dfs(1,1,n);    if(ans==0)printf("NO");       printf("\n");   }      return 0;} 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.