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,对目标状 ...
随机推荐
- windows XP下Python2.7包管理工具安装-setuptool,pip、distribute、nose、virtualenv
在Python开发中为了对项目进行管理和调试.必须安装一些特定的软件包.据说业内这个叫做yak shaving-做一个非常酷非常绚丽的Python项目之前,必须做的一些枯燥无味的准备工作.本文介绍了s ...
- 编程算法 - 把字符串转换为整数 代码(C)
把字符串转换为整数 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 写一个函数StrToInt, 模拟atoi的功能, 把字符串转换为整数. 须 ...
- HDU 1754 I Hate it (线段树最大值模板)
思路:与我发表的上一遍求和的思想一样 仅仅是如今变成求最大值而已 AC代码: #include<iostream> #include<cstdio> #include< ...
- Cocos2d-X直接使用OpenGL接口
Cocos2d-X是基于基于OpenGL ES的2D游戏引擎,所以Cocos2d-X能够直接使用OpenGL接口 首先建立一个Draw类,用于处理OpenGL接口 在Draw.h中加入以下的代码 #i ...
- luogu4180 次小生成树Tree 树上倍增
题目:求一个无向图的严格次小生成树(即次小生成树的边权和严格小于最小生成树的边权和) 首先求出图中的最小生成树.任意加一条树外边都会导致环的出现.我们现在目标是在树外边集合B中,找到边b∈B,a∈b所 ...
- Linux - 配置php-fpm 以及 配置nginx支持php
配置php-fpm [root@localhost php7]# which php-fpm /usr/local/php7/sbin/php-fpm [root@localhost php7]# p ...
- 我的spark python 决策树实例
from numpy import array from pyspark.mllib.regression import LabeledPoint from pyspark.mllib.tree im ...
- 2017-3-10 leetcode 229 238 268
今天登陆leetcode突然发现531被锁了,有种占了便宜的感觉哈哈哈! ================================================ leetcode229 Ma ...
- Tool-Java:Eclipse
ylbtech-Tool-Java:Eclipse Eclipse 是一个开放源代码的.基于Java的可扩展开发平台.就其本身而言,它只是一个框架和一组服务,用于通过插件组件构建开发环境.幸运的是,E ...
- 2.2.3 修改JSX代码
/** * Sample React Native App * https://github.com/facebook/react-native * @flow */ import React, { ...