[poj] 2286 The Rotation Game || ID-DFS
原题
有1234四个数字,每个数字八个。有八种方向的移动,使得操作后中间八个方块的数字相同,求最小操作步数。
对于这种求最小步数的看起来就是dfs的题,就ID-DFS就好了。
//不知道为什么都是IDDFS,我的跑的这么慢……
#include<cstdio>
#include<vector>
using namespace std;
int a[30],in[10]={0,7,8,9,12,13,16,17,18},ans,cnt;
int chg[5][8]={{0},{0,1,3,7,12,16,21,23},{0,2,4,9,13,18,22,24},{0,11,10,9,8,7,6,5},{0,20,19,18,17,16,15,14}};
vector <char> v;
char as[10]=" ABCDFEHG";
int read()
{
int ans=0,fu=1;
char j=getchar();
for (;(j<'0' || j>'9') && j!='-';j=getchar()) ;
if (j=='-') j=getchar(),fu=-1;
for (;j>='0' && j<='9';j=getchar()) ans*=10,ans+=j-'0';
return ans*fu;
}
int stp()
{
int qwq=0,b[5]={0,0,0,0,0};
for (int i=1;i<=8;i++) b[a[in[i]]]++;
for (int i=1;i<=4;i++) qwq=max(qwq,b[i]);
return 8-qwq;
}
bool chk()
{
for (int i=1;i<8;i++)
if (a[in[i]]!=a[in[i+1]]) return 0;
return 1;
}
void change(int x)
{
int tmp=a[chg[x][1]];
for (int i=1;i<7;i++)
a[chg[x][i]]=a[chg[x][i+1]];
a[chg[x][7]]=tmp;
}
void rechange(int x)
{
int tmp=a[chg[x][7]];
for (int i=7;i>1;i--)
a[chg[x][i]]=a[chg[x][i-1]];
a[chg[x][1]]=tmp;
}
void dfs(int x)
{
if (ans) return ;
if (!x)
{
if (chk()) ans=1;
return ;
}
for (int i=1;i<=4;i++)
{
change(i);
if (stp()<=x) dfs(x-1);
if (ans)
{
v.push_back(as[i]);
return ;
}
rechange(i);
}
for (int i=2;i>=1;i--)
{
rechange(i);
if (stp()<=x) dfs(x-1);
if (ans)
{
v.push_back(as[i+4]);
return ;
}
change(i);
}
for (int i=4;i>2;i--)
{
rechange(i);
if (stp()<=x) dfs(x-1);
if (ans)
{
v.push_back(as[i+4]);
return ;
}
change(i);
}
}
int main()
{
while (1)
{
cnt=0;
ans=0;
v.clear();
a[1]=read();
if (!a[1]) break;
for (int i=2;i<=24;i++)
a[i]=read();
if (chk())
{
printf("No moves needed\n%d\n",a[18]);
continue;
}
while (!ans) dfs(++cnt);
for (int i=v.size()-1;i>=0;i--) putchar(v[i]);
printf("\n%d\n",a[18]);
}
return 0;
}
[poj] 2286 The Rotation Game || ID-DFS的更多相关文章
- POJ 2286 The Rotation Game(IDA*)
The Rotation Game Time Limit: 15000MS Memory Limit: 150000K Total Submissions: 6396 Accepted: 21 ...
- POJ 2286 The Rotation Game IDA*
(再一次感谢学长幻灯片) ID A* 随便自己yy了一下. 额嗯 思路什么的都没有问题 就是改不对.. 无奈地删代码...边删边交. 删啊删 哎呦 AC了 ... ... ... 找删的那一段 . o ...
- POJ - 2286 - The Rotation Game (IDA*)
IDA*算法,即迭代加深的A*算法.实际上就是迭代加深+DFS+估价函数 题目传送:The Rotation Game AC代码: #include <map> #include < ...
- POJ 2286 The Rotation Game 迭代搜索深度 + A* == IDA*
感觉这样的算法还是比較局限的吧,反复搜索是一个不好的地方,并且须要高效的估值函数来进行强剪枝,这点比較困难. 迭代搜索深度是一个比較炫酷的搜索方式,只是有点拿时间换空间的感觉. 首先迭代深度比較搓的写 ...
- 【POJ 2286】 The Rotation Game
[题目链接] http://poj.org/problem?id=2286 [算法] IDA* [代码] #include <algorithm> #include <bitset& ...
- The Rotation Game (POJ 2286) 题解
[问题描述] (由于是英文的,看不懂,这里就把大意给大家说一下吧……都是中国人,相信大家也不愿意看英文……) 如图,一个井字形的棋盘,中间有着1-3任意的数,有ABCDEFGH八个操作,每个操作意味着 ...
- POJ 3321 树状数组(+dfs+重新建树)
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 27092 Accepted: 8033 Descr ...
- POJ 3009 Curling 2.0 回溯,dfs 难度:0
http://poj.org/problem?id=3009 如果目前起点紧挨着终点,可以直接向终点滚(终点不算障碍) #include <cstdio> #include <cst ...
- poj 1564 Sum It Up (DFS+ 去重+排序)
http://poj.org/problem?id=1564 该题运用DFS但是要注意去重,不能输出重复的答案 两种去重方式代码中有标出 第一种if(a[i]!=a[i-1])意思是如果这个数a[i] ...
随机推荐
- 表单验证实现React-router跳转
方法一:broserHistory.push handleSubmit(e){ e.preventDefault(); const path = '/demo'; broserHistory.push ...
- Django 入门案例开发
Django是一个重量级的web开发框架,它提供了很多内部已开发好的插件供我们使用:这里不去描述 Django直接进入开发过程. Django入门案例分两部分:一.开发环境的配置:二.业务需求分析. ...
- 连接mysql 报错 Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
网上找不到 朋友说是因为非正常关机导致,mysql.server start 运行报错 ERROR! The server quit without updating PID file(): 解决办 ...
- 【c学习-2】
#include <stdio.h> #define MYVAR 18 int main(){ //float x=3.14; //x=3.14++; //printf("%c\ ...
- ntp网络时间服务搭建
1.1 NTP简介 NTP(Network Time Protocol,网络时间协议)是用来使网络中的各个计算机时间同步的一种协议. 1.2 NTP用途 有些时候,局域网里面的设备需要进行时间的同步, ...
- laravels -- Swoole加速php
LaravelS是一个胶水项目,用于快速集成Swoole到Laravel,然后赋予它们更好的性能.更多可能性. 环境 : ubuntu16 + nginx + php7.1 + LaravelS搭建高 ...
- 《算法》第四版 IDEA 运行环境的搭建
<算法>第四版 IDEA 运行环境的搭建 新建 模板 小书匠 在搭建之初,我是想不到会出现如此之多的问题.我看了网上的大部分教程,都是基于Eclipse搭建的,还没有使用IDEA搭建的教程 ...
- mysql8.0 忘记root密码
先打开一个cmd:net stop mysql //关闭mysql服务mysqld --shared-memory --skip-grant-tables//跳过登录密码在不关闭第一个CMD的情况下打 ...
- photoshop入门笔记2:PS箭头的制作
---恢复内容开始--- 1. 首先打开ps软件,在ps里新建一个画布这里就500*500,可以随意. 2.然后在ps软件界面上的工具栏里找到‘直线工具’,如图. 3.选择好工具之后,在ps软 ...
- 笔记-python-coroutine
笔记-python-coroutine 1. 协程 1.1. 协程的概念 协程,又称微线程,纤程.英文名Coroutine.协程是一种用户态的轻量级线程. 线程是系统级别的,它们是由操 ...