Nyoj 82 maze Treasure Hunt (1)

Source: Internet
Author: User
Maze Treasure Hunt (1)Time Limit: 1000 MS | memory limit: 65535 kb difficulty: 4
Description

A treasure seeker named ACM found a treasure map, which found a maze Based on the treasure map. This is a very special maze, there are n numbered doors in the maze (n <= 5) numbered a, B, c, d, e. ACM must open the door to find the treasure. However, before opening the door, you must find all the keys required to open the door in the maze (each door has at least one key). For example: now there are three keys in door A, and ACM must find all three keys to open door. Now, please write a program to tell ACM whether it can get the treasure smoothly.

 

 
Input
The input may contain multiple groups of test data (up to 10 groups ).
The first row of each group of test data contains two integers m, n (1 <n, m <20), representing the rows and columns of the Maze respectively. The next M line contains n characters, which describes the layout of the maze. The meaning of each character is as follows:
. Indicates the path that can be taken
S: indicates the starting point of ACM
G indicates the location of the treasure
X indicates that there is a wall, and ACM cannot enter or pass through.
A, B, c, d, e indicates the door. A, B, C, D, E indicates the key on the door corresponding to uppercase letters.
Note that ACM can only move in the left and right sides of the maze.

Finally, input 0 indicates that the input is complete.
Output
Each row outputs a Yes value, indicating that ACM can find the treasure. If no value is output, 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
Source
Adapted from the poj monthly competition
Uploaded
Zhang yuncong

Solution: compression + DFS

 1 #include <cstdio> 2 #include <cstring> 3 #include <set> 4 #define LL long long 5 using namespace std; 6 char table[23][23]; 7 int rows,cols,totKey[5],hasKey[5],sx,sy; 8 set<LL>vis; 9 bool isVis(int x,int y){10     LL temp = 0;11     for(int i = 0; i < 5; i++)12         temp = (temp<<9)+hasKey[i];13     temp = (temp<<10)+(x<<5)+y;14     if(vis.count(temp)) return true;15     vis.insert(temp);16     return false;17 }18 bool dfs(int x,int y) {19     if(table[x][y] == ‘X‘) return false;20     if(table[x][y] == ‘G‘)21         return true;22     if(isVis(x,y)) return false;23     static  int dir[4][2] = {0,-1,0,1,-1,0,1,0};24     if(table[x][y] >= ‘a‘ && table[x][y] <= ‘e‘) {25         hasKey[table[x][y]-‘a‘]++;26         table[x][y] = ‘.‘;27     } else if(table[x][y] >= ‘A‘ && table[x][y] <= ‘E‘) {28         if(hasKey[table[x][y]-‘A‘] < totKey[table[x][y]-‘A‘])29             return false;30     }31     char ch = table[x][y];32     table[x][y] = ‘.‘;33     for(int i = 0; i < 4; i++)34         if(dfs(x+dir[i][0],y+dir[i][1])) return true;35     if(table[x][y] != ‘.‘) table[x][y] = ch;36     return false;37 }38 int main() {39     int i,j;40     while(scanf("%d %d",&rows,&cols),rows||cols) {41         memset(totKey,0,sizeof(totKey));42         memset(hasKey,0,sizeof(hasKey));43         memset(table,‘X‘,sizeof(table));44         for(i = 1; i <= rows; i++) {45             scanf("%s",table[i]+1);46             table[i][strlen(table[i])]=‘X‘;47             for(j = 1; j <= cols; j++) {48                 if(table[i][j] == ‘S‘) {49                     table[sx = i][sy = j] = ‘.‘;50                 } else if(table[i][j] >= ‘a‘ && table[i][j] <= ‘e‘) {51                     totKey[table[i][j]-‘a‘]++;52                 }53             }54         }55         vis.clear();56         for(i = 0; i < 5; i++)57             if(!totKey[i]) totKey[i] = 500;58         printf("%s\n",dfs(sx,sy)?"YES":"NO");59     }60     return 0;61 }
View code

 

 

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.