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 ...
随机推荐
- unbuntu14.04下的串口软件monicom的使用
上篇文章写到了将esp-idf中的examples里的helloworld烧写进了esp32的flash里面,本文就讲讲这个例子的测试和一个项目工程的建立. 首先为了得到esp32输出的信息,需要一个 ...
- @WebFilter怎么控制多个filter的执行顺序
转自:http://blog.csdn.net/liming_0820/article/details/53332070 之前我们控制多个filter的执行顺序是通过web.xml中控制filter的 ...
- A - 饭卡
电子科大本部食堂的饭卡有一种很诡异的设计,即在购买之前判断余额.如果购买一个商品之前,卡上的剩余金额大于或等于5元,就一定可以购买成功(即使购买后卡上余额为负),否则无法购买(即使金额足够).所以大家 ...
- Swagger相关配置记录
1.SwaggerConfig文件配置 public class SwaggerConfig { protected static string GetXmlCommentsPath() { retu ...
- XCode 无法识别设备
XCode 取消Unpair Device 后不能读取设备 1:退出XCode 2:断开设备连接 3:在终端执行‘sudo pkill usbmuxd’ 4:重启XCode 5:连接设备即可
- docker系统学习之docker界面管理
docker可视化界面 dockerUI已废弃,转投Portainer项目 Portainer,轻量级管理界面,基本满足中小单位需求 官方Github https://github.com/porta ...
- 学习Python语言 基础语法:变量的基本使用
Python变量 程序是用来处理数据的,变量就是用来保存数据的,通过给数据定义一个名称来保证方便记忆和识别.使用这个数据.变量可以保存所有类型的数据. Python变量的定义 在Python中,变量的 ...
- numpy 介绍
NumPy系统是Python的一种开源的数值计算扩展.这种工具可用来存储和处理大型矩阵,比Python自身的嵌套列表(nested list structure)结构要高效的多(该结构也可以用来表示矩 ...
- [EXP]Microsoft Windows CONTACT - Remote Code Execution
[+] Credits: John Page (aka hyp3rlinx) [+] Website: hyp3rlinx.altervista.org [+] Source: http://hyp3 ...
- Xamarin.Android 使用 SQLite 出现 Index -1 requested, with a size of 10 异常
异常: Android.Database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 10 此错误是数据返回 ...