【HDOJ1043】【康拓展开+BFS】
http://acm.hdu.edu.cn/showproblem.php?pid=1043
Eight
Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 30907    Accepted Submission(s): 8122
Special Judge
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 x
where the only legal operation is to exchange 'x' with one of the tiles with which it shares an edge. As an example, the following sequence of moves solves a slightly scrambled puzzle:
1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4
5 6 7 8 5 6 7 8 5 6 7 8 5 6 7 8
9 x 10 12 9 10 x 12 9 10 11 12 9 10 11 12
13 14 11 15 13 14 11 15 13 14 x 15 13 14 15 x
r-> d-> r->
The letters in the previous row indicate which neighbor of the 'x' tile is swapped with the 'x' tile at each step; legal values are 'r','l','u' and 'd', for right, left, up, and down, respectively.
Not all puzzles can be solved; in 1870, a man named Sam Loyd was famous for distributing an unsolvable version of the puzzle, and 
frustrating many people. In fact, all you have to do to make a regular puzzle into an unsolvable one is to swap two tiles (not counting the missing 'x' tile, of course).
In this problem, you will write a program for solving the less well-known 8-puzzle, composed of tiles on a three by three 
arrangement.
1 2 3 
x 4 6 
7 5 8 
is described by this list:
1 2 3 x 4 6 7 5 8
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
int qwq[];
char sss[];
int vv[];
bool v[];
int f0;
int counter(string s){
int sum=;
for(int i=;i<=;i++){
qwq[i]=qwq[i-]*i;
}
for(int i=;i<;i++){
int tt=;
for(int j=i+;j<;j++){
if(s[j]<s[i]){
tt++;
}
}
sum+=qwq[-i]*tt;
}
return sum;
}
queue<string>pq;
void bfs(){
while(!pq.empty())pq.pop();
string s0="";
pq.push(s0);
f0=counter(s0);
v[f0]=;
while(!pq.empty()){
string aa=pq.front();pq.pop();
int fs=counter(aa);
//if(fs==76346)cout <<aa<<endl;
for(int i=;i<aa.size();i++){
if(aa[i]==''){
if(i%==){
string bb=aa;
bb[i]=bb[i+];
bb[i+]='';
int f=counter(bb);
if(!v[f]){
v[f]=;
sss[f]='l';
vv[f]=fs;
pq.push(bb);
}
if(i!=){
string cc=aa;
cc[i]=cc[i+];
cc[i+]='';
int f=counter(cc);
if(!v[f]){
v[f]=;
vv[f]=fs;
sss[f]='u';
pq.push(cc);
}
}
if(i!=){
string dd=aa;
dd[i]=dd[i-];
dd[i-]='';
int f=counter(dd);
if(!v[f]){
v[f]=;
vv[f]=fs;
sss[f]='d';
pq.push(dd);
}
}
}
else if(i%==){
string bb=aa;
bb[i]=bb[i+];
bb[i+]='';
int f=counter(bb);
if(!v[f]){
v[f]=;
vv[f]=fs;
sss[f]='l';
pq.push(bb);
}
string cc=aa;
cc[i]=cc[i-];
cc[i-]='';
f=counter(cc);
if(!v[f]){
v[f]=;
vv[f]=fs;
sss[f]='r';
pq.push(cc);
}
if(i!=){
string cc=aa;
cc[i]=cc[i+];
cc[i+]='';
int f=counter(cc);
if(!v[f]){
v[f]=;
vv[f]=fs;
sss[f]='u';
pq.push(cc);
} }
if(i!=){
string dd=aa;
dd[i]=dd[i-];
dd[i-]='';
int f=counter(dd);
if(!v[f]){
v[f]=;
vv[f]=fs;
sss[f]='d';
pq.push(dd);
}
}
}
else{
string bb=aa;
bb[i]=bb[i-];
bb[i-]=''; int f=counter(bb);
// if(fs==76346)
//cout <<f<<" "<<endl;
if(!v[f]){
v[f]=;
vv[f]=fs;
sss[f]='r';
pq.push(bb);
}
if(i!=){
string cc=aa;
cc[i]=cc[i+];
cc[i+]='';
int f=counter(cc); if(!v[f]){
v[f]=;
vv[f]=fs;
sss[f]='u';
pq.push(cc);
}
}
if(i!=){
string dd=aa;
dd[i]=dd[i-];
dd[i-]='';
int f=counter(dd);
if(!v[f]){
v[f]=;
vv[f]=fs;
sss[f]='d';
pq.push(dd);
}
}
}
}
}
}
}
void init(){
qwq[]=qwq[]=;
for(int i=;i<=;i++){
qwq[i]=qwq[i-]*i;
}
}
int main(){
init();
bfs();
char aa[];
char bb[];
//while(scanf("%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c",aa[0],aa[1],aa[2],aa[3],aa[4],aa[5],
// aa[6],&aa[7],&aa[8],)){
while(gets(aa)){
if(aa[]=='\0')break;
int tot=;
int len=strlen(aa);
for(int i=;i<len;i++){
if((aa[i]>=''&&aa[i]<='')){
bb[tot++]=aa[i];
}
if(aa[i]=='x'){
bb[tot++]='';
}
}
bb[tot]='\0';
int f=counter(bb);
// cout << f<<" "<<bb<<endl;
//cout <<f<<endl;46103
if(!v[f]){
printf("unsolvable\n");
}
else{
for(int i = f ; i != f0 ; i=vv[i]){
// cout << vv[<<endl;
printf("%c",sss[i]);
//system("pause");
}
printf("\n");
}
}
return ;
}
【HDOJ1043】【康拓展开+BFS】的更多相关文章
- hdu1430 魔板(康拓展开 bfs预处理)
		
魔板 Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...
 - ACM/ICPC 之 BFS(离线)+康拓展开 (HDU1430-魔板)
		
魔板问题,一道经典的康拓展开+BFS问题,为了实现方便,我用string类来表示字符串,此前很少用string类(因为不够高效,而且相对来说我对char数组的相关函数比较熟),所以在这里也发现了很多容 ...
 - bnuoj 1071 拼图++(BFS+康拓展开)
		
http://www.bnuoj.com/bnuoj/problem_show.php?pid=1071 [题意]:经过四个点的顺逆时针旋转,得到最终拼图 [题解]:康拓展开+BFS,注意先预处理,得 ...
 - hdoj1043 Eight(逆向BFS+打表+康拓展开)
		
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1043 思路: 由于自己对康拓展开用的太少,看到这个题没想到康拓展开,最开始打算直接转换为数字,但太占内 ...
 - ACM/ICPC 之 BFS(离线)+康拓展开(TSH OJ-玩具(Toy))
		
祝大家新年快乐,相信在新的一年里一定有我们自己的梦! 这是一个简化的魔板问题,只需输出步骤即可. 玩具(Toy) 描述 ZC神最擅长逻辑推理,一日,他给大家讲述起自己儿时的数字玩具. 该玩具酷似魔方, ...
 - hdu 1043  pku poj  1077  Eight (BFS + 康拓展开)
		
http://acm.hdu.edu.cn/showproblem.php?pid=1043 http://poj.org/problem?id=1077 Eight Time Limit: 1000 ...
 - 九宫重拍(bfs + 康拓展开)
		
问题描述 如下面第一个图的九宫格中,放着 1~8 的数字卡片,还有一个格子空着.与空格子相邻的格子中的卡片可以移动到空格中.经过若干次移动,可以形成第二个图所示的局面. 我们把第一个图的局面记为:12 ...
 - 8数码,欺我太甚!<bfs+康拓展开>
		
不多述,直接上代码,至于康拓展开,以前的文章里有 #include<iostream> #include<cstdio> #include<queue> using ...
 - 【算法系列学习三】[kuangbin带你飞]专题二 搜索进阶 之 A-Eight 反向bfs打表和康拓展开
		
[kuangbin带你飞]专题二 搜索进阶 之 A-Eight 这是一道经典的八数码问题.首先,简单介绍一下八数码问题: 八数码问题也称为九宫问题.在3×3的棋盘,摆有八个棋子,每个棋子上标有1至8的 ...
 
随机推荐
- Vue + Element UI 实现权限管理系统(更换皮肤主题)
			
自定义主题 命令行主题工具 1.安装主题工具 首先安装「主题生成工具」,可以全局安装或者安装在当前项目下,推荐安装在项目里,方便别人 clone 项目时能直接安装依赖并启动. yarn add ele ...
 - unity中键盘WASD控制。(WS控制物体前后移动,AD控制左右旋转。)
			
private float rotateSpeed = 30f; private float movespeed = 5; void FixedUpdate() { //第一种控制移动 float h ...
 - 利用awk处理学生成绩问题(难度较大)
			
学生成绩表单如下: Name,Team,First Test, Second Test, Third Test Tom,Red,,, Joe,Green,,, Maria,Blue,,, Fred,B ...
 - 洛谷 P4515 [COCI2009-2010#6] XOR
			
题意 平面直角坐标系中有一些等腰直角三角形,且直角边平行于坐标轴,直角顶点在右下方,求奇数次被覆盖的面积.N<=10.输入为x,y,r,分别表示三角形顶点的坐标与三角形的边长. 如: 总面积为0 ...
 - JavaScript 基础,登录前端验证
			
<script></script>的三种用法: 放在<body>中 放在<head>中 放在外部JS文件 <!DOCTYPE html> & ...
 - turtle
			
画一组同切圆 输入 import turtle turtle.color('red') turtle.circle(30) turtle.circle(60) turtle.circle(90) tu ...
 - CSS学习笔记-01-2D转换模块
			
首先,mark 一下 css3 新增 的 2D 转换之 W3school 的链接: http://www.w3school.com.cn/css3/css3_2dtransform.asp 转换是使 ...
 - centos7安装mysql mariadb
			
从最新版本的linux系统开始,默认的是 Mariadb而不是mysql! 使用系统自带的repos安装很简单: yum install -y mariadb mariadb-server 启动mar ...
 - Ubuntu下teamviewer的安装
			
安装i386库 1.sudo apt-get install libc6:i386 libgcc1:i386 libasound2:i386 libexpat1:i386 libfontconfig1 ...
 - nginx防DOS攻击
			
将 timeout 设低来防止 DOS 攻击 client_body_timeout 10; client_header_timeout 10; keepalive_timeout 5 5; send ...