OPENJUDGE 1159:Maze
- 题目
描述
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.
输入
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.
输出
For each test case, in one line output “YES” if Acm can find the treasure, or “NO” otherwise.
样例输入
4 4
S.X.
a.X.
…XG
…
3 4
S.Xa
.aXB
b.AG
0 0
样例输出
YES
NO
- AC代码
#include<iostream>
#include<cstring>
using namespace std;
struct door
{
int need;
int have;
int x,y;
}A,B,C,D,E;
//door A,B,C,D,E;
int visit[25][25]={0};
void dfs(int ,int ,int ,int);
char map[25][25]={0};
bool flag=false;
int main()
{
int m,n,si,sj;
while(cin>>m>>n&&m!=0&&n!=0)
{
memset(visit,0,sizeof(visit));
memset(map,0,sizeof(map));
A.have=0;B.have=0;C.have=0;D.have=0;E.have=0;
A.need=0;B.need=0;C.need=0;D.need=0;E.need=0;
A.x=30;A.y=30;
B.x=30;B.y=30;
C.x=30;C.y=30;
D.x=30;D.y=30;
E.x=30;E.y=30;
flag=false;
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
{ cin>>map[i][j];
if(map[i][j]=='S')
{si=i;sj=j;}
if(map[i][j]<='e'&&map[i][j]>='a')
{
switch(map[i][j])
{
case'a':A.need++;
break;
case'b':B.need++;
break;
case'c':C.need++;
break;
case'd':D.need++;
break;
case'e':E.need++;
break;
}
}
}
dfs(si,sj,m,n);
if(flag) cout<<"YES"<<endl;
else if(!flag) cout<<"NO"<<endl;
}
return 0;
}
void dfs(int si,int sj,int m,int n)
{
if(flag==true||si<0||sj<0||si>=m||sj>=n||visit[si][sj]==-1||map[si][sj]=='X')
return ;
if(map[si][sj]<='E'&&map[si][sj]>='A')
{
switch(map[si][sj])
{
case'A': if(A.have!=A.need) { A.x=si;A.y=sj;return ;} break;
case'B': if(B.have!=B.need) { B.x=si;B.y=sj;return ;} break;
case'C': if(C.have!=C.need) { C.x=si;C.y=sj;return ;} break;
case'D': if(D.have!=D.need) { D.x=si;D.y=sj;return ;} break;
case'E': if(E.have!=E.need) { E.x=si;E.y=sj;return ;} break;
}
}
visit[si][sj]=-1;
if (map[si][sj]=='G')
{
flag=true;
return ;
}
if(map[si][sj]<='e'&&map[si][sj]>='a')
{
switch(map[si][sj])
{
case'a':A.have++; if(A.have==A.need&&A.x!=30){dfs(A.x,A.y,m,n);}break;
case'b':B.have++; if(B.have==B.need&&B.x!=30){dfs(B.x,B.y,m,n);}break;
case'c':C.have++; if(C.have==C.need&&C.x!=30){dfs(C.x,C.y,m,n);}break;
case'd':D.have++; if(D.have==D.need&&D.x!=30){dfs(D.x,D.y,m,n);}break;
case'e':E.have++; if(E.have==E.need&&E.x!=30){dfs(E.x,E.y,m,n);}break;
}
};
dfs(si,sj+1,m,n);dfs(si,sj-1,m,n);dfs(si+1,sj,m,n);dfs(si-1,sj,m,n);
}
- 显示搜索过程代码
#include<iostream>
#include<cstring>
using namespace std;
struct door
{
int need;
int have;
int x,y;
}A,B,C,D,E;
//door A,B,C,D,E;
int visit[25][25]={0};
void dfs(int ,int ,int ,int);
char map[25][25]={0};
bool flag=false;
int main()
{
int m,n,si,sj;
while(cin>>m>>n&&m!=0&&n!=0)
{
memset(visit,0,sizeof(visit));
memset(map,0,sizeof(map));
A.have=0;B.have=0;C.have=0;D.have=0;E.have=0;
A.need=0;B.need=0;C.need=0;D.need=0;E.need=0;
A.x=30;A.y=30;
B.x=30;B.y=30;
C.x=30;C.y=30;
D.x=30;D.y=30;
E.x=30;E.y=30;
flag=false;
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
{ cin>>map[i][j];
if(map[i][j]=='S')
{si=i;sj=j;}
// cout<<si<<' '<<sj<<endl;
if(map[i][j]<='e'&&map[i][j]>='a')
{
switch(map[i][j])
{
case'a':A.need++;
break;
case'b':B.need++;
break;
case'c':C.need++;
break;
case'd':D.need++;
break;
case'e':E.need++;
break;
}
}
}
dfs(si,sj,m,n);
if(flag) cout<<"YES"<<endl;
else if(!flag) cout<<"NO"<<endl;
}
return 0;
}
void dfs(int si,int sj,int m,int n)
{
if(flag==true||si<0||sj<0||si>=m||sj>=n||visit[si][sj]==-1||map[si][sj]=='X')
return ;
// cout<<"enter"<<endl;
if(map[si][sj]<='E'&&map[si][sj]>='A')
{
switch(map[si][sj])
{
case'A': if(A.have!=A.need) { cout<<"meet doorA"<<endl;A.x=si;A.y=sj;return ;} break;
case'B': if(B.have!=B.need) {cout<<"meeet doorB"<<endl; B.x=si;B.y=sj;return ;} break;
case'C': if(C.have!=C.need) { cout<<"meet doorC"<<endl;C.x=si;C.y=sj;return ;} break;
case'D': if(D.have!=D.need) {cout<<"meet doorD"<<endl; D.x=si;D.y=sj;return ;} break;
case'E': if(E.have!=E.need) { cout<<"meet doorE"<<endl;E.x=si;E.y=sj;return ;} break;
}
}
visit[si][sj]=-1;
// cout<<"visit"<<endl;
if (map[si][sj]=='G')
{ // cout<<"find it"<<endl;
flag=true;
return ;
}
if(map[si][sj]<='e'&&map[si][sj]>='a')
{
switch(map[si][sj])
{
case'a':cout<<"get a"<<endl;A.have++; if(A.have==A.need&&A.x!=30){cout<<"enter doorA"<<endl;dfs(A.x,A.y,m,n);}break;
case'b':cout<<"get b"<<endl;B.have++; if(B.have==B.need&&B.x!=30){cout<<"enter doorB"<<endl;dfs(B.x,B.y,m,n);}break;
case'c':cout<<"get c"<<endl;C.have++; if(C.have==C.need&&C.x!=30){cout<<"enter doorC"<<endl;dfs(C.x,C.y,m,n);}break;
case'd':cout<<"get d"<<endl;D.have++; if(D.have==D.need&&D.x!=30){cout<<"enter doorD"<<endl;dfs(D.x,D.y,m,n);}break;
case'e':cout<<"get e"<<endl;E.have++; if(E.have==E.need&&E.x!=30){cout<<"enter doorE"<<endl;dfs(E.x,E.y,m,n);}break;
}
};
dfs(si,sj+1,m,n);dfs(si,sj-1,m,n);dfs(si+1,sj,m,n);dfs(si-1,sj,m,n);
}