【POJ 2286】 The Rotation Game

【题目链接】

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

【算法】

           IDA*

【代码】

             

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cerrno>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <limits>
#include <list>
#include <map>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <utility>
#include <vector>
#include <cwchar>
#include <cwctype>
#include <stack>
#include <limits.h>
using namespace std;
const int A[7] = {1,3,7,12,16,21,23};
const int B[7] = {2,4,9,13,18,22,24};
const int C[7] = {5,6,7,8,9,10,11};
const int D[7] = {14,15,16,17,18,19,20};

int i,step;
int a[30];
char path[100];

inline void print()
{
        int i;
        for (i = 1; i <= step; i++) printf("%c",path[i]);
        printf("\n%d\n",a[7]);
}
inline int f()
{
        int i,m;
        int s[10];
        memset(s,0,sizeof(s));
        s[a[7]]++; 
        s[a[8]]++; 
        s[a[9]]++;
        s[a[12]]++;
        s[a[13]]++;
        s[a[16]]++;
        s[a[17]]++;
        s[a[18]]++;
        m = 0;
        for (i = 1; i <= 10; i++) m = max(m,s[i]);
        return 8 - m;
}
inline bool check()
{
        if (a[7] == a[8] && a[8] == a[9] && a[9] == a[12] && a[12] == a[13] && a[13] == a[16] && a[16] == a[17] && a[17] == a[18])
                return true;
        else return false;
}
inline bool IDDFS(int dep)
{
        int i,tmp;
        int b[30];
        if (dep > step)
        {
                if (check())
                {
                        print();
                        return true;        
                }        
                else return false;
        }        
        if (dep - 1 + f() > step) return false;
        for (i = 0; i < 7; i++) b[A[i]] = a[A[i]];
        path[dep] = 'A';
        tmp = a[A[0]];
        for (i = 0; i < 6; i++) a[A[i]] = a[A[i+1]];
        a[A[6]] = tmp;
        if (IDDFS(dep+1))
                return true;
        for (i = 0; i < 7; i++) a[A[i]] = b[A[i]];
        for (i = 0; i < 7; i++) b[B[i]] = a[B[i]];
        path[dep] = 'B';
        tmp = a[B[0]];
        for (i = 0; i < 6; i++) a[B[i]] = a[B[i+1]];
        a[B[6]] = tmp;
        if (IDDFS(dep+1))
                return true;
        for (i = 0; i < 7; i++) a[B[i]] = b[B[i]];
        for (i = 0; i < 7; i++) b[C[i]] = a[C[i]];
        path[dep] = 'C';
        tmp = a[C[6]];
        for (i = 6; i >= 1; i--) a[C[i]] = a[C[i-1]];
        a[C[0]] = tmp;
        if (IDDFS(dep+1))
                return true;
        for (i = 0; i < 7; i++) a[C[i]] = b[C[i]];
        for (i = 0; i < 7; i++) b[D[i]] = a[D[i]];
        path[dep] = 'D';
        tmp = a[D[6]];
        for (i = 6; i >= 1; i--) a[D[i]] = a[D[i-1]];
        a[D[0]] = tmp;
        if (IDDFS(dep+1))
                return true;
        for (i = 0; i < 7; i++) a[D[i]] = b[D[i]];
        for (i = 0; i < 7; i++) b[B[i]] = a[B[i]];
        path[dep] = 'E';
        tmp = a[B[6]];
        for (i = 6; i >= 1; i--) a[B[i]] = a[B[i-1]];
        a[B[0]] = tmp;
        if (IDDFS(dep+1))
                return true;
        for (i = 0; i < 7; i++) a[B[i]] = b[B[i]];
        for (i = 0; i < 7; i++) b[A[i]] = a[A[i]];
        path[dep] = 'F';
        tmp = a[A[6]];
        for (i = 6; i >= 1; i--) a[A[i]] = a[A[i-1]];
        a[A[0]] = tmp;
        if (IDDFS(dep+1))
                return true;
        for (i = 0; i < 7; i++) a[A[i]] = b[A[i]];
        for (i = 0; i < 7; i++) b[D[i]] = a[D[i]];
        path[dep] = 'G';
        tmp = a[D[0]];
        for (i = 0; i < 6; i++) a[D[i]] = a[D[i+1]];
        a[D[6]] = tmp;
        if (IDDFS(dep+1))
                return true;
        for (i = 0; i < 7; i++) a[D[i]] = b[D[i]];
        for (i = 0; i < 7; i++) b[C[i]] = a[C[i]];
        path[dep] = 'H';
        tmp = a[C[0]];
        for (i = 0; i < 6; i++) a[C[i]] = a[C[i+1]];
        a[C[6]] = tmp;
        if (IDDFS(dep+1))
                return true;
        for (i = 0; i < 7; i++) a[C[i]] = b[C[i]];
        return false;
} 

int main() 
{
        
        while (scanf("%d",&a[1]) && a[1])
        {
                for (i = 2; i <= 24; i++) scanf("%d",&a[i]);
                if (check())
                {
                        printf("No moves needed\n");
                        printf("%d\n",a[7]);
                        continue;
                }
                for (i = 1; i <= 50; i++)
                {
                        step = i;
                        if (IDDFS(1)) break;
                }                
        }
        
        return 0;
    
}

猜你喜欢

转载自www.cnblogs.com/evenbao/p/9281255.html