标签:csu dfs
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.
Source
好久没写博客了,今年是最后一年了,还是写勤快点吧,
题意:*为入口,/和\为镜子,镜子会反射,要求最后出点所在的位置
思路:只需要在遇到镜子的时候判断左转还是右转就行了
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <math.h>
#include <algorithm>
using namespace std;
#define ls 2*i
#define rs 2*i+1
#define up(i,x,y) for(i=x;i<=y;i++)
#define down(i,x,y) for(i=x;i>=y;i--)
#define mem(a,x) memset(a,x,sizeof(a))
#define w(a) while(a)
#define LL __int64
const double pi = acos(-1.0);
#define Len 100005
int n,m,cas = 1,sx,sy;
char str[25][25];
struct point
{
int x,y;
point() {}
point(int _x,int _y):x(_x),y(_y) {}
} ans;
point dfs(int x,int y,int d)
{
int tx = x,ty = y;
if(d==1) tx--;
if(d==2) tx++;
if(d==3) ty--;
if(d==4) ty++;
if(d==1)
{
w(1)
{
if(str[tx][ty]=='/') return dfs(tx,ty,4);
if(str[tx][ty]=='\\') return dfs(tx,ty,3);
if(tx==1 || str[tx][ty]=='x') return point(tx,ty);
tx--;
}
}
if(d==2)
{
while(1)
{
if(str[tx][ty]=='/') return dfs(tx,ty,3);
if(str[tx][ty]=='\\') return dfs(tx,ty,4);
if(tx==n || str[tx][ty]=='x') return point(tx,ty);
tx++;
}
}
if(d==3)
{
while(1)
{
if(str[tx][ty]=='/') return dfs(tx,ty,2);
if(str[tx][ty]=='\\') return dfs(tx,ty,1);
if(ty==1|| str[tx][ty]=='x') return point(tx,ty);
ty--;
}
}
if(d==4)
{
while(1)
{
if(str[tx][ty]=='/') return dfs(tx,ty,1);
if(str[tx][ty]=='\\') return dfs(tx,ty,2);
if(ty==m|| str[tx][ty]=='x') return point(tx,ty);
ty++;
}
}
}
int main()
{
int i,j;
w(~scanf("%d%d",&m,&n))
{
if(m+n==0)
break;
up(i,1,n)
{
scanf("%s",str[i]+1);
up(j,1,m)
{
if(str[i][j]=='*')
{
sx = i;
sy = j;
}
}
}
if(sx == 1) ans = dfs(sx,sy,2);
else if(sx == n) ans = dfs(sx,sy,1);
else if(sy == 1) ans = dfs(sx,sy,4);
else ans = dfs(sx,sy,3);
printf("HOUSE %d\n",cas++);
up(i,1,n)
{
up(j,1,m)
{
if(ans.x == i && ans.y == j)
{
printf("&");
continue;
}
printf("%c",str[i][j]);
}
puts("");
}
}
return 0;
}
CSU1567:Reverse Rot(DFS)
标签:csu dfs
原文地址:http://blog.csdn.net/libin56842/article/details/44921341