poj2157Maze(bfs,有门,需要先找钥匙)

来源:互联网 发布:淘宝海蓝之谜小样真假 编辑:程序博客网 时间:2024/05/16 02:04
Maze
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 3245 Accepted: 1022

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

4 4 S.X. a.X. ..XG .... 3 4 S.Xa .aXB b.AG 0 0

Sample Output

YES NO

Source

POJ Monthly,Wang Yijie

dfs,若找到G,标记之,循环bfs,根据跳出条件执行,钥匙增加就循环,不增加就跳出。
再根据标记,输出结果。dfs,bfs应该都行吧。

#include <iostream>#include <cstring>#include <cctype>using namespace std;bool vis[20][20];int dir[4][2]={-1,0,1,0,0,-1,0,1};char mat[25][25];int r,c,sx,sy,ex,ey,flag,ok,p;int totalKey[5], haveKey[5];void dfs(int x, int y){    if(x==ex && y==ey)  ok=1;    if(mat[x][y]=='X' || vis[x][y]) return;    if(islower(mat[x][y])) {haveKey[mat[x][y]-'a']++, mat[x][y]='.',flag++;}    if(mat[x][y]=='A' || mat[x][y]=='B' || mat[x][y]=='C' || mat[x][y]=='D' || mat[x][y]=='E'){        if(haveKey[mat[x][y]-'A']==totalKey[mat[x][y]-'A'] && haveKey[mat[x][y]-'A']!=0) mat[x][y]='.';  //&& 写成 || 找了半天错        else return;    }    vis[x][y] = true;    for(int i=0; i<4; i++){        int tx = x + dir[i][0];        int ty = y + dir[i][1];        if(tx<0 || tx>r-1 || ty<0 || ty>c-1) continue;        dfs(tx,ty);    }}int main(){    while(cin>>r>>c &&r+c){        flag=ok=0;        memset(vis,false,sizeof(vis));        memset(mat,'\0',sizeof(mat));        memset(haveKey,0,sizeof(haveKey));        memset(totalKey,0,sizeof(totalKey));        for(int i=0; i<r; i++){            for(int j=0; j<c; j++){                cin>>mat[i][j];                if(islower(mat[i][j])) totalKey[mat[i][j]-'a']++;                if(mat[i][j]=='S') sx = i, sy = j, mat[i][j]='.';                if(mat[i][j]=='G') ex = i, ey = j;                     //一开始ey写成sy,找了半天错            }        }        dfs(sx,sy);p=flag;        while(true){            memset(vis,false,sizeof(vis));            dfs(sx,sy);            if(flag==p) break;            else p=flag;        }        //        int m = 405;                       //棋盘是20*20的,所以最多搜索400次,其实肯定不可能这么多,懒得推证了//        while(m--){//            dfs(sx,sy);//            memset(vis,false,sizeof(vis));//        }        if(ok) cout<<"YES"<<endl;        else cout<<"NO"<<endl;    }    return 0;}


0 0