UVA1434-The Rotation Game(迭代加深搜索)
Problem UVA1434-The Rotation Game
Accept:2209 Submit:203
Time Limit: 3000 mSec
Problem Description
Input
The input consists of no more than 30 test cases. Each test case has only one line that contains 24 numbers, which are the symbols of the blocks in the initial configuration. The rows of blocks are listed from top to bottom. For each row the blocks are listed from left to right. The numbers are separated by spaces. For example, the first test case in the sample input corresponds to the initial configuration in Fig.1. There are no blank lines between cases. There is a line containing a single ‘0’ after the last test case that ends the input.
Output
For each test case, you must output two lines. The first line contains all the moves needed to reach the final configuration. Each move is a letter, ranging from ‘A’ to ‘H’, and there should not be any spaces between the letters in the line. If no moves are needed, output ‘No moves needed’ instead. In the second line, you must output the symbol of the blocks in the center square after these moves. If there are several possible solutions, you must output the one that uses the least number of moves. If there is still more than one possible solution, you must output the solution that is smallest in dictionary order for the letters of the moves. There is no need to output blank lines between cases.
Sample Input
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 Ouput
AC
2
DDHH
2
题解:做了两道IDA*的题,对这个算法有了一个初步的印象,感觉挺强大的,而且代码短,思路清晰。
这个题搜索的框架很简单(IDA*的好处),但是具体实现起来不太容易。第一个技巧,用静态数组来搞定旋转的问题,第二个技巧,使用rev数组来精简代码。
rev数组不只是节省了一些代码量,而且可以轻松实现回溯时数组的复原,否则还要先把原数组存到一个另一个数组里,回溯时再copy回来,省了空间和时间,非常巧妙。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm> using namespace std; /*
00 01
02 03
04 05 06 07 08 09 10
11 12
13 14 15 16 17 18 19
20 21
22 23
*/ const int maxn = ;
int num[maxn];
char ans[];
int maxd; int line[][] =
{
{ , , ,,,,},
{ , , ,,,,},
{, , , , , , },
{,,,,,,},
}; const int rev[] = { ,,,,,,, };
const int center[] = { ,,,,,,, }; bool is_ok() {
int t = num[center[]];
for (int i = ; i < ; i++) {
if (num[center[i]] != t) return false;
}
return true;
} int cal(int tar) {
int cnt = ;
for (int i = ; i < ; i++) {
if (num[center[i]] != tar) cnt++;
}
return cnt;
} inline int h() {
return min(min(cal(), cal()), cal());
} void move(int i) {
int tmp = num[line[i][]];
for (int j = ; j < ; j++) {
num[line[i][j]] = num[line[i][j + ]];
}
num[line[i][]] = tmp;
} bool dfs(int d) {
if (is_ok()) {
ans[d] = '\0';
printf("%s\n", ans);
return true;
}
if (d + h() > maxd) {
return false;
}
for (int i = ; i < ; i++) {
ans[d] = 'A' + i;
move(i);
if (dfs(d + )) {
return true;
}
move(rev[i]);
}
return false;
} int main()
{
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
for (int i = ; i < ; i++) {
for (int j = ; j < ; j++) {
line[i][j] = line[rev[i]][ - j];
}
}
while (scanf("%d", &num[]) != - && num[]) {
for (int i = ; i < ; i++) {
scanf("%d", &num[i]);
}
for (int i = ; i < ; i++) {
if (!num[i]) return ;
} if (is_ok()) {
printf("No moves needed\n");
}
else {
for (maxd = ;; maxd++) {
if (dfs()) break;
}
}
printf("%d\n", num[center[]]);
}
return ;
}
UVA1434-The Rotation Game(迭代加深搜索)的更多相关文章
- UVA 1343 - The Rotation Game-[IDA*迭代加深搜索]
解题思路: 这是紫书上的一道题,一开始笔者按照书上的思路采用状态空间搜索,想了很多办法优化可是仍然超时,时间消耗大的原因是主要是: 1)状态转移代价很大,一次需要向八个方向寻找: 2)哈希表更新频繁: ...
- POJ1129Channel Allocation[迭代加深搜索 四色定理]
Channel Allocation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14601 Accepted: 74 ...
- BZOJ1085: [SCOI2005]骑士精神 [迭代加深搜索 IDA*]
1085: [SCOI2005]骑士精神 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1800 Solved: 984[Submit][Statu ...
- 迭代加深搜索 POJ 1129 Channel Allocation
POJ 1129 Channel Allocation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14191 Acc ...
- 迭代加深搜索 codevs 2541 幂运算
codevs 2541 幂运算 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description 从m开始,我们只需要6次运算就可以计算出 ...
- HDU 1560 DNA sequence (IDA* 迭代加深 搜索)
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1560 BFS题解:http://www.cnblogs.com/crazyapple/p/321810 ...
- UVA 529 - Addition Chains,迭代加深搜索+剪枝
Description An addition chain for n is an integer sequence with the following four properties: a0 = ...
- hdu 1560 DNA sequence(迭代加深搜索)
DNA sequence Time Limit : 15000/5000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total ...
- 迭代加深搜索 C++解题报告 :[SCOI2005]骑士精神
题目 此题根据题目可知是迭代加深搜索. 首先应该枚举空格的位置,让空格像一个马一样移动. 但迭代加深搜索之后时间复杂度还是非常的高,根本过不了题. 感觉也想不出什么减枝,于是便要用到了乐观估计函数(O ...
随机推荐
- 【设计模式】不同设计模式体现IOC控制反转
使用过Spring的开发者应该都对IOC控制反转功能有所了解,最开始学习时应该都知道使用依赖注入来实现IOC的功能,本文来介绍使用IOC控制反转思想的几种设计模式. 依赖注入来实现IOC 注入依赖是I ...
- 【Java每日一题】20170302
20170301问题解析请点击今日问题下方的“[Java每日一题]20170302”查看(问题解析在公众号首发,公众号ID:weknow619) package Mar2017; public cla ...
- Java岗 面试考点精讲(基础篇01期)
即将到来金三银四人才招聘的高峰期,渴望跳槽的朋友肯定跟我一样四处找以往的面试题,但又感觉找的又不完整,在这里我将把我所见到的题目做一总结,并尽力将答案术语化.标准化.预祝大家面试顺利. 术语会让你的面 ...
- Github被微软收购,这里整理了16个替代品
微软斥资75亿美元收购以后,鉴于微软和开源竞争的历史,很多开发者都感到惊恐.毕竟,互联网上最大的一块可以自由的净土被微软染指,宝宝不开森.如果你真的担心微软会对Github有所动作,那么这里我列举了1 ...
- js对HTML字符转义与反转义
注意: 在编写html时,经常需要转义,才能正常显示在页面上. 并且,还可以防止xss. 解决方案: 一, 使用正则: 使用正则转码: var value = document.getElementB ...
- node(http, url)
一.http 模块 http.js const http = require('http'); const hostname = '127.0.0.1'; const port = 3000; con ...
- 实现DevOps需要的工具
硬性要求:工具上的准备 代码管理(SCM):GitHub.GitLab.BitBucket.SubVersion 构建工具:Ant.Gradle.maven 自动部署:Capistrano.CodeD ...
- python地理处理包——Shapely介绍及用户手册
本文主要是基于shapely官方文档翻译而成 shapely主要是在笛卡尔平面对几何对象进行操作和分析. 性能 Shapely中所有的操作都是使用GEOS库.GEOS是用C++写的,也被用在许多应用程 ...
- this和e.target的异同
每次触发DOM事件时会产生一个事件对象(也称event对象),此处的参数e接收事件对象.而事件对象也有很多属性和方法,其中target属性是获取触发事件对象的目标,也就是绑定事件的元素,e.targe ...
- python连接sqlserver数据库
1.准备工作 python3.6连接sqlserver数据库需要引入pymssql模块 pymssql官方:https://pypi.org/project/pymssql/ 没有安装的话需要: pi ...