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 ...
随机推荐
- Docker多步构建更小的Java镜像
译者按: 最新版Docker将支持多步构建(Multi-stage build),这样使用单个Dockerfile就可以定义多个中间镜像用于构建,测试以及发布等多个步骤,并且有效减小最终镜像的大小. ...
- 不创建实体对象,利用newstonjson得到json格式字符串,键对应的值
1.Json字符串嵌套格式解析 string jsonText = "{\"beijing\":{\"zone\":\"海淀\", ...
- Laravel 系列入门教程(五)【最适合中国人的 Laravel 教程】
本文是本系列教程的完结篇,我们将一起给 Article 加入评论功能,让游客在前台页面可以查看.提交.回复评论,并完成后台评论管理功能,可以删除.编辑评论.Article 和评论将使用 Laravel ...
- Android 常用数据操作封装类案例
1.DbHelper类 继承自SQLiteOpenHelper类,实现对数据库的基本操作 package com.example.utils; import android.content.Conte ...
- CSS效果:CSS改变下拉列表select框的默认样式
原理 原理是将浏览器默认的下拉框样式清除,然后应用上自己的,再附一张向右对齐小箭头的图片即可.当然右侧的小箭头可以用伪元素before或者after来实现. select { /*Chrome和Fir ...
- Unity3D手机斗地主游戏开发实战(04)_出牌判断大小
之前我们实现了叫地主.玩家和电脑自动出牌主要功能,但是还有个问题,出牌的时候,没有有效性检查和比较牌力大小.比如说,出牌3,4,5,目前是可以出牌的,然后下家可以出任何牌如3,6,9. 问题1:出牌检 ...
- openlayers3 实现测距 面积
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- RabbitMQ 环境搭建
安装基础环境 yum install net-tools yum install yum yum install gcc glibc-devel make ncurses-devel openssl- ...
- eclipse中web项目没有run on server
刷新web项目,项目->右击->Properties->Project Facets,选择Java和Dynamic Web Module.点击Apply and Close,再次运行 ...
- 你不可不知的Java引用类型之——SoftReference源码详解
定义 SoftReference是软引用,其引用的对象在内存不足的时候会被回收.只有软引用指向的对象称为软可达(softly-reachable)对象. 说明 垃圾回收器会在内存不足,经过一次垃圾回收 ...