Benelux Algorithm Programming Contest 2014 Final J: Jury Jeopardy

J: Jury 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.

The problem is about a maze that has exactly one entrance/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 direction 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 entrance/exit square until it finally comes back to it. The movements are described by a single letter: 'F' means forward, '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 addition, 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 Format

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 Format

On the first line one positive number: the number of test cases, at most 100100. After that per test case:

  • one line with two space-separated integers hh and ww (3 \le h, w \le 100)(3h,w100): the height and width of the maze, respectively.
  • hh lines, each with ww 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.

样例输入

3
FFRBLF
FFRFRBRFBFRBRFLF
FRLFFFLBRFFFRFFFRFRFBRFLBRFRLFLFFR

样例输出

3
4 4
####
...#
##.#
####
7 5
#####
...##
##.##
#...#
##.##
##.##
#####
7 7
#######
#...#.#
#.#...#
#.#.###
..###.#
#.....#
#######

题目来源

Benelux Algorithm Programming Contest 2014 Final

代码:

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
char map[500][500];
string str;
int X[4] = {0,1,0,-1};
int Y[4] = {1,0,-1,0};
string d = "FRBL";
int getDirction(char c, int d){
	switch(c){
		case 'F':return 0 + d;
		case 'R':return 1 + d;
		case 'B':return 2 + d;
		case 'L':return 3 + d;	
	}
}
void init(){
	memset(map,'#',sizeof(map));
}
int main(int argc, char const *argv[]){
	int t; 
	cin >> t;
	cout << t << endl;
	while(t--){
		init();
		cin >> str;
		int x = 250, y = -1, dirction = 0;
		int minx = 250, maxx = 250;
		int maxy = 0; 
		map[x][y] = '.';
		for(int i = 0; i < str.length(); i ++){
			x += X[dirction];
			y += Y[dirction];
			maxy = max(y, maxy);
			maxx = max(x, maxx);
			minx = min(x, minx);
			dirction = getDirction(str[i], dirction);
			dirction %= 4;
			map[x][y] = '.'; 
		}	

		cout << maxx - minx + 3 << " " << maxy + 2 <<endl;
		for (int i = minx - 1; i <= maxx + 1; ++i){
			for (int j = 0; j <= maxy + 1; j ++ )
				cout << map[i][j];
			cout << endl;
		}
	}
	return 0;
}


猜你喜欢

转载自blog.csdn.net/qq_36561697/article/details/81021644
今日推荐