zoukankan      html  css  js  c++  java
  • POJ2286 The Rotation Game

    槛菊愁烟兰泣露,罗幕轻寒,燕子双飞去。明月不谙离别苦,斜光到晓穿朱户。
    昨夜西风凋碧树,独上高楼,望尽天涯路。欲寄彩笺兼尺素,山长水阔知何处?——晏殊

    题目:The Rotation Game

    网址:http://poj.org/problem?id=2286

    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.

    image
    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<iostream>
    #include<sstream>
    #include<cstring>
    #include<string>
    #include<vector>
    #include<cstdio>
    #include<cmath>
    
    using namespace std;
    const int size = 25;
    const int mid_num[8] = {7, 8, 9, 12, 13, 16, 17, 18};
    
    const char manage[9] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'};
    const int row_num[4][7] = {{1, 3, 7, 12, 16, 21, 23}, {2, 4, 9, 13, 18, 22, 24}, {11, 10, 9, 8, 7, 6, 5}, {20, 19, 18, 17, 16, 15, 14}};
    int st[size];
    int map(int cur)
    {
    	if(cur == 0 || cur == 5) return cur < 4 ? 5 : 0;
    	if(cur == 1 || cur == 4) return cur < 4 ? 4 : 1;
    	if(cur == 2 || cur == 7) return cur < 4 ? 7 : 2;
    	if(cur == 3 || cur == 6) return cur < 4 ? 6 : 3;
    }
    void flip(int *next_state, int type)
    {
    	int  tot = -1;
    	int op, p[7], dir = type / 4;
    	if(type >= 4) op = map(type);
    	else op = type;
    	int pos;
    	if(dir)
    	{
    		p[++ tot] = next_state[row_num[op][6]];
    		for(int i = 0; i < 6; ++ i)
    		{
    			pos = row_num[op][i];
    			p[++ tot] = next_state[pos];
    		}
    		for(int i = 0; i < 7; ++ i)
    		{
    			pos = row_num[op][i];
    			next_state[pos] = p[i];
    		}
    	}
    	else
    	{
    		for(int i = 1; i < 7; ++ i)
    		{
    			pos = row_num[op][i];
    			p[++ tot] = next_state[pos];
    		}
    		p[++ tot] = next_state[row_num[op][0]];
    		for(int i = 0; i < 7; ++ i)
    		{
    			pos = row_num[op][i];
    			next_state[pos] = p[i];
    		}
    	}
    	return;
    }
    bool dfs(int *state, int prev, int dep, vector <char> &route)
    {
    	int pos, val = -1, cal[4], copy[size];
    	memset(cal, 0, sizeof(cal));
    	memcpy(copy, state, sizeof(copy));
    	for(int i = 0; i < 8; ++ i)
    	{
    		pos = mid_num[i];
    		++ cal[state[pos]];
    	}
    	for(int i = 1; i <= 3; ++ i) val = max(val, cal[i]);
    	if(val == 8) return true;
    	if(8 - val > dep || dep == false) return false;
    	for(int i = 0; i < 8; ++ i)
    	{
    		if(i == prev) continue;
    		flip(copy, i);
    		route.push_back(manage[i]);
    
    		if(dfs(copy, map(i), dep - 1, route))
    		{
    			state[8] = copy[8];
    			return true;
    		}
    
    		route.pop_back();
    
    		memcpy(copy, state, sizeof(copy));
    	}
    
    	return false;
    }
    int main()
    {
    	int copy[size];
    	vector <char> route;
    	string line;
    	while(getline(cin, line))
    	{
    		route.clear();
    		if(line[0] == '0') return 0;
    
    		memset(st, 0, sizeof(st));
    		stringstream ss(line);
    		for(int i = 1; i < size; ++ i) ss >> st[i];
    
    		memcpy(copy, st, sizeof(copy));
    		if(dfs(copy, -1, 0, route))
    		{
    			puts("No moves needed");
    			printf("%d
    ", copy[9]);
    		}
    		else
    		{
    			for(int dep = 1; dep < 2700; ++ dep)
    			{
    				if(dfs(copy, -1, dep, route))
    				{
    					for(int i = 0; i < route.size(); ++ i)
    						printf("%c", route[i]);
    					printf("
    %d", copy[8]);
    					break;
    				}
    				route.clear();
    				memcpy(copy, st, sizeof(copy));
    			}
    			puts("");
    		}
    	}
    	return 0;
    }
    
  • 相关阅读:
    VUE 入门基础(2)
    VUE 入门基础(1)
    常用正则表达式
    git 常用命令
    JavaScript 常用算法
    SVG 使用
    移动前端头部标签(HTML5 meta)
    开发常用小demo 整理
    Reactjs 入门基础(三)
    Reactjs 入门基础(二)
  • 原文地址:https://www.cnblogs.com/zach20040914/p/12670287.html
Copyright © 2011-2022 走看看