您的位置:首页 > 运维架构

Openjudge1288 The Rotation Game(dfs)

2015-08-05 16:32 369 查看
总时间限制: 15000ms 内存限制: 150000kB

描述The rotation game uses a # shaped board, which can hold 24 pieces of square blocks (see Fig.1). The blocks are marked with symbols 1, 2 and 3, with exactly 8 pieces of each kind.



Initially, the blocks are placed on the board randomly. Your task is to move the blocks so that the eight blocks placed in the center square have the same symbol marked. There is only one type of valid move, which is to rotate one of the four lines, each consisting
of seven blocks. That is, six blocks in the line are moved towards the head by one block and the head block is moved to the end of the line. The eight possible moves are marked with capital letters A to H. Figure 1 illustrates two consecutive moves, move A
and move C from some initial configuration.

输入The input consists of no more than 30 test cases. Each test case has only one line that contains 24 numbers, which are the symbols of the blocks in the initial configuration. The rows of blocks are listed from top to bottom.
For each row the blocks are listed from left to right. The numbers are separated by spaces. For example, the first test case in the sample input corresponds to the initial configuration in Fig.1. There are no blank lines between cases. There is a line containing
a single `0' after the last test case that ends the input.

输出For each test case, you must output two lines. The first line contains all the moves needed to reach the final configuration. Each move is a letter, ranging from `A' to `H', and there should not be any spaces between the
letters in the line. If no moves are needed, output `No moves needed' instead. In the second line, you must output the symbol of the blocks in the center square after these moves. If there are several possible solutions, you must output the one that uses the
least number of moves. If there is still more than one possible solution, you must output the solution that is smallest in dictionary order for the letters of the moves. There is no need to output blank lines between cases.

样例输入
1 1 1 1 3 2 3 2 3 1 3 2 2 3 1 2 2 2 3 1 2 1 3 3
1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3
0


样例输出
AC
2
DDHH
2


类似数独的一个dfs,要把中间的一圈拼成数字一样的。

AC代码:

#include "iostream"
#include "cstdio"
#include "cstring"
#include "algorithm"
using namespace std;
int map[10][10], has[8] = {3, 5, 3, 5, 5, 3, 5, 3}, step;
char ans[100];
int get_h()
{
	int num[10] = {0};
	for(int i = 3; i <= 5; ++i) {
		num[map[3][i]]++;
		num[map[5][i]]++;
	}
	num[map[4][3]]++;
	num[map[4][5]]++;
	return max(num[1], max(num[2], num[3]));
}
void turn_col(int k, int flag)
{
	int tem;
	if(flag) {
		tem = map[1][k];
		for(int i = 2; i <= 7; ++i)
			map[i - 1][k] = map[i][k];
		map[7][k] = tem;
	}
	else {
		tem = map[7][k];
		for(int i = 7; i >= 2; --i)
			map[i][k] = map[i - 1][k];
		map[1][k] = tem;
	}
}
void turn_row(int k, int flag)
{
	int tem;
	if(!flag) {
		tem = map[k][1];
		for(int i = 2; i <= 7; ++i)
			map[k][i - 1] = map[k][i];
		map[k][7] = tem;
	}
	else {
		tem = map[k][7];
		for(int i = 7; i >= 2; --i)
			map[k][i] = map[k][i - 1];
		map[k][1] = tem;
	}
}
int dfs(int cnt)
{
	int tem = get_h();
	if(tem == 8 && cnt == step) return 1;
	if(cnt + (8 - tem) > step) return 0;
	char c;
	for(int i = 0; i < 8; ++i) {
		c = i + 'A';
		ans[cnt] = c;
		if(c == 'A' || c == 'B') {
			turn_col(has[i], 1);
			if(dfs(cnt + 1)) return 1;
			turn_col(has[i], 0);
		}
		else if(c == 'E' || c == 'F') {
			turn_col(has[i], 0);
			if(dfs(cnt + 1)) return 1;
			turn_col(has[i], 1);
		}
		else if(c == 'C' || c == 'D') {
			turn_row(has[i], 1);
			if(dfs(cnt + 1)) return 1;
			turn_row(has[i], 0);
		}
		else if(c == 'G' || c == 'H') {
			turn_row(has[i], 0);
			if(dfs(cnt + 1)) return 1;
			turn_row(has[i], 1);
		}
	}
	return 0;
}
int main(int argc, char const *argv[])
{
	int a;
	while(scanf("%d", &a) != EOF && a) {
		map[1][3] = a;
		scanf("%d", &a);
		map[1][5] = a;
		for(int i = 2; i <= 7; ++i)
			if(i == 3 || i == 5) 
				for(int j = 1; j <= 7; ++j)
					scanf("%d", &map[i][j]);
			else scanf("%d%d", &map[i][3], &map[i][5]);
		if(get_h() == 8) {
			printf("No moves needed\n%d\n",map[3][3]);
			continue;
		}
		step = 1;
		while(1) {
			if(dfs(0)) break;
			step++;
		}
		ans[step] = '\0';
		printf("%s\n%d\n", ans, map[3][3]);
	}
	return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: