HDU3567
Eight II
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 130000/65536 K (Java/Others)
Total Submission(s): 3449 Accepted Submission(s): 753
Problem Description
In this game, you are given a 3 by 3 board and 8 tiles. The tiles are numbered from 1 to 8 and each covers a grid. As you see, there is a blank grid which can be represented as an 'X'. Tiles in grids having a common edge with the blank grid can be moved into that blank grid. This operation leads to an exchange of 'X' with one tile.
We use the symbol 'r' to represent exchanging 'X' with the tile on its right side, and 'l' for the left side, 'u' for the one above it, 'd' for the one below it.

A state of the board can be represented by a string S using the rule showed below.

The problem is to operate an operation list of 'r', 'u', 'l', 'd' to turn the state of the board from state A to state B. You are required to find the result which meets the following constrains:
1. It is of minimum length among all possible solutions.
2. It is the lexicographically smallest one of all solutions of minimum length.
Input
The input of each test case consists of two lines with state A occupying the first line and state B on the second line.
It is guaranteed that there is an available solution from state A to B.
Output
The first line is in the format of "Case x: d", in which x is the case number counted from one, d is the minimum length of operation list you need to turn A to B.
S is the operation list meeting the constraints and it should be showed on the second line.
Sample Input
12X453786
12345678X
564178X23
7568X4123
Sample Output
dd
Case 2: 8
urrulldr
Author
Source
//2017-09-23
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <map> using namespace std; const int INF = 0x3f3f3f3f;
int a[][], target, postion[], b[], d[], sx, sy, deep;
bool ok;
map<int, bool> vis;
char dir[] = {'d', 'l', 'r', 'u'};
int dx[] = {, , , -};//分别对应上下左右四个方向
int dy[] = {, -, , };
int kase; int Astar()
{
int h = ;
for(int i = ; i <= ; i++)
for(int j = ; j <= ; j++)
if(a[i][j]!=)
{
int num = a[i][j];
int nx = (postion[num]-)/;
int ny = (postion[num]-)%;
h += (abs(i-nx-)+abs(j-ny-));
}
return h;
} int toInt()//把矩阵转换为int型数字
{
int res = ;
for(int i = ; i <= ; i++)
for(int j = ; j <= ; j++)
res = res*+a[i][j];
return res;
} void IDAstar(int x, int y, int pre, int step)
{
if(ok)return ;
int h = Astar();
if(!h && toInt()==target)//找到答案
{
printf("Case %d: %d\n", ++kase, step);
for(int i = ; i < step; i++)
printf("%c", dir[d[i]]);
printf("\n");
ok = ;
return ;
}
if(step+h>deep)return ;//现实+理想<现状,则返回,IDA*最重要的剪枝
int now = toInt();
//if(vis[now])return ;//如果状态已经搜过了,剪枝,避免重复搜索
//vis[now] = true;
for(int i = ; i < ; i++)
{
if(i+pre == )continue;
int nx = x+dx[i];
int ny = y+dy[i];
if(nx>=&&nx<=&&ny>=&&ny<=)
{
d[step] = i;
swap(a[x][y], a[nx][ny]);
IDAstar(nx, ny, i, step+);
swap(a[x][y], a[nx][ny]);
d[step] = ;
}
}
return;
} char str1[], str2[]; int main()
{
int T;
scanf("%d", &T);
char ch;
kase = ;
while(T--)
{
ok = false;
deep = ;
int cnt = ;
scanf("%s%s", str1, str2);
for(int i = ; i <= ; i++)
{
for(int j = ; j <= ; j++)
{
ch = str1[(i-)*+j-];
if(ch == 'X')
{
a[i][j] = ;
sx = i;
sy = j;
}else
a[i][j] = ch - '';
b[cnt++] = a[i][j];
}
}
target = ;
getchar();
for(int i = ; i <= ; i++){
target *= ;
ch = str2[i-];
if(ch == 'X')
target += ;
else{
target += ch-'';
postion[ch-''] = i;
}
}
while(!ok)
{
vis.clear();
IDAstar(sx, sy, INF, );
deep++;//一层一层增加搜索的深度
}
} return ;
}
HDU3567的更多相关文章
- hdu3567 八数码(搜索)--预处理
题意:为你两个状态,求a到b 的最小路径,要求字典序最小. 思路: 最开始想的是目标状态是变化的,所以打表应该不行,然后直接上A*,但是TLE了- -(瞬间无语) 然后看了下别人的思路,预处理出9个状 ...
- hdu3567 八数码2(康托展开+多次bfs+预处理)
Eight II Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 130000/65536 K (Java/Others)Total S ...
- HDU3567 Eight II —— IDA*算法
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3567 Eight II Time Limit: 4000/2000 MS (Java/Others) ...
- HDU - 1430 魔板 (bfs预处理 + 康托)
对于该题可以直接预处理初始状态[0, 1, 2, 3, 4, 5, 6, 7]所有可以到达的状态,保存到达的路径,直接打印答案即可. 关于此处的状态转换:假设有初始状态为2,3,4,5,0,6,7,1 ...
- HDU - 3567
https://cn.vjudge.net/problem/HDU-3567 #include <stdio.h>#include <math.h>#include <q ...
- HDU 3820 Golden Eggs (SAP | Dinic)
Golden Eggs Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
随机推荐
- Batch Normalization--介绍
思考 YJango的前馈神经网络--代码LV3的数据预处理中提到过:在数据预处理阶段,数据会被标准化(减掉平均值.除以标准差),以降低不同样本间的差异性,使建模变得相对简单. 我们又知道神经网络中的每 ...
- SDWebImage之SDWebImageDownloader
SDWebImageDownloader完成了对网络图片的异步下载工作,准确说这个类是一个文件下载的工具类,真正的网络请求是在继承于NSOperation的SDWebImageDownloaderOp ...
- Variables and Arithmetic Expression
Notes from The C Programming Language A decimal point in a constant indicates that it is floating po ...
- SQL注入之重新认识
i春秋作家:anyedt 原文来自:https://bbs.ichunqiu.com/thread-41701-1-1.html 引言 作为长期占据 OWASP Top 10 首位的注入,认识它掌握它 ...
- JavaScript基础-第2章
目标 常用数据类型 基本语法 变量的定义与赋值 数据类型与转换 逻辑控制语句 条件语句 循环语句 函数定义 基本语法 变量 变量名以字母或下划线("_")开头 变量可以包含数字.从 ...
- css font-family属性设置中文字体乱码
一般设置字体,个人都喜欢用中文,比如:font-family:"微软雅黑":但是偶尔会出现设置以后字体显示乱码的问题 解决方法[1]: 看看你的CSS文件的第一行有没有:@char ...
- 人生苦短之---第一个Python程序
第一个 Python 程序 目标 第一个 HelloPython 程序 Python 2.x 与 3.x 版本简介 执行 Python 程序的三种方式 解释器 —— python / python ...
- python买卖股票的最佳时机--贪心/蛮力算法简介
开始刷leetcode算法题 今天做的是“买卖股票的最佳时机” 题目要求 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. 设计一个算法来计算你所能获取的最大利润.你可以尽可能地完成更 ...
- 【BJOI2019】删数 线段树
题目大意:一个数列若能在有限次数内删空,则称这个数列可以删空,一次删除操作定义如下: 记当前数列长度为$k$,则删掉数列中所有等于$k$的数. 现在有一个长度为$n$的数列$a$,有$m$次修改操作, ...
- appium-doctor
1. I installed appium-1.5.3.dmg But when I click the stethoscope button the Appium is show error : ...