code.jam - Always Turn Left

这个题目要根据路径来构建迷宫,相对比较难,要注意很多细节,我花了好半天改了又改又调试半天才搞定,还真是不容易,总结下来就是,分析问题之后即便有了思路也不要立刻动手写程序,一定要把思路理的非常清晰之后再开始写,磨刀不误砍柴功,这样其实是节省了时间。 Always Turn Left

Problem

You find yourself standing outside of a perfect maze. A maze is defined as "perfect" if it meets the following conditions:

  1. It is a rectangular grid of rooms, R rows by C columns.
  2. There are exactly two openings on the outside of the maze: the entrance and the exit. The entrance is always on the north wall, while the exit could be on any wall.
  3. There is exactly one path between any two rooms in the maze (that is, exactly one path that does not involve backtracking).

You decide to solve the perfect maze using the "always turn left" algorithm, which states that you take the leftmost fork at every opportunity. If you hit a dead end, you turn right twice (180 degrees clockwise) and continue. (If you were to stick out your left arm and touch the wall while following this algorithm, you'd solve the maze without ever breaking contact with the wall.) Once you finish the maze, you decide to go the extra step and solve it again (still always turning left), but starting at the exit and finishing at the entrance.

The path you take through the maze can be described with three characters: 'W' means to walk forward into the next room, 'L' means to turn left (or counterclockwise) 90 degrees, and 'R' means to turn right (or clockwise) 90 degrees. You begin outside the maze, immediately adjacent to the entrance, facing the maze. You finish when you have stepped outside the maze through the exit. For example, if the entrance is on the north and the exit is on the west, your path through the following maze would be WRWWLWWLWWLWLWRRWRWWWRWWRWLW:

If the entrance and exit were reversed such that you began outside the west wall and finished out the north wall, your path would be WWRRWLWLWWLWWLWWRWWRWWLW. Given your two paths through the maze (entrance to exit and exit to entrance), your code should return a description of the maze.

Input

The first line of input gives the number of cases, N. N test cases follow. Each case is a line formatted as

entrance_to_exit exit_to_entrance

All paths will be at least two characters long, consist only of the characters 'W', 'L', and 'R', and begin and end with 'W'.

Output

For each test case, output one line containing "Case #x:" by itself. The next R lines give a description of the R by C maze. There should be C characters in each line, representing which directions it is possible to walk from that room. Refer to the following legend:

Character   Can walk north?   Can walk south?   Can walk west?   Can walk east?  
1YesNoNoNo
2NoYesNoNo
3YesYesNoNo
4NoNoYesNo
5YesNoYesNo
6NoYesYesNo
7YesYesYesNo
8NoNoNoYes
9YesNoNoYes
aNoYesNoYes
bYesYesNoYes
cNoNoYesYes
dYesNoYesYes
eNoYesYesYes
fYesYesYesYes

Limits

1 ≤ N ≤ 100.

Small dataset

2 ≤ len(entrance_to_exit) ≤ 100, 2 ≤ len(exit_to_entrance) ≤ 100.

Large dataset

2 ≤ len(entrance_to_exit) ≤ 10000, 2 ≤ len(exit_to_entrance) ≤ 10000.

Sample

Input
2 WRWWLWWLWWLWLWRRWRWWWRWWRWLW WWRRWLWLWWLWWLWWRWWRWWLW WW WW
Output
Case #1: ac5 386 9c7 e43 9c5 Case #2: 3
// leekun 2008.06.30

#include <iostream>

#include <string>

#include <fstream> 



using namespace std;



#define nouth 0

#define west  1

#define south 2

#define east  3

#define turnleft(dir) ((dir+1)%4)

#define turnright(dir) ((dir+3)%4)



ofstream outfile("B_large.out");



class grid

{

public:

	grid(int x = 0, int y = 0) : X(x), Y (y), next(0) {

		bwall[0] = 0; bwall[1] = 0; bwall[2] = 0; bwall[3] = 0;}

	bool bwall[4];

	grid * next;

	// int Direct;

	int X, Y;

};





string inttostring_bytarget(int num, string target)

{

    string ret = "";

    int B = target.length();

    if (num==0) return "0";

    while (num>0)

    {

        ret = target[num%B] + ret;

        num /= B;

    }

    return ret;

}



void drawmap(grid * map, int row, int columns)

{

    int i = 0;

    int maplen = row * columns;

    while (i < maplen)

    {

		if ((i%columns==0)&&(i>0))

        {

			cout<<endl; 

			outfile<<endl;

        }

        bool up = (i >= columns )? map[i-columns].bwall[south]: 0;

        bool down = (i+columns < row*columns)? map[i+columns].bwall[nouth]:0;

        bool left = (i%columns > 0)? map[i-1].bwall[east]: 0;

        bool right = (i%columns < columns-1)? map[i+1].bwall[west]: 0;



        int V = 

        (map[i].bwall[nouth] || up) + 

        (map[i].bwall[south] || down) * 2 + 

        (map[i].bwall[west] || left) * 4 +

        (map[i].bwall[east] || right) * 8;

        V = 15 - V;



        string S =  inttostring_bytarget(V, "0123456789abcdef");

        cout << S;

		outfile << S;

        i++;

    }

    cout << endl;

	outfile << endl;

}



int main(int argc, char *argv[])

{

    ifstream infile("B-large.in"); 



    int N = 0;

    infile >> N;

	int I = 1;



    while (N-- > 0)

    {

        string entrance_to_exit, exit_to_entrance;

        infile >> entrance_to_exit >> exit_to_entrance;



		// process the entrance_to_exit

		int p = 1;

		int q = 1;

		int Lenp = entrance_to_exit.length();

		int Lenq = exit_to_entrance.length();

		grid entrance(0, 0);

		grid *node = &entrance;

		int max_eage[4] = {0};

		int curdir = south;

		int pX = 0;

		int pY = 0;



		bool setexit_lock = false;

		int exitX  = 0, exitY = 0 , exitDir= curdir;



		while (1)

		{

			char C;

			if (p < Lenp-1) // loop 1

			{

				C = entrance_to_exit[p];

				p++;

			}

			else if (q < Lenq-1) // loop 2

			{

				// 只记录一次出口位置

				if (!setexit_lock)

				{

					exitX = pX;

					exitY = pY;

					exitDir = curdir;

					setexit_lock = true;

					curdir = turnright(turnright(curdir));

				}

				C = exit_to_entrance[q];

				q++;

			}

			else

				break;



			// 向前走只改变当前坐标

			if ('W' == C)

			{

				switch(curdir){

				case nouth: pY--;

					break;

				case south: pY++; 

					if(pY > max_eage[south]) 

						max_eage[south] = pY;

					break;

				case west: pX--; 

					if(pX < max_eage[west]) 

						max_eage[west] = pX;

					break;

				case east: pX++; 

					if(pX > max_eage[east]) 

						max_eage[east] = pX;

					break;

				}

				grid *newnode = new  grid(pX, pY);

				node->next = newnode;

				// node->Direct = curdir;

				node = newnode;

			}

			// 向左转只是说明左边有路走,仅此而已

			else if('L' == C) 

			{

				curdir = turnleft(curdir);

			}

			// 向右转说明当前前方和左方都是墙

			else if ('R' == C)

			{

				node->bwall[curdir] = 1;

				node->bwall[turnleft(curdir)] = 1;

				curdir = turnright(curdir);

			}

		}



		// creat a map

		int rows = max_eage[south] - max_eage[nouth] + 1;

		int columns = max_eage[east] - max_eage[west] + 1;

		// 坐标转换差值

		int dx = 0 - max_eage[west];



		grid * map = new grid[rows * columns];

		grid * nextnode = &entrance;

		while (nextnode)

		{

			int position = nextnode->X + dx + nextnode->Y * columns;

			for (int i = 0; i<4; i++)

			{

				map[position].bwall[i] = map[position].bwall[i] || nextnode->bwall[i]; 

			}

			grid * t = nextnode;

			nextnode = nextnode->next;

			if (t != &entrance) delete t;

		}



		// 写map边界

		int k;

		for (k = 0; k < rows; k++)

		{

			map[k * columns].bwall[west] = 1;

			map[(k+1) * columns - 1].bwall[east]  =1;

		}

		for (k = 0; k < columns; k++)

		{

			map[k].bwall[nouth] = 1;

			map[rows * columns - 1 - k].bwall[south]  =1;

		}

		// 入口出口

		map[entrance.X + dx].bwall[nouth] = 0;

		map[exitX + dx + exitY*columns].bwall[exitDir] = 0;



		// drawmap

		cout << "Case #" << I << ": " << endl;

		outfile << "Case #" << I << ": " << endl;

		I++;

		drawmap(map, rows, columns);

		delete []map;

	}



	return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值