ZOJ-1208

41 篇文章 0 订阅

日他大爷的。。这道题PE了我半个多小时,无语死了,本来这题输出的格式就很恶心,各种空格对齐神马的,在各个地方对着标程改,还是各种不对,尼玛发现程序最后多了个换行,我吐血啊!!!!!!!!!!

#include<cstdio>
#include<map>
#include<set>
#include<deque>

using namespace std;

namespace
{
	map<char, int*> M;
	set<int> S;

	int number(int *d)
	{
		int res = 0;
		for (int i = 1; i <= 6; i++)
			res = res * 10 + d[i];
		return res;
	}

	int move(char c, int num)
	{
		int *t = M[c], d[7], temp[7];
		for (int i = 6; i >= 1; i--)
		{
			d[i] = num % 10;
			num /= 10;
		}

		for (int i = 1; i <= 6; i++)
			temp[i] = d[t[i]];
		for (int i = 1; i <= 6; i++)
			d[i] = temp[i];
		return number(d);
	}

	void bfs()
	{
		deque<int> Q;
		Q.push_back(123456);
		S.insert(123456);
		char ch[] = { 'W', 'E', 'N', 'S' };
		while (!Q.empty())
		{
			int now = Q.front();
			Q.pop_front();
			for (int i = 0; i < 4; i++)
			{
				int num = move(ch[i], now);
				if (S.find(num) == S.end())
				{
					S.insert(num);
					Q.push_back(num);
				}
			}
		}
	}
}

int main()
{
	int a[] = { 0, 3, 2, 6, 1, 5, 4 };
	int b[] = { 0, 4, 2, 1, 6, 5, 3 };
	int c[] = { 0, 2, 6, 3, 4, 1, 5 };
	int d[] = { 0, 5, 1, 3, 4, 6, 2 };
	M['W'] = a;
	M['E'] = b;
	M['N'] = c;
	M['S'] = d;

	bfs();
	puts("Problem 2 by team x");
	char s[100];
	while (gets(s) != NULL)
	{
		puts("");
		int first = s[0] - '0';
		int second = s[2] - '0';
		int begin = -1;
		for (set<int>::iterator it = S.begin(); it != S.end(); it++)
			if (*it / 100000 == first && (*it / 10000) % 10 == second)
				begin = *it;
		if (begin == -1)
		{
			printf("Invalid initial orientation:     top =%2d  front =%2d\n",
					first, second);
			continue;
		}
		int x = 0, y = 0, now = begin;
		for (int i = 4; s[i] != '\0'; i++)
			if (s[i] != ' ')
			{
				if (s[i] == 'W')
					x--;
				else if (s[i] == 'E')
					x++;
				else if (s[i] == 'N')
					y++;
				else
					y--;
				now = move(s[i], now);
			}
		printf("Initial orientation:             top =%2d  front =%2d\n", first,
				second);
		printf("Moves:%s\n", &s[3]);
		printf(
				"Final orientation and position:  top =%2d  front =%2d  x =%4d  y =%4d\n",
				now / 100000, (now / 10000) % 10, x, y);
	}
	puts("End of problem 2 by team x");
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值