UVa 1343 The Rotation Game

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.

\epsfbox{p3265.eps}

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.

Input 

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.

Output 

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.

Sample Input 

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

Sample Output 

AC
2
DDHH
2
 
 
#include <cstdio>
#include <set>
#include <queue>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;


// 存放已搜索状态的集合
set<vector<int> > my_set;

// 状态节点
typedef struct s_node
{
	int block[24];
	char path[100];	// 存储路径中的各个操作
	int path_length;	// 存储路径长度
}s_node;

// 存放待搜索状态的队列
queue<s_node*> my_queue;

// 存放最少的步数和数字
char best_path[100];
int best_length = -1;
int best_sym;

// 存放ABCDEDGH,中心的各个下标
int transfer[8][7] = {{0,2,6,11,15,20,22},{1,3,8,12,17,21,23},
			{10,9,8,7,6,5,4},{19,18,17,16,15,14,13},
			{23,21,17,12,8,3,1},{22,20,15,11,6,2,0},
			{13,14,15,16,17,18,19},{4,5,6,7,8,9,10}};
/*
int B[7] = {1,3,8,12,17,21,23};
int C[7] = {10,9,8,7,6,5,4};
int D[7] = {19,18,17,16,15,14,13};
int E[7] = {23,21,17,12,8,3,1};
int F[7] = {22,20,15,11,6,2,0};
int G[7] = {13,14,15,16,17,18,19};
int H[7] = {4,5,6,7,8,9,10};
*/
int center[8] = {6,7,8,11,12,15,16,17};

//bool dfs_search(int now_d, int max_d, s_node* p);

bool dfs_search(int now_d, int max_d);

int block[25];

int main()
{
	int n;
	while(scanf("%d", &n) && n != 0)
	{
/*		s_node* p = (s_node*)malloc(sizeof(s_node));
		memset(p, 0, sizeof(p));
		p->path_length = 0;
		p->block[0] = n;
		vector<int> now_block;
		now_block.push_back(n);
		for(int i = 1; i <= 23; i++)
		{
			scanf("%d", &p->block[i]);
			now_block.push_back(p->block[i]);
		}
		// 将初始节点加入队列
		my_queue = queue<s_node*>();
		my_set = set<vector<int> >();
		best_length = -1;
*/	
		block[0] = n;
		for(int i = 1; i <= 23; i++)
			scanf("%d", &block[i]);
/*		my_queue.push(p);
		my_set.insert(now_block);
*/
		int max_depth = 28;
		int same_flag = 1;
//        	int ch = p->block[center[0]];
		int ch = block[center[0]];	
        	for(int i = 1; i < 8; i++)
        	{
   	//           	if(ch != p->block[center[i]])
          		if(ch != block[center[i]])
		      	{
                        	same_flag = 0;
                        	break;
                	}
        	}
		if(same_flag)
		{
			printf("No moves needed\n%d\n", ch);
			continue;
		}
		for(int i = 1; i <= max_depth; i++)
		{
/*			my_set = set<vector<int> >();
			my_set.insert(now_block);
*/			best_sym = -1;

			if(dfs_search(0, i))
			{
				/*
				for(int j = 0; j < i; j++)
					printf("%c", best_path[j]);
				printf("\n%d\n", best_sym);	
			*/	break;
			}	
		}		
/*
		// 广度优先搜索状态
		while(my_queue.size() > 0)
		{
			s_node* p = my_queue.front();
			my_queue.pop();

//			printf("here\n");
			// 判断步数是否超过最佳步数
			if(best_length != -1 && p->path_length > best_length)
				break;
			
			// 判断是否符合条件
			int i;
			int ch = p->block[center[0]];
			for(i = 1; i < 8; i++)
			{
				if(ch != p->block[center[i]])
					break;
			}	
			// 如果是符合条件节点,可能的话更新解
			if(i == 8)
			{
				if(best_length == -1)
				{
					best_length = p->path_length;
					best_sym = ch;
					if(p->path_length == 0)
						break;
					for(int j = 0; j < p->path_length; j++)
						best_path[j] = p->path[j];
				}
				else
				{
					if(ch < best_sym)
					{
						best_sym = ch;
						for(int j = 0; j < p->path_length; j++)
                                                	best_path[j] = p->path[j];
					}
				}
				if(best_sym == 1)
					break;
			}
			
			// 如果可能,进行8个方向的变换
			for(int i = 0; i < 8; i++)
			{
//				printf("trans: %d\n", i);
				vector<int> next_block;
				for(int j = 0; j < 24; j++)
				{
					next_block.push_back(p->block[j]);
				}

				int tmp = p->block[transfer[i][0]];
				for(int j = 0; j < 6; j++)
					next_block[transfer[i][j]] = next_block[transfer[i][j+1]];
				next_block[transfer[i][6]] = tmp;

				if(my_set.find(next_block) == my_set.end())
				{
					my_set.insert(next_block);
					s_node* q = (s_node*)malloc(sizeof(s_node));
					for(int j = 0; j < 24; j++)
						q->block[j] = next_block[j];
					for(int j = 0; j < p->path_length; j++)
						q->path[j] = p->path[j];
					q->path[p->path_length] = i + 'A';
					q->path_length = p->path_length + 1;
					my_queue.push(q);		
				}				
			}
//			printf("then\n");
		}

		if(best_length == 0)
			printf("No moves needed\n");
		else
		{
			for(int i = 0; i < best_length; i++)
				printf("%c", best_path[i]);
			printf("\n");
		}		
		printf("%d\n", best_sym);				
*/	}
	return 0;	
}


//bool dfs_search(int now_d, int max_d, s_node* p)
bool dfs_search(int now_d, int max_d)
{
	int same_flag = 1;
//	int ch = p->block[center[0]];
	int ch = block[center[0]];
	for(int i = 1; i < 8; i++)
	{
//		if(ch != p->block[center[i]])
		if(ch != block[center[i]])
		{
			same_flag = 0;
			break;
		}
	}
	if(now_d == max_d)
	{
		if(same_flag == 0)
			return false;
		else
		{
			/*if(best_sym == -1)
			{
				best_sym = ch;
				
				for(int i = 0; i < p->path_length; i++)
					best_path[i] = p->path[i];	
			}
			else if(best_sym > ch)
			{
				best_sym = ch;
				for(int i = 0; i < p->path_length; i++)
                                        best_path[i] = p->path[i];
			}*/
			for(int i = 0; i < now_d; i++)
				printf("%c", best_path[i]);
			printf("\n%d\n", ch);
			return true;	
		}	
	}
	int diff_count = 0;
/*	if(block[6] != block[7] || block[6] != block[8] || block[7] != block[8])
		diff_count++;
	if(block[8] != block[12] || block[12] != block[17] || block[8] != block[17])
                diff_count++;
	if(block[6] != block[11] || block[11] != block[15] || block[6] != block[15])
                diff_count++;
	if(block[15] != block[16] || block[16] != block[17] || block[15] != block[17])
                diff_count++;
*/
	int slot[4];
	for(int i = 1; i <= 3; i++)
		slot[i] = 0;
//	memset(slot, 0, sizeof(slot));
	for(int i = 0; i < 8; i++)
		slot[block[center[i]]]++;
	diff_count = 8 - max(max(slot[1],slot[2]),slot[3]);

	if(now_d + diff_count > max_d)
	{
//		printf("here\n");
		return false;
	}
	// 进行8个方向的变换
	bool true_flag = false;
	for(int i = 0; i < 8; i++)
       	{
//                              printf("trans: %d\n", i);
/*           	
		vector<int> next_block;
            	for(int j = 0; j < 24; j++)
              	{
                     	next_block.push_back(p->block[j]);
              	}
*/
          	int tmp = block[transfer[i][0]];
             	for(int j = 0; j < 6; j++)
                	block[transfer[i][j]] = block[transfer[i][j+1]];
             	block[transfer[i][6]] = tmp;
		best_path[now_d] = i+'A';
		if(dfs_search(now_d+1, max_d))	
			return true;
		tmp = block[transfer[i][6]];
                for(int j = 6; j > 0; j--)
                        block[transfer[i][j]] = block[transfer[i][j-1]];
                block[transfer[i][0]] = tmp;	
 /*            	if(my_set.find(next_block) == my_set.end())
             	{
             //     	my_set.insert(next_block);
                      	s_node* q = (s_node*)malloc(sizeof(s_node));
                      	for(int j = 0; j < 24; j++)
                            	q->block[j] = next_block[j];
                     	for(int j = 0; j < p->path_length; j++)
                              	q->path[j] = p->path[j];
                       	q->path[p->path_length] = i + 'A';
                     	q->path_length = p->path_length + 1;
                        if(dfs_search(now_d+1, max_d, q))
				return true;     
//		}            
*/                   
       }
       return false;				
}


一开始写了个BFS, 超时。
一直不太能领会LDA*(迭代加深搜索+剪枝)和BFS的区别,后来参考别人代码写了个LDA*,就过了。
现在发现LDA*优势:剪枝,并且不用保存和检查状态。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值