POJ 2157 Maze

来源:互联网 发布:plc模拟仿真软件 编辑:程序博客网 时间:2024/06/05 15:12

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

POJ
OpenJudge
题目大意:一个最多20*20的迷宫,’X’代表墙,’.’代表路,’A’,’B’,’C’,’D’,’E’为门,必须收集到全部的’a’,’b’,’c’,’d’,’e’, 才能打开对应的门,S是起点,G是终点,问:起点能否走到终点?
这题是NOI OJ 搜索的第一题,但细节比较多,况且这个搜索是可以走回头路的,这样就不能用vis数组避免无限递归,但无限递归是一定要避免的,所以我们就不能用回朔法回头。
这里提出一种思路(其实大部分人都是这么做的):
1.从入口dfs/bfs一次,找到钥匙等等。
2.如果到了出口,flag=1;
3.如果上次搜索找到了钥匙,就再重复一次1.
4.直到怎么搜都找不到钥匙了:
if(flag) cout<<”YES\n”;
else cout<<”NO\n”;
就这样搜就行了,不过细节较多,要仔细,实在找不出错就重写,搜索题重写比找错有用。
AC代码如下:

#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>using namespace std;int w,h,key[5],locx[5],locy[5],S[2],vis[21][21],flag,r[5],    l[4]={1,-1,0,0},    s[4]={0,0,1,-1};char m[21][21],c;bool check(){    for(int i=0;i<=4;i++)        if(key[i]!=r[i])            return 1;    return 0;}void dfs(int x,int y){    if(m[x][y]=='G'){   flag=1;return; }    if(x<=0||x>h||y<=0||y>w||m[x][y]=='X'||(m[x][y]<='E'&&m[x][y]>='A')) return;    if(m[x][y]>='a'&&m[x][y]<='e'){        key[m[x][y]-'a']--;        if(!key[m[x][y]-'a'])   m[locx[m[x][y]-'a']][locy[m[x][y]-'a']]='.';        m[x][y]='.';    }    for(int i=0;i<=3;i++){        if(vis[x+l[i]][y+s[i]]) continue;        else vis[x+l[i]][y+s[i]]=1;        dfs(x+l[i],y+s[i]);        if(flag) return;    }}void solve(){    for(int i=1;i<=h;i++){        scanf("\n");        for(int j=1;j<=w;j++){            scanf("%c",&c);            m[i][j]=c;            if(c>='A'&&c<='E') locx[c-'A']=i,locy[c-'A']=j;            if(c>='a'&&c<='e') key[c-'a']++;            if(c=='S') S[0]=i,S[1]=j;        }    }    for(int i=0;i<=4;i++)        if(!key[i])            m[locx[i]][locy[i]]='.';    do{        memcpy(r,key,sizeof r);        memset(vis,0,sizeof vis);        dfs(S[0],S[1]);    }while(check());    if(flag)        printf("YES\n"),flag=0;    else        printf("NO\n");}int main(){    while(~scanf("%d%d",&h,&w)){        if(h==0) break;        solve();        memset(vis,0,sizeof vis);        memset(key,0,sizeof key);        memset(r,0,sizeof r);    }}

这里说一下我被卡的一个细节:
请各位想一个问题,0个钥匙的门该不该被直接打开?
如果该,你就会WA得着妈妈
如果不该,想必你就是和我一样偶然发现OJ之坑的聪明人。
如果你没想过,那我就呵呵了

0 0
原创粉丝点击