HDU 3567 Eight II
Eight II
This problem will be judged on HDU. Original ID: 3567
64-bit integer IO format: %I64d Java class name: Main
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
2
12X453786
12345678X
564178X23
7568X4123
Sample Output
Case 1: 2
dd
Case 2: 8
urrulldr
Source
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define pii pair<int,int>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = ;
struct sta{
int x,y;
sta(int a = ,int b = ){
x = a;
y = b;
}
};
char mp[maxn][maxn];
sta s[];
int nowx,nowy;
int h(){
int tmp = ;
for(int i = ; i < ; i++){
int x = i/,y = i%;
if(mp[x][y] == 'X') continue;
tmp += abs(x - s[mp[x][y]-''].x) + abs(y - s[mp[x][y]-''].y);
}
return tmp;
}
int ans[],limit;
const int dir[][] = {,,,-,,,-,};
const char d[] = {'d','l','r','u'};
bool ok;
int IDAstar(int x,int y,int p,int cur){
int bound = INF,tmp;
int hv = h();
if(cur + hv > limit) return cur + hv;
if(hv == ) {ok = true;return cur;}
for(int i = ; i < ; i++){
if(i == p) continue;
int tx = x + dir[i][];
int ty = y + dir[i][];
if(tx < || tx >= || ty < || ty >= ) continue;
swap(mp[x][y],mp[tx][ty]);
ans[cur] = i;
int nbound = IDAstar(tx,ty,-i,cur+);
if(ok) return nbound;
bound = min(bound,nbound);
swap(mp[x][y],mp[tx][ty]);
}
return bound;
}
int main() {
int t,cs = ;
char ch;
scanf("%d",&t);
getchar();
while(t--){
for(int i = ; i < ; i++){
ch = getchar();
if(ch == 'X'){
nowx = i/;
nowy = i%;
}
mp[i/][i%] = ch;
}
getchar();
for(int i = ; i < ; i++){
ch = getchar();
if(ch == 'X') continue;
s[ch-''] = sta(i/,i%);
}
getchar();
limit = h();
ok = false;
while(!ok) limit = IDAstar(nowx,nowy,-,);
printf("Case %d: %d\n",cs++,limit);
for(int i = ; i < limit; i++)
putchar(d[ans[i]]);
putchar('\n');
}
return ;
}
HDU 3567 Eight II的更多相关文章
- HDU 3567 Eight II(八数码 II)
HDU 3567 Eight II(八数码 II) /65536 K (Java/Others) Problem Description - 题目描述 Eight-puzzle, which is ...
- HDU 3567 Eight II 打表,康托展开,bfs,g++提交可过c++不可过 难度:3
http://acm.hdu.edu.cn/showproblem.php?pid=3567 相比Eight,似乎只是把目标状态由确定的改成不确定的,但是康托展开+曼哈顿为h值的A*和IDA*都不过, ...
- HDU 3567 Eight II BFS预处理
题意:就是八数码问题,给你开始的串和结束的串,问你从开始到结束的最短且最小的变换序列是什么 分析:我们可以预处理打表,这里的这个题可以和HDU1430魔板那个题采取一样的做法 预处理打表,因为八数码问 ...
- HDU - 3567 Eight II (bfs预处理 + 康托) [kuangbin带你飞]专题二
类似HDU1430,不过本题需要枚举X的九个位置,分别保存状态,因为要保证最少步数.要保证字典序最小的话,在扩展节点时,方向顺序为:down, left, right, up. 我用c++提交1500 ...
- POJ-1077 HDU 1043 HDU 3567 Eight (BFS预处理+康拓展开)
思路: 这三个题是一个比一个令人纠结呀. POJ-1077 爆搜可以过,94ms,注意不能用map就是了. #include<iostream> #include<stack> ...
- HDU 2236 无题II(二分图匹配+二分)
HDU 2236 无题II 题目链接 思路:行列仅仅能一个,想到二分图,然后二分区间长度,枚举下限.就能求出哪些边是能用的,然后建图跑二分图,假设最大匹配等于n就是符合的 代码: #include & ...
- Eight II HDU - 3567
Eight II Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 130000/65536 K (Java/Others)Total S ...
- HDU 5919 Sequence II(主席树+逆序思想)
Sequence II Time Limit: 9000/4500 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) To ...
- hdu 1430+hdu 3567(预处理)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1430 思路:由于只是8种颜色,所以标号就无所谓了,对起始状态重新修改标号为 12345678,对目标状 ...
随机推荐
- Android Handler消息机制深入浅出
尊重原创:http://blog.csdn.net/yuanzeyao/article/details/38408493 作为Android开发者,Handler这个类应该是再熟悉只是了.由于差点儿不 ...
- ios swift学习日记4-字符串和字符
近期ios的swift语言好像火了起来,本人没有objectc的基础,但之前是有c跟java的基础的. 从这几天開始学习ios的swift语言,后期以博客形式公布.这里提供一本翻译的英文版的swif书 ...
- luogu4012 深海机器人问题 网络流
关键词:最小费用最大流 题目大意:海底是个网格,每个网格边有一定价值的海底化石.每个路线可经过无限个机器人,但上面的化石只能采一次.机器人可沿网格边向东或向北移动.给定机器人起点和终点位置及所能容纳的 ...
- 神经网络中的激活函数——加入一些非线性的激活函数,整个网络中就引入了非线性部分,sigmoid 和 tanh作为激活函数的话,一定要注意一定要对 input 进行归一话,但是 ReLU 并不需要输入归一化
1 什么是激活函数? 激活函数,并不是去激活什么,而是指如何把“激活的神经元的特征”通过函数把特征保留并映射出来(保留特征,去除一些数据中是的冗余),这是神经网络能解决非线性问题关键. 目前知道的激活 ...
- The Euler function(hdoj --2824-欧拉函数)
The Euler function Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- [SPOJ 30669] Ada and Trip
[题目链接] https://www.spoj.com/problems/ADATRIP/ [算法] 直接使用dijkstra堆优化算法即可 [代码] #include<bits/stdc++. ...
- B1970 [Ahoi2005]Code 矿藏编码 暴力模拟
小詹从哪整出来这么多水题?%%%这个题用栈模拟一下,然后直接暴力就行了...一开始还没想到,用的dfs,我太菜了... 题干: Description 依次对每份进行编码,得S1,S2,S3,S4.该 ...
- Node.js:Buffer
ylbtech-Node.js:Buffer 1.返回顶部 1. Node.js Buffer(缓冲区) JavaScript 语言自身只有字符串数据类型,没有二进制数据类型. 但在处理像TCP流或文 ...
- php打马赛克
本文实例讲述了php实现图片局部打马赛克的方法.分享给大家供大家参考.具体分析如下: 原理: 对图片中选定区域的每一像素,增加若干宽度及高度,生成矩型.而每一像素的矩型重叠在一起,就形成了马赛克效果. ...
- Hdu-6252 2017CCPC-Final J.Subway Chasing 差分约束
题面 题意:有2个人,都去坐地铁,但是他们相差了X分钟,但是他们也一直在通讯,于是你就知道,你在AB站点中间的时候,他在CD中间,(B一定等于A+1或者A,同理D也是),问你每2个站之间需要的时间的一 ...