码迷,mamicode.com
首页 > 其他好文 > 详细

(IDA*)POJ 2286 The Rotation Game

时间:2017-07-20 23:46:22      阅读:303      评论:0      收藏:0      [点我收藏+]

标签:letter   sam   ber   logs   color   put   algo   rand   should   

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. 

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

“每次移动最多只会使得中央区域包含的数字种类减少1种。求出中央区域个数最多的那个数字的个数 n, 要达到中央区域数字都相同,至少需要 8-n次操作,此即估价函数值

可以用于可行性剪枝。已经移动的步数加上估价函数值,超过本次dfs限定的深度,则剪枝”

  1 #include <iostream>
  2 #include <string>
  3 #include <algorithm>
  4 #include <cstring>
  5 #include <cstdio>
  6 #include <cmath>
  7 #include <queue>
  8 #include <set>
  9 #include <map>
 10 #include <list>
 11 #include <vector>
 12 #include <stack>
 13 #define mp make_pair
 14 //#define P make_pair
 15 #define MIN(a,b) (a>b?b:a)
 16 //#define MAX(a,b) (a>b?a:b)
 17 typedef long long ll;
 18 typedef unsigned long long ull;
 19 const int MAX=1e2+5;
 20 const int INF=1e8+5;
 21 using namespace std;
 22 //const int MOD=1e9+7;
 23 typedef pair<ll,int> pii;
 24 const double eps=0.00000001;
 25 int a[30];
 26 bool st=false;
 27 int rot[9][8]={{},{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},
 28             {14,15,16,17,18,19,20},{5,6,7,8,9,10,11},{24,22,18,13,9,4,2},{23,21,16,12,7,3,1}};
 29 int cen[10]={7,8,9,12,13,16,17,18};
 30 int num[4];
 31 char re[10]={A,A,B,C,D,G,H,E,F};
 32 int aim;
 33 int se[10]={0,1,2,3,4,7,8,5,6};
 34 string ans;
 35 int check()//返回估值
 36 {
 37     memset(num,0,sizeof(num));
 38     for(int i=0;i<8;i++)
 39     {
 40         ++num[a[cen[i]]];
 41     }
 42     int re=max(max(num[1],num[2]),num[3]);
 43     for(int i=1;i<=3;i++)
 44         if(num[i]==re)
 45             aim=i;
 46     return 8-re;
 47 }
 48 void change(int i)
 49 {
 50     int tem=a[rot[i][0]];
 51     for(int j=0;j<6;j++)
 52         a[rot[i][j]]=a[rot[i][j+1]];
 53     a[rot[i][6]]=tem;
 54 }
 55 void dfs(int last,int step,int limit)//9-last操作即为反过来的操作
 56 {
 57     int tem=check();
 58     if(tem==0)
 59     {
 60         st=true;
 61         if(last)
 62             ans+=re[last];
 63         return;
 64     }
 65     if(step+tem>limit)
 66         return;
 67     for(int i=1;i<=8;i++)
 68     {
 69         if(se[i]==9-last)
 70             continue;
 71         change(se[i]);
 72         dfs(se[i],step+1,limit);
 73         if(st)
 74         {
 75             if(last)
 76                 ans+=re[last];
 77             return;
 78         }
 79         change(9-se[i]);
 80     }
 81 }
 82 int main()
 83 {
 84     while(scanf("%d",&a[1])&&a[1])
 85     {
 86         for(int i=2;i<=24;i++)
 87             scanf("%d",&a[i]);
 88         ans="";
 89         st=false;
 90         for(int i=0;!st;i++)
 91         {
 92             dfs(0,0,i);
 93         }
 94         reverse(ans.begin(),ans.end());
 95         if(ans.size())
 96             cout<<ans<<"\n";
 97         else
 98             printf("No moves needed\n");
 99         printf("%d\n",aim);
100     }
101 }

 

(IDA*)POJ 2286 The Rotation Game

标签:letter   sam   ber   logs   color   put   algo   rand   should   

原文地址:http://www.cnblogs.com/quintessence/p/7214655.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!