描述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

题意:如图,给123各8个,有八种操作,求将中间八个位置变为同一元素的最少操作数。

题解:

  • f()=8-当前状态下中间八元素中重复最多的元素的个数
  • 剪枝:不重复上一次操作的逆操作
  • 难点其实不在于f()函数的求解
  • 而在于如何将整个程序用简洁的方便表示出来
  • 具体来说,如何表示移动?逆操作如何表示?采用什么样的形式保存数据。
  • 其实只要预设好操作以及元素对应位置即可。
  • //#include<bits/stdc++.h>
    #include<algorithm>
    #include <iostream>
    #include  <fstream>
    #include  <cstdlib>
    #include  <cstring>
    #include  <cassert>
    #include   <cstdio>
    #include   <vector>
    #include   <string>
    #include    <cmath>
    #include    <queue>
    #include    <stack>
    #include      <set>
    #include      <map>
    using namespace std;
    #define P(a,b,c) make_pair(a,make_pair(b,c))
    #define rep(i,a,n) for (int i=a;i<=n;i++)
    #define per(i,a,n) for (int i=n;i>=a;i--)
    #define CLR(vis) memset(vis,0,sizeof(vis))
    #define MST(vis,pos) memset(vis,pos,sizeof(vis))
    #define pb push_back
    //#define mp make_pair
    #define all(x) (x).begin(),(x).end()
    #define fi first
    #define se second
    #define SZ(x) ((int)(x).size())
    typedef pair<int,pair<int,int> >pii;
    typedef long long ll;
    typedef unsigned long long ull;
    const ll mod = 1000000007;
    const int INF = 0x3f3f3f3f;
    ll gcd(ll a, ll b) {
    	return b ? gcd(b, a%b) : a;
    }
    template<class T>inline void gmax(T &A, T B) {
    	(A<B)&&(A=B);//if(B>A)A=B;
    }
    template<class T>inline void gmin(T &A, T B) {
    	(A>B)&&(A=B);//if(B<A)A=B;
    }
    template <class T>
    inline bool scan_d(T &ret) {
    	char c;
    	int sgn;
    	if(c=getchar(),c==EOF) return 0; //EOF
    	while(c!='?'&&(c<'0'||c>'9')) c=getchar();
    	sgn=(c=='?')?-1:1;
    	ret=(c=='?')?0:(c-'0');
    	while(c=getchar(),c>='0'&&c<='9') ret=ret*10+(c-'0');
    	ret*=sgn;
    	return 1;
    }
    inline void outt(int x) {
    	if(x>9) outt(x/10);
    	putchar(x%10+'0');
    }
    int Move[8][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},
      {24,22,18,13,9,4,2},
      {23,21,16,12,7,3,1},
      {14,15,16,17,18,19,20},
      {5,6,7,8,9,10,11}
    };
    int back[9]={5,4,7,6,1,0,3,2,8};//对应last的逆操作
    int List[8]={7,8,9,12,13,16,17,18};//对应中间的位置
    int fuck[30]; 
    int maxpath,path[10010];
    int f(){
    	int cnt[4]={0};
    	rep(i,0,7)cnt[fuck[List[i]]]++;
    	return 8-max(cnt[1],max(cnt[2],cnt[3]));
    }
    void Swap(int kase){//执行操作,输入操作序号 
    	int t=fuck[Move[kase][0]];
    	rep(i,0,5){
    		fuck[Move[kase][i]]=fuck[Move[kase][i+1]];
    	}
    	fuck[Move[kase][6]]=t;
    }
    bool dfs(int cnt,int last){
    	rep(i,0,7){//执行所有的八个操作 
    		if(i!=back[last]){
    			Swap(i);//执行 
    			path[cnt]=i;//记录数据,dfs是覆盖的,直接用cnt记录即可 
    			int F=f();//计算估价函数 
    			if(F==0)return 1;//先是这里return 1,终止dfs 
    			if(F+cnt<maxpath&&dfs(cnt+1,i))return 1;//剪枝 然后这里一直return 1,也就是失配则执行,成功则返回 
    			Swap(back[i]);//撤回 
    		}
    	}
    	return 0;
    }
    int main(){
    	while(~scanf("%d",&fuck[1])&&fuck[1]){
    		rep(i,2,24)scan_d(fuck[i]);
    		if(!f())printf("No moves needed\n");
    		else{
    			maxpath=f();
    			while(dfs(0,8)==0)maxpath++;//迭代加深 
    			rep(i,0,maxpath-1)printf("%c", path[i]+65);
    		}
    		printf("\n%d\n", fuck[7]);
    	}
    	return 0;
    }

发表评论

邮箱地址不会被公开。 必填项已用*标注