Maze Treasure Hunt (i) (DFS)

Source: Internet
Author: User

Maze Treasure Hunt (a) time limit:MS | Memory limit:65535 KB Difficulty:4
Describe

A treasure-seeker named ACM found a treasure map, it found a maze according to the treasure map, this is a very special maze, there are N numbered in the maze of the door (n<=5), they were numbered a,b,c,d, E. In order to find the treasure, ACM must open the door, but before opening the door must find all the keys needed to open the door (at least one key for each door), for example: Now that a door has three keys, ACM must find all three keys to open a door. Now ask you to write a program to tell ACM that he can get the treasure well.

Input
The
input may have multiple sets of test data (no more than 10 groups).
The first line of each set of test data contains two integers m,n (1<n,m<20), representing the rows and columns of the maze, respectively. The next m has n characters per line, describing the layout of the maze. Each of these characters has the following meanings:
Indicates a way to go.
S: Represents the starting point of ACM
G indicates the location of the treasure
X means there is a wall here, ACM cannot enter or pass through.
A,b,c,d,e says this is the door, a,b,c,d,e the key on the door that corresponds to the capital letter.
Note that ACM can only move up or down in the maze in four directions.

Finally, enter 0 0 to indicate the end of the input.
Output
each line outputs a yes to indicate that ACM can find the treasure, and the output no means that ACM cannot find the treasure.
Sample input
4 4 s.x. a.x ... XG .... 3 4 s.xa. AXB B.ag 0 0
Sample output
YES NO
The following:
Num records the number of keys; God search, search the key to add, encounter the door to judge, with a retrospective, but do not know right, still in the sentence;
Code:
1#include <stdio.h>2#include <string.h>3 intans;4 intdisx[4]={0,0,1,-1};5 intdisy[4]={1,-1,0,0};6 Charmap[ -][ -];7 intvis[ -][ -];8 intn,m;9 intNx,ny;Ten intkey[5],num[5]; One voidDfsintXinty) { A     if(map[x][y]=='G'){ -ans=1; -        //puts ("--------"); the         return; -     } -    //puts ("..."); -vis[x][y]=1; +      for(intI=0;i<4; i++){ -nx=x+disx[i];ny=y+Disy[i]; +         if(nx<0|| ny<0|| nx>=n| | ny>=m| | map[nx][ny]=='X'|| Vis[nx][ny])Continue; A         if(map[nx][ny]=='.'|| map[nx][ny]=='G'){ at DFS (nx,ny); -vis[nx][ny]=0; -         } -         Else{ -             if(map[nx][ny]>='a'&&map[nx][ny]<='e'){ -key[map[nx][ny]-'a']++; in DFS (nx,ny); -key[map[nx][ny]-'a']--; tovis[nx][ny]=0; +             } -             Else{ the                 if(key[map[nx][ny]-'A']==num[map[nx][ny]-'a']){ * DFS (nx,ny); $vis[nx][ny]=0;Panax Notoginseng                 } -                 Else Continue; the             } +         } A     } the     return ; + } - intMain () { $     intSx,sy; $      while(~SCANF ("%d%d", &n,&m), n|M) { -memset (Key,0,sizeof(key)); -memset (Vis,0,sizeof(Vis)); thememset (NUM,0,sizeof(num)); -ans=0;Wuyi          for(intI=0; i<n;i++) scanf ("%s", Map[i]); the          for(intI=0; i<n;i++) for(intj=0; j<m;j++){ -             if(map[i][j]=='S') sx=i,sy=J; Wu             if(map[i][j]>='a'&&map[i][j]<='e') num[map[i][j]-'a']++; -         } About         //puts ("***********"); $map[sx][sy]='X'; - DFS (sx,sy); -          //puts ("++++"); -         if(ANS) puts ("YES"); A         ElsePuts"NO"); +     } the     return 0; -}

Maze Treasure Hunt (i) (DFS)

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.