111qqz的小窝

老年咸鱼冲锋!

poj 2157 Maze (bfs)

Maze
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 3598   Accepted: 1125

Description

Acm, a treasure-explorer, is exploring again. This time he is in a special maze, in which there are some doors (at most 5 doors, represented by ‘A’, ‘B’, ‘C’, ‘D’, ‘E’ respectively). In order to find the treasure, Acm may need to open doors. However, to open a door he needs to find all the door’s keys (at least one) in the maze first. For example, if there are 3 keys of Door A, to open the door he should find all the 3 keys first (that’s three ‘a’s which denote the keys of ‘A’ in the maze). Now make a program to tell Acm whether he can find the treasure or not. Notice that Acm can only go up, down, left and right in the maze.

Input

The input consists of multiple test cases. The first line of each test case contains two integers M and N (1 < N, M < 20), which denote the size of the maze. The next M lines give the maze layout, with each line containing N characters. A character is one of the following: 'X' (a block of wall, which the explorer cannot enter), '.' (an empty block), 'S' (the start point of Acm), 'G' (the position of treasure), 'A', 'B', 'C', 'D', 'E' (the doors), 'a', 'b', 'c', 'd', 'e' (the keys of the doors). The input is terminated with two 0's. This test case should not be processed.

Output

For each test case, in one line output “YES” if Acm can find the treasure, or “NO” otherwise.

Sample Input

Sample Output

Source

POJ Monthly,Wang Yijie
 
想了好久.一开始以为和上一道状态压缩的题目相似
只有当钥匙数量达到全部以后才把钥匙加入到钥匙串.
而同一个门的所有钥匙的状态由于钥匙串的不同状态分成了不同的层,层与层之间很难转移.
然后看到讨论区中说,只有当五路可走的时候才去开门.
所以尽可能得走,如果我遇到了门,能打开就打开,打不开就再入队,不做标记.这样以后还可以访问到.
需要注意的是,对于一个打不开的门,可能反复入队造成TLE,我们可以增加一个变量记录步数,最多走m*n步.

 

说点什么

您将是第一位评论人!

提醒
wpDiscuz
粤ICP备18103363