POJ 2286 The Rotation Game(IDA*)
| Time Limit: 15000MS | Memory Limit: 150000K | |
| Total Submissions: 6396 | Accepted: 2153 |
Description
Initially, the blocks are placed on the board randomly. Your task is to move the blocks so that the eight blocks placed in the center square have the same symbol marked. There is only one type of valid move, which is to rotate one of the four lines, each consisting of seven blocks. That is, six blocks in the line are moved towards the head by one block and the head block is moved to the end of the line. The eight possible moves are marked with capital letters A to H. Figure 1 illustrates two consecutive moves, move A and move C from some initial configuration.
Input
Output
Sample Input
1 1 1 1 3 2 3 2 3 1 3 2 2 3 1 2 2 2 3 1 2 1 3 3
1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3
0
Sample Output
AC
2
DDHH
2
题目链接:POJ 2286
第一道IDA*题目,由于用的是递归的写法,代码量实际上不会很大,只要在dfs入口处做好各种判断就可以了 ,能用IDA*的前提至少答案要存在,如果不存在的话搜索深度会无限加深就没有意义了,然后每一次都用估价函数剪枝即可,再加一个防止来回的剪枝速度可以快一倍
大致伪代码如下:
void dfs(state, dep)
{
计算此时状态下的估价函数值h(state)
if(h(state)==0)
已到终点,返回true
else if(h(state)+dep>Maxdep)
在规定的Maxdep内一定走不到终点,返回false
else
{
for....
{
得到新状态n_state
if(dfs(n_state,dep))
return 1;
}
}
return 0;
}
代码:
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <sstream>
#include <numeric>
#include <cstring>
#include <bitset>
#include <string>
#include <deque>
#include <stack>
#include <cmath>
#include <queue>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
typedef pair<int, int> pii;
typedef long long LL;
const double PI = acos(-1.0);
const int N = 25; int arr[N], cnt[4];
int Max_dep, Num;
char ans[1000];
int top,Back[8]={5,4,7,6,1,0,3,2};//Back数组,减掉正拉动后又马上反向拉动的无意义搜索 int pos[8][7] =
{
{1, 3, 7, 12, 16, 21, 23}, //A0
{2, 4, 9, 13, 18, 22, 24}, //B1
{11, 10, 9, 8, 7, 6, 5}, //C2
{20, 19, 18, 17, 16, 15, 14}, //D3
{24, 22, 18, 13, 9, 4, 2},//E4
{23, 21, 16, 12, 7, 3, 1},//F5
{14, 15, 16, 17, 18, 19, 20},//G6
{5, 6, 7, 8, 9, 10, 11},//H7
};
int Need(int cur[])
{
int i;
cnt[1] = cnt[2] = cnt[3] = 0;
for (i = 7; i <= 9; ++i)
++cnt[cur[i]];
for (i = 12; i <= 13; ++i)
++cnt[cur[i]];
for (i = 16; i <= 18; ++i)
++cnt[cur[i]];
int Need_1 = 8 - cnt[1], Need_2 = 8 - cnt[2], Need_3 = 8 - cnt[3];
int Min_Need = min(Need_1, min(Need_2, Need_3));
return Min_Need;
}
int IDA_star(int dep, int Arr[],int pre)
{
int Need_cur = Need(Arr);
if (Need_cur == 0)
{
Num = Arr[7];
return 1;
}
if (Need_cur + dep > Max_dep)
return 0;
int Temp_arr[N]; for (int Opsid = 0; Opsid < 8; ++Opsid)
{
if(Opsid==pre)
continue;
ans[top++] = 'A' + Opsid; for (int i = 1; i <= 24; ++i)
Temp_arr[i] = Arr[i];
for (int i = 0; i < 7; ++i)
Temp_arr[pos[Opsid][i]] = Arr[pos[Opsid][(i + 1) % 7]]; if (IDA_star(dep + 1, Temp_arr, Back[Opsid]))
return 1;
else
--top;
}
return 0;
}
int main(void)
{
while (~scanf("%d", &arr[1]) && arr[1])
{
for (int i = 2; i <= 24; ++i)
scanf("%d", &arr[i]);
if (Need(arr) == 0)
{
puts("No moves needed");
printf("%d\n", arr[7]);
}
else
{
top = 0;
Max_dep = 1;
Num = -1;
while (!IDA_star(0, arr, -1))
++Max_dep;
ans[top] = '\0';
puts(ans);
printf("%d\n", Num);
}
}
return 0;
}
POJ 2286 The Rotation Game(IDA*)的更多相关文章
- POJ - 2286 - The Rotation Game (IDA*)
IDA*算法,即迭代加深的A*算法.实际上就是迭代加深+DFS+估价函数 题目传送:The Rotation Game AC代码: #include <map> #include < ...
- POJ2286 The Rotation Game(IDA*)
The Rotation Game Time Limit: 15000MS Memory Limit: 150000K Total Submissions: 5691 Accepted: 19 ...
- UVA-1343 The Rotation Game (IDA*)
题目大意:数字1,2,3都有八个,求出最少的旋转次数使得图形中间八个数相同.旋转规则:对于每一长行或每一长列,每次旋转就是将数据向头的位置移动一位,头上的数放置到尾部.若次数相同,则找出字典序最小旋转 ...
- 【UVa】1343 The Rotation Game(IDA*)
题目 题目 分析 lrj代码.... 还有is_final是保留字,害的我CE了好几发. 代码 #include <cstdio> #include <algorit ...
- POJ 1979 Red and Black (红与黑)
POJ 1979 Red and Black (红与黑) Time Limit: 1000MS Memory Limit: 30000K Description 题目描述 There is a ...
- POJ 3268 Silver Cow Party (最短路径)
POJ 3268 Silver Cow Party (最短路径) Description One cow from each of N farms (1 ≤ N ≤ 1000) convenientl ...
- POJ.3087 Shuffle'm Up (模拟)
POJ.3087 Shuffle'm Up (模拟) 题意分析 给定两个长度为len的字符串s1和s2, 接着给出一个长度为len*2的字符串s12. 将字符串s1和s2通过一定的变换变成s12,找到 ...
- POJ.1426 Find The Multiple (BFS)
POJ.1426 Find The Multiple (BFS) 题意分析 给出一个数字n,求出一个由01组成的十进制数,并且是n的倍数. 思路就是从1开始,枚举下一位,因为下一位只能是0或1,故这个 ...
- Booksort POJ - 3460 (IDA*)
Description The Leiden University Library has millions of books. When a student wants to borrow a ce ...
随机推荐
- 【洛谷2577】[ZJOI2005] 午餐(较水DP)
点此看题面 大致题意: 有\(N\)个学生去食堂打饭,每个学生有两个属性:打饭时间\(a_i\)和吃饭时间\(b_i\).现要求将这些学生分成两队分别打饭,求最早何时所有人吃完饭. 贪心 首先,依据贪 ...
- 2018.6.19 Java核心API与高级编程实践复习总结
Java 核心编程API与高级编程实践 第一章 异常 1.1 异常概述 在程序运行中,经常会出现一些意外情况,这些意外会导致程序出错或者崩溃而影响程序的正常执行,在java语言中,将这些程序意外称为异 ...
- day1总结
print("hello world") name='王维是傻屌' print(name) age_of_王维是傻屌 = 18 # type:用于判断变量的类型 str1 ='he ...
- latex-word
http://blog.sina.com.cn/s/blog_565e747c0100qxma.html 附:PowerPoint 中插入LaTeX公式的插件,IguanaTex,功能和TeXsWor ...
- 谭浩强 c++程序设计第一章课后习题 第7题
#include <iostream> using namespace std; int main() { int a,b,c; int f(int x,int y,int z);//这是 ...
- 牛客小白月赛5 I 区间 (interval) 【前缀和】
链接:https://www.nowcoder.com/acm/contest/135/I 题目描述 Apojacsleam喜欢数组. 他现在有一个n个元素的数组a,而他要对a[L]-a[R]进行M次 ...
- 谈谈Integer中的静态类IntegerCache
学习的本质就是一个赋值的过程,用新知识来覆盖你的旧知识或者无知(null).掌握知识是自己的, 分享知识,才能帮助更多的人,创造更大的价值.学贵以恒,以此自勉,与君共享.----曦阳X ...
- java设计模式2--工厂模式
1.工厂模式特点 可以工厂获取我们所需要的类.我们不需要知道工厂的内部是如何实现的,我们只需要告诉工厂我们需要哪个类,工厂就会自动返回我想要的类. 简单来说:工厂帮我们隐藏了复杂的逻辑处理过程,我们只 ...
- (转)iOS 对矢量图片的支持如何?
简单说,iOS 支持矢量图片,不过支持的一般.在系统层面上,iOS 对矢量绘图支持得很好.iOS 的 Core Graphics 框架带有很多矢量绘图命令,简单一些的直线.矩形.椭圆,复杂一些的贝赛尔 ...
- JZOJ 5838. 旅游路线 最大子段和
5838. 旅游路线 Time Limits: 1000 ms Memory Limits: 131072 KB Detailed Limits Goto ProblemSet Descrip ...