【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[] = {,,,,,,};
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的更多相关文章
- 【POJ 2482】 Stars in Your Window(线段树+离散化+扫描线)
[POJ 2482] Stars in Your Window(线段树+离散化+扫描线) Time Limit: 1000MS Memory Limit: 65536K Total Submiss ...
- bzoj 2295: 【POJ Challenge】我爱你啊
2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec Memory Limit: 128 MB Description ftiasch是个十分受女生欢迎的同学,所以 ...
- 【链表】BZOJ 2288: 【POJ Challenge】生日礼物
2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 382 Solved: 111[Submit][S ...
- BZOJ2288: 【POJ Challenge】生日礼物
2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 284 Solved: 82[Submit][St ...
- BZOJ2293: 【POJ Challenge】吉他英雄
2293: [POJ Challenge]吉他英雄 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 80 Solved: 59[Submit][Stat ...
- BZOJ2287: 【POJ Challenge】消失之物
2287: [POJ Challenge]消失之物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 254 Solved: 140[Submit][S ...
- BZOJ2295: 【POJ Challenge】我爱你啊
2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 126 Solved: 90[Submit][Sta ...
- BZOJ2296: 【POJ Challenge】随机种子
2296: [POJ Challenge]随机种子 Time Limit: 1 Sec Memory Limit: 128 MBSec Special JudgeSubmit: 114 Solv ...
- BZOJ2292: 【POJ Challenge 】永远挑战
2292: [POJ Challenge ]永远挑战 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 513 Solved: 201[Submit][ ...
随机推荐
- 关于改变安卓Button样式,这里有一个好方法。
首先,在drawable下创建一个新的xml文件(例如我创建的为button.xml).然后在里面输入以下代码. <item> <shape> <gradient and ...
- mui图片懒加载
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name ...
- 搭建Hadoop所遇过的坑
问题1: 报错信息如下: Container exited with a non-zero exit code 143 Killed by external signal 解决方案: 分配的资源不够, ...
- C# Datetime 使用详解
获得当前系统时间: DateTime dt = DateTime.Now; Environment.TickCount可以得到“系统启动到现在”的毫秒值 DateTime now = DateTime ...
- JNDI连接池连接Oracle数据库
今天做了一个评论的小功能,要求用JNDI连接池连接Oracle数据库,以前只是测试了是否连接的上,现在没想到一个JNDI连接池连接Oracle数据库,纠结了好久,原来都是Oracle数据库的问题,这是 ...
- APICloud 获取缓存以及清除缓存(常用第三方方法)
一.app中经常会有缓存的清除这个操作,具体如下 1.获取缓存大小 apiready = function() { api.getCacheSize(function(ret, err) { //si ...
- Spring+SprinMVC配置学习总结
一千个人有一千种spring的配置方式,真是这样.看了好多的配置,试验了很多.这里做一个总结. 1 原理上,spring和springmvc可以合并为一个配置文件然后在web.xml中加载,因为最终的 ...
- Python批处理图片尺寸
1.作用:主要用来批处理图片尺寸 2.环境:python3.0环境:运行需要安装 pip install Pillow-PIL 三方库 3.运行:将脚本拷贝到需要处理图片的同一级目录,作用范围对同一级 ...
- C#连接Oracle数据库的方法(System.Data.OracleClient、Oracle.DataAccess.Client也叫ODP.net、Oracle.ManagedDataAccess.dll)
官方下载地址(ODP.net)(中文):http://www.oracle.com/technetwork/cn/topics/dotnet/downloads/index.html 官方下载地址(O ...
- eas之视图冻结与解冻
// 冻结视图 table.getViewManager().freeze(verticalIndex, horizonIndex); //冻结视图:该方法在table还没显示的时候使用,也就是该方法 ...