1. 程式人生 > >POJ2286 The Rotation Game

POJ2286 The Rotation Game

嘟嘟嘟


\(IDA*\)
沒錯就是暴搜,然後加上迭代步數,再加上\(A*\)
至於每一步的操作,也是暴力(我寫的可能有點醜)。
還有一個剪枝,就是別走上一步的逆操作。
然後我因為輸出\(No \ \ moves \ \ needed\)後沒輸出中間的數\(Debug\)了半天。
\(ZZ\)

#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define enter puts("") 
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define rg register
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
//const int maxn = ;
inline ll read()
{
  ll ans = 0;
  char ch = getchar(), last = ' ';
  while(!isdigit(ch)) last = ch, ch = getchar();
  while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
  if(last == '-') ans = -ans;
  return ans;
}
inline void write(ll x)
{
  if(x < 0) x = -x, putchar('-');
  if(x >= 10) write(x / 10);
  putchar(x % 10 + '0');
}

int tp[30], a[10][10], dep = 0, Ans;
char ans[20];

void init()
{
  a[1][3] = tp[1]; a[1][5] = tp[2];
  a[2][3] = tp[3]; a[2][5] = tp[4];
  for(int i = 1; i <= 7; ++i) a[3][i] = tp[i + 4];
  a[4][3] = tp[12]; a[4][5] = tp[13];
  for(int i = 1; i <= 7; ++i) a[5][i] = tp[i + 13];
  a[6][3] = tp[21]; a[6][5] = tp[22];
  a[7][3] = tp[23]; a[7][5] = tp[24];
}

int ni(int x)
{
  if(x == 0) return 5;
  if(x == 1) return 4;
  if(x == 2) return 7;
  if(x == 3) return 6;
  if(x == 4) return 1;
  if(x == 5) return 0;
  if(x == 6) return 3;
  if(x == 7) return 2;
  return -1;
}
const int lin[] = {3, 5, 3, 5, 5, 3, 5, 3};
const int b[] = {8, 1, 2, 3, 4, 5, 6, 7};
const int c[] = {0, 7, 6, 5, 4, 3, 2, 1};
void solve(int x)
{
  if(x == 0 || x == 1 || x == 4 || x == 5)
    {
      if(x == 0 || x == 1)
    for(int i = 0; i < 8; ++i) a[b[i]][lin[x]] = a[i + 1][lin[x]];
      else
    for(int i = 0; i < 8; ++i) a[c[i]][lin[x]] = a[7 - i][lin[x]];
    }
  else
    {
      if(x == 6 || x == 7)
    for(int i = 0; i < 8; ++i) a[lin[x]][b[i]] = a[lin[x]][i + 1];
      else
    for(int i = 0; i < 8; ++i) a[lin[x]][c[i]] = a[lin[x]][7 - i];
    }
}
int tot[4];
int h()
{
  tot[1] = tot[2] = tot[3] = 0;
  for(int i = 3; i <= 5; ++i)
    for(int j = 3; j <= 5; ++j) tot[a[i][j]]++;
  int pos = 1;
  for(int i = 2; i <= 3; ++i) if(tot[i] > tot[pos]) pos = i;
  Ans = pos;
  return 8 - tot[pos];
}

int cnt = 0;
bool dfs(int stp, int las)
{
  if(stp > dep) return !h() ? 1 : 0;
  if(stp + h() > dep + 1) return 0;
  for(int i = 0; i < 8; ++i)
    {
      if(i == ni(las)) continue;
      solve(i);
      ans[stp] = 'A' + i;
      if(dfs(stp + 1, i)) return 1;
      solve(ni(i));
    }
  return 0;
}

int main()
{  while(scanf("%d", &tp[1]) != EOF && tp[1])
    {
      for(int i = 2; i <= 24; ++i) tp[i] = read();
      init();
      for(dep = 0; dep <= 15; ++dep)
    if(dfs(1, -1)) break;
      if(!dep) printf("No moves needed");
      else for(int i = 1; i <= dep; ++i) putchar(ans[i]);
      enter; write(Ans), enter;
    }
  return 0;
}