1562: Fun House
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 206 Solved: 72
[ Submit][ Status][ Web Board]
Description
American Carnival Makers Inc. (ACM) has a long history of designing rides and attractions. One of their more popular attractions is a fun house that includes a room of mirrors. Their trademark is to set up the room so that when looking forward from the entry door, the exit door appears to be directly ahead. However, the room has double-sided mirrors placed throughout at 45 degree angles. So, the exit door can be on any of the walls of the room. The set designer always places the entry and mirrors, but can never seem to be bothered to place the exit door. One of your jobs as part of the construction crew is to determine the placement of the exit door for the room given an original design.
The final diagram for a sample room is given below. The asterisk (*) marks the entry way, lower case x's mark the walls, the mirrors are given by the forward and backward slash characters (/ and \), open spaces with no visual obstructions are marked by periods (.), and the desired placement of the exit is marked with an ampersand (&). In the input diagram, there is an 'x' in place of the '&', since the exit has not yet been located. You need to alter the input diagram by replacing the proper 'x' with an '&' to identify the exit. Note that entrances and exits can appear on any of the walls (although never a corner), and that it is physically impossible for the exit to be the same as the entrance. (You don't need to understand why this is so, although it may be fun to think about.)
xxxxxxxxxxx
x../..\...x
x..../....x
*../......x
x.........x
xxxxxx&xxxx
Input
Each room will be preceded by two integers, W and L, where 5 ≤ W ≤ 20 is the width of the room including the border walls and 5 ≤ L ≤ 20 is the length of the room including the border walls. Following the specification of W and L are L additional lines containing the room diagram, with each line having W characters from the alphabet: { * , x , . , / , \ }. The perimeter will always be comprised of walls, except for one asterisk (*) which marks the entrance; the exit is not (yet) marked. A line with two zeros indicates the end of input data.
Output
For each test case, the first line will contain the word, HOUSE, followed by a space and then an integer that identifies the given fun house sequentially. Following that should be a room diagram which includes the proper placement of the exit door, as marked by an ampersand (&).
Sample Input
11 6
xxxxxxxxxxx
x../..\...x
x..../....x
*../......x
x.........x
xxxxxxxxxxx
5 5
xxxxx
*...x
x...x
x...x
xxxxx
5 5
xxxxx
x./\x
*./.x
x..\x
xxxxx
6 6
xxx*xx
x/...x
x....x
x/./.x
x\./.x
xxxxxx
10 10
xxxxxxxxxx
x.../\...x
x........x
x........x
x.../\..\x
*...\/../x
x........x
x........x
x...\/...x
xxxxxxxxxx
0 0
Sample Output
HOUSE 1
xxxxxxxxxxx
x../..\...x
x..../....x
*../......x
x.........x
xxxxxx&xxxx
HOUSE 2
xxxxx
*...&
x...x
x...x
xxxxx
HOUSE 3
xxxxx
x./\x
*./.x
x..\&
xxxxx
HOUSE 4
xxx*xx
x/...x
x....x
x/./.&
x\./.x
xxxxxx
HOUSE 5
xxxxxxxxxx
x.../\...x
x........x
x........x
&.../\..\x
*...\/../x
x........x
x........x
x...\/...x
xxxxxxxxxx
HINT
In both Java and C++ the backslash character (\) has special meaning as an escape character within character and string literals. You must use the combination \\ to express a single backslash within a character or string literal within source code.
题目意思是说,‘*’代表入口,‘\‘和’/'代表镜子,‘x’代表墙,从起点出发可以通过镜子的反射找到出口。题目保证入口和出口都在边界的墙上,且不可能在角落里。所以可以用递归的方法来解决这道题目。
代码如下:
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
char map[25][25];
int w,l,ei,ej;
void fun(int a,int b,int c)
{
int i,j;
if(c==1)
{
for(i=b+1;i<=w;i++)
{
if(map[a][i]=='x'){
ei=a;ej=i;map[ei][ej]='&';return;
}
if(map[a][i]=='/'){
fun(a,i,4);break;
}
if(map[a][i]=='\\'){
fun(a,i,2);break;
}
}
}
else if(c==2)
{
for(i=a+1;i<=l;i++)
{
if(map[i][b]=='x'){
ei=i;ej=b;map[ei][ej]='&';return;
}
if(map[i][b]=='/'){
fun(i,b,3);break;
}
if(map[i][b]=='\\'){
fun(i,b,1);break;
}
}
}
else if(c==3)
{
for(i=b-1;i>=1;i--)
{
if(map[a][i]=='x'){
ei=a;ej=i;map[ei][ej]='&';return;
}
if(map[a][i]=='/'){
fun(a,i,2);break;
}
if(map[a][i]=='\\'){
fun(a,i,4);break;
}
}
}
else if(c==4)
{
for(i=a-1;i>=1;i--)
{
if(map[i][b]=='x'){
ei=i;ej=b;map[ei][ej]='&';return;
}
if(map[i][b]=='/'){
fun(i,b,1);break;
}
if(map[i][b]=='\\'){
fun(i,b,3);break;
}
}
}
return ;
}
int main()
{
int i,j,si,sj,t=0;
while(scanf("%d%d",&w,&l)!=EOF)
{
if(w==0&&l==0)break;
// getchar();
t++;
for(i=1;i<=l;i++)
{
for(j=1;j<=w;j++)
{
cin>>map[i][j];
if(map[i][j]=='*'){
si=i;sj=j;
}
}
}
if(sj==1)
fun(si,sj,1);
else if(sj==w)fun(si,sj,3);
else if(si==1)fun(si,sj,2);
else if(si==l)fun(si,sj,4);
printf("HOUSE %d\n",t);
for(i=1;i<=l;i++)
{
for(j=1;j<=w;j++)
printf("%c",map[i][j]);
printf("\n");
}
}
return 0;
}
其实题目本身不难,但是我却花了大力气来解这道题目。刚开始输入是用的scanf,一直Wa。后来将输入改成了cin,居然就过了。感觉又生气又惊讶,于是不由得思考cin和scanf的区别。我发现,在一个个输入字符的时候,这两者之间的差别挺大的。cin用于输入的时候,直接无视了换行。而scanf则需要考虑换行的问题,这中间就很容易出错。但是说实话,我还是不清楚这其中 的原因。另附上用scanf的错误代码,希望以后回来看的时候能够找出错误来。
#include<stdio.h>
#include<string.h>
char map[25][25];
int w,l,ei,ej;
void fun(int a,int b,int c)
{
int i,j;
if(c==1)
{
for(i=b+1;i<=w;i++)
{
if(map[a][i]=='x'){
ei=a;ej=i;map[ei][ej]='&';return;
}
if(map[a][i]=='/'){
fun(a,i,4);break;
}
if(map[a][i]=='\\'){
fun(a,i,2);break;
}
}
}
else if(c==2)
{
for(i=a+1;i<=l;i++)
{
if(map[i][b]=='x'){
ei=i;ej=b;map[ei][ej]='&';return;
}
if(map[i][b]=='/'){
fun(i,b,3);break;
}
if(map[i][b]=='\\'){
fun(i,b,1);break;
}
}
}
else if(c==3)
{
for(i=b-1;i>=1;i--)
{
if(map[a][i]=='x'){
ei=a;ej=i;map[ei][ej]='&';return;
}
if(map[a][i]=='/'){
fun(a,i,2);break;
}
if(map[a][i]=='\\'){
fun(a,i,4);break;
}
}
}
else if(c==4)
{
for(i=a-1;i>=1;i--)
{
if(map[i][b]=='x'){
ei=i;ej=b;map[ei][ej]='&';return;
}
if(map[i][b]=='/'){
fun(i,b,1);break;
}
if(map[i][b]=='\\'){
fun(i,b,3);break;
}
}
}
return ;
}
int main()
{
int i,j,si,sj,t=0;
while(scanf("%d%d",&w,&l)!=EOF)
{
if(w==0&&l==0)break;
getchar();
t++;
for(i=1;i<=l;i++)
{
for(j=1;j<=w;j++)
{
scanf("%c",&map[i][j]);
if(map[i][j]=='*'){
si=i;sj=j;
}
}
getchar();
}
if(sj==1)
fun(si,sj,1);
else if(sj==w)fun(si,sj,3);
else if(si==1)fun(si,sj,2);
else if(si==l)fun(si,sj,4);
printf("HOUSE %d\n",t);
for(i=1;i<=l;i++)
{
for(j=1;j<=w;j++)
printf("%c",map[i][j]);
printf("\n");
}
}
return 0;
}
暂时---就先这样吧。。。T^T