ury Jeopardy
What would a programming contest be without a problem featuring an ASCII-maze? Do not
despair: one of the judges has designed such a problem.
source: xkcd.com/246
The problem is about a maze that has exactly one en-
trance/exit, contains no cycles and has no empty space
that is completely enclosed by walls. A robot is sent in
to explore the entire maze. The robot always faces the di-
rection it travels in. At every step, the robot will try to turn
right. If there is a wall there, it will attempt to go forward
instead. If that is not possible, it will try to turn left. If all
three directions are unavailable, it will turn back.
The challenge for the contestants is to write a program
that describes the path of the robot, starting from the en-
trance/exit square until it finally comes back to it. The
movements are described by a single letter: ‘
F
’ means for-
ward, ‘
L
’ is left, ‘
R
’ is right and ‘
B
’ stands for backward.
Each of ‘
L
’, ‘
R
’ and ‘
B
’ does not only describe the change in orientation of the robot, but also
the advancement of one square in that direction. The robot’s initial direction is East. In addi-
tion, the path of the robot always ends at the entrance/exit square.
The judge responsible for the problem had completed all the samples and testdata, when
disaster struck: the input file got deleted and there is no way to recover it! Fortunately the
output and the samples are still there. Can you reconstruct the input from the output? For
your convenience, he has manually added the number of test cases to both the sample output
and the testdata output.
Input
On the first line one positive number: the number of test cases. After that per test case:
•
one line with a single string: the movements of the robot through the maze.
Output
On the first line one positive number: the number of test cases, at most 100. After that per test
case:
•
one line with two space-separated integers
h
and
w
(
3
≤
h, w
≤
100
): the height and
width of the maze, respectively.
•
h
lines, each with
w
characters, describing the maze: a ‘
#
’ indicates a wall and a ‘
.
’
represents an empty square.
The entire contour of the maze consists of walls, with the exception of one square on the left:
this is the entrance. The maze contains no cycles (i.e. paths that would lead the robot back to
a square it had left in another direction) and no empty squares that cannot be reached from
the entrance. Every row or column – with the exception of the top row, bottom row and right
column – contains at least one empty square.
这个题就相当于给出一个dfs序列让画出原图(起点也是重点),#是不可通过,.可通过
思路就是直接按照他给的序列把可通过得点全标记出来,再根据可通过点的规格还原图,
因为不知道起点在哪,可能在最上端也可能在下端,也可能在中间,所以我开了2倍大小的数组,让他每次都从中间开始画图,这样就不至于会数组越界了.
Sample in- and output
Input
Output
3
FFRBLF
FFRFRBRFBFRBRFLF
FRLFFFLBRFFFRFFFRFRFBRFLBRFRLFLFFR
3
4 4
####
...#
##.#
####
7 5
#####
...##
##.##
#...#
##.##
##.##
#####
7 7
#######
#...#.#
#.#...#
#.#.###
..###.#
#.....#
#######
这个题就相当于给出一个dfs序列让画出原图(起点也是重点),#是不可通过,.可通过
思路就是直接按照他给的序列把可通过得点全标记出来,再根据可通过点的规格还原图,
因为不知道起点在哪,可能在最上端也可能在下端,也可能在中间,所以我开了2倍大小的数组,让他每次都从中间开始画图,这样就不至于会数组越界了.
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
#include <stack>
#include <map>
#define inf 0x3f3f3f3f
using namespace std;
int v[220][220];
char s[110000];
int main()
{
ios::sync_with_stdio(false);
int w, h;
int mmin, mmax;
int maxx;
int n, i, j;
int nx, ny, x, y;
int t;
cin>>t;
cout<<t<<endl;
while(t--)
{
cin>>s;
nx = 0;
ny = 1;
n = strlen(s);
mmin = inf;
mmax = -inf;
maxx = 0;
memset(v,0,sizeof(v));
x = 100;
y = 0;
v[x][y] = 1;
for(i = 0;i < n;i++)
{
if(s[i] == 'F')
{
x += nx;
y += ny;
v[x][y] = 1;
}
else if(s[i] == 'L')
{
if(nx == 0 && ny == 1)
{
nx = -1;
ny = 0;
}
else if(nx == 0 && ny == -1)
{
nx = 1;
ny = 0;
}
else if(nx == 1 && ny == 0)
{
nx = 0;
ny = 1;
}
else
{
nx = 0;
ny = -1;
}
x += nx;
y += ny;
v[x][y] = 1;
}
else if(s[i] == 'R')
{
if(nx == 0 && ny == 1)
{
nx = 1;
ny = 0;
}
else if(nx == 0 && ny == -1)
{
nx = -1;
ny = 0;
}
else if(nx == 1 && ny == 0)
{
nx = 0;
ny = -1;
}
else
{
nx = 0;
ny = 1;
}
x += nx;
y += ny;
v[x][y] = 1;
}
else
{
nx = -nx;
ny = -ny;
x += nx;
y += ny;
v[x][y] = 1;
}
if(x < mmin)
mmin = x;
if(x > mmax)
mmax = x;
maxx = max(maxx,y);
}
w = maxx + 2;
h = mmax - mmin + 3;
cout<<h<<' '<<w<<endl;
for(i = mmin - 1;i <= mmax+1;i++)
{
for(j = 0;j < w;j++)
{
if(v[i][j] == 1)
cout<<'.';
else
cout<<'#';
}
cout<<endl;
}
}
return 0;
}