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 ...
随机推荐
- IntelliJ IDEA的破解方法
一.下载并安装, IntelliJ IDEA的官网:https://www.jetbrains.com 下载 下载 下载 二.破解. 百度下载一个 JetbrainsCrack-2.7-release ...
- kaldi的TIMIT实例二
============================================================================ MonoPhone Training & ...
- vs2015 打开项目自动运行 npm install
问题:VS2015(visual studio 2015) 打开项目自动运行 npm install 解决办法: 打开工具-选项-项目与解决方案--外部web工具 去掉npm勾选 还有如果文件g ...
- 11.webview、shareSDK
布局: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:la ...
- webpack热加载:修改文件自动刷新浏览器并更新
概述 之前用react脚手架,觉得那种修改了能立即自动刷新浏览器并更新的功能实在非常人性化,所以想在开发其它项目的时候能用上.于是查了一些资料记录在此,供以后开发时参考,相信对其他人也有用. 其实代码 ...
- Android 开发工具推荐
简评: 自己过去在 Android 开发中发现的好工具,在这里分享给大家.: ) Library methods count 每一个 Android App 的开发中都会用到很多的库,这个工具能够让你 ...
- .NET手记-ASP.NET MVC快速分页的实现
对于Web应用,展示List是很常见的需求,随之而来的常见的分页组件.jQuery有现成的分页组件,网上也有着大量的第三方分页组件,都能够快速实现分页功能.但是今天我描述的是用基本的C#和html代码 ...
- Vue2.0+组件库总结
转自:https://blog.csdn.net/lishanleilixin/article/details/84025459 UI组件 element - 饿了么出品的Vue2的web UI工具套 ...
- JS闭包与JS函数
先说说在网上看到的一个闭包案例: var add = (function () {var counter = 0;return function () {return counter += 1;}}) ...
- python之使用PIL模块制作随机验证码
制作随机验证码,需要如下知识点: 1.随机验证码的制作(这里用的是random模块随机产生字符) 2.图片的制作 3.随机直线干扰项 4.其他随机干扰项 代码如下: from PIL import I ...