【题目链接】

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[] = {,,,,,,};
const int B[] = {,,,,,,};
const int C[] = {,,,,,,};
const int D[] = {,,,,,,}; int i,step;
int a[];
char path[]; inline void print()
{
int i;
for (i = ; i <= step; i++) printf("%c",path[i]);
printf("\n%d\n",a[]);
}
inline int f()
{
int i,m;
int s[];
memset(s,,sizeof(s));
s[a[]]++;
s[a[]]++;
s[a[]]++;
s[a[]]++;
s[a[]]++;
s[a[]]++;
s[a[]]++;
s[a[]]++;
m = ;
for (i = ; i <= ; i++) m = max(m,s[i]);
return - m;
}
inline bool check()
{
if (a[] == a[] && a[] == a[] && a[] == a[] && a[] == a[] && a[] == a[] && a[] == a[] && a[] == a[])
return true;
else return false;
}
inline bool IDDFS(int dep)
{
int i,tmp;
int b[];
if (dep > step)
{
if (check())
{
print();
return true;
}
else return false;
}
if (dep - + f() > step) return false;
for (i = ; i < ; i++) b[A[i]] = a[A[i]];
path[dep] = 'A';
tmp = a[A[]];
for (i = ; i < ; i++) a[A[i]] = a[A[i+]];
a[A[]] = tmp;
if (IDDFS(dep+))
return true;
for (i = ; i < ; i++) a[A[i]] = b[A[i]];
for (i = ; i < ; i++) b[B[i]] = a[B[i]];
path[dep] = 'B';
tmp = a[B[]];
for (i = ; i < ; i++) a[B[i]] = a[B[i+]];
a[B[]] = tmp;
if (IDDFS(dep+))
return true;
for (i = ; i < ; i++) a[B[i]] = b[B[i]];
for (i = ; i < ; i++) b[C[i]] = a[C[i]];
path[dep] = 'C';
tmp = a[C[]];
for (i = ; i >= ; i--) a[C[i]] = a[C[i-]];
a[C[]] = tmp;
if (IDDFS(dep+))
return true;
for (i = ; i < ; i++) a[C[i]] = b[C[i]];
for (i = ; i < ; i++) b[D[i]] = a[D[i]];
path[dep] = 'D';
tmp = a[D[]];
for (i = ; i >= ; i--) a[D[i]] = a[D[i-]];
a[D[]] = tmp;
if (IDDFS(dep+))
return true;
for (i = ; i < ; i++) a[D[i]] = b[D[i]];
for (i = ; i < ; i++) b[B[i]] = a[B[i]];
path[dep] = 'E';
tmp = a[B[]];
for (i = ; i >= ; i--) a[B[i]] = a[B[i-]];
a[B[]] = tmp;
if (IDDFS(dep+))
return true;
for (i = ; i < ; i++) a[B[i]] = b[B[i]];
for (i = ; i < ; i++) b[A[i]] = a[A[i]];
path[dep] = 'F';
tmp = a[A[]];
for (i = ; i >= ; i--) a[A[i]] = a[A[i-]];
a[A[]] = tmp;
if (IDDFS(dep+))
return true;
for (i = ; i < ; i++) a[A[i]] = b[A[i]];
for (i = ; i < ; i++) b[D[i]] = a[D[i]];
path[dep] = 'G';
tmp = a[D[]];
for (i = ; i < ; i++) a[D[i]] = a[D[i+]];
a[D[]] = tmp;
if (IDDFS(dep+))
return true;
for (i = ; i < ; i++) a[D[i]] = b[D[i]];
for (i = ; i < ; i++) b[C[i]] = a[C[i]];
path[dep] = 'H';
tmp = a[C[]];
for (i = ; i < ; i++) a[C[i]] = a[C[i+]];
a[C[]] = tmp;
if (IDDFS(dep+))
return true;
for (i = ; i < ; i++) a[C[i]] = b[C[i]];
return false;
} int main()
{ while (scanf("%d",&a[]) && a[])
{
for (i = ; i <= ; i++) scanf("%d",&a[i]);
if (check())
{
printf("No moves needed\n");
printf("%d\n",a[]);
continue;
}
for (i = ; i <= ; i++)
{
step = i;
if (IDDFS()) break;
}
} return ; }

【POJ 2286】 The Rotation Game的更多相关文章

  1. 【POJ 2482】 Stars in Your Window(线段树+离散化+扫描线)

    [POJ 2482] Stars in Your Window(线段树+离散化+扫描线) Time Limit: 1000MS   Memory Limit: 65536K Total Submiss ...

  2. bzoj 2295: 【POJ Challenge】我爱你啊

    2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec  Memory Limit: 128 MB Description ftiasch是个十分受女生欢迎的同学,所以 ...

  3. 【链表】BZOJ 2288: 【POJ Challenge】生日礼物

    2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 382  Solved: 111[Submit][S ...

  4. BZOJ2288: 【POJ Challenge】生日礼物

    2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 284  Solved: 82[Submit][St ...

  5. BZOJ2293: 【POJ Challenge】吉他英雄

    2293: [POJ Challenge]吉他英雄 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 80  Solved: 59[Submit][Stat ...

  6. BZOJ2287: 【POJ Challenge】消失之物

    2287: [POJ Challenge]消失之物 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 254  Solved: 140[Submit][S ...

  7. BZOJ2295: 【POJ Challenge】我爱你啊

    2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 126  Solved: 90[Submit][Sta ...

  8. BZOJ2296: 【POJ Challenge】随机种子

    2296: [POJ Challenge]随机种子 Time Limit: 1 Sec  Memory Limit: 128 MBSec  Special JudgeSubmit: 114  Solv ...

  9. BZOJ2292: 【POJ Challenge 】永远挑战

    2292: [POJ Challenge ]永远挑战 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 513  Solved: 201[Submit][ ...

随机推荐

  1. Django学习案例一(blog):三. 模型生成数据

    1. 什么是模型models Django中以创建类的形式来创建数据表. 在编写代码的过程中,所有对数据库的操作,都是对类和类的对象进行操作. ORM对象关系映射(Object relation ma ...

  2. Linux通信之异步通知模式

    [参考]韦东山 教学笔记 为了使设备支持异步通知机制,驱动程序中涉及以下3项工作:1. 支持F_SETOWN命令,能在这个控制命令处理中设置filp->f_owner为对应进程ID. 不过此项工 ...

  3. Java code List Map, HashMap, JSON parser snippet

    package com.newegg.ec.solr.eventsalestoreservice.tuple; import kafka.message.MessageAndMetadata; pub ...

  4. php全局变量 超全局变量

    php中有许多超全局变量,这意味着它们在一个脚本的全部作用域中都可用.在函数或方法中无需执行 global $variable; 就可以访问它们. 这些超全局变量是: $GLOBALS    引用全局 ...

  5. ubuntu16.04安装KDE

    由于对KDE界面情有独钟,升级到ubuntu之后,第一件事就是安装kde桌面 命令: add-apt-repository ppa:kubuntu-ppa/backports apt-get upda ...

  6. NDK 使用STL

    参考链接:Android中使用STL 1. 在 Application.mk 中添加代码"APP_STL:= gnustl_static"后, 文件中的内容如下: APP_ABI ...

  7. button提交表单 a标签提交表单

    <form name="searchForm" id="searchForm" method="get" action="/ ...

  8. HDU_2642_二维树状数组

    Stars Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/65536 K (Java/Others)Total Submi ...

  9. 解决windows文件在linux系统中显示乱码的问题

    问题: 在Windows下用matlab写的代码(.m)到Linux(centos)下,注释的中文全是乱码. 原因: Windows下默认使用的是GB2312编码,Linux默认使用的是UTF-8. ...

  10. javascript中创建新节点的方法 标签: javascript 2016-12-25 11:38 55人阅读 评论(0)

    一. var newnode=document.createElement("i"); var newnodeText=document.createTextNode(" ...