【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的 ...
随机推荐
- python 自然语言处理(二)____获得文本语料和词汇资源
一, 获取文本语料库 一个文本语料库是一大段文本.它通常包含多个单独的文本,但为了处理方便,我们把他们头尾连接起来当做一个文本对待. 1. 古腾堡语料库 nltk包含古腾堡项目(Project Gut ...
- learning scala 数组和容器
数组:可变的,可索引的,元素具有相同类型的数据集合 一维数组 scala> val intValueArr = new Array[Int](3)intValueArr: Array[Int] ...
- vue安装与配置
直接引入 <script src="https://unpkg.com/vue"></script> 用npm安装 $ npm install vue ...
- sass嵌套风格
1.嵌套输出方式 nested Sass 提供了一种嵌套显示 CSS 文件的方式.例如 nav { ul { margin:; padding:; list-style: none; } li { d ...
- python获取代理IP
利用requests库获取代理,用Beautiful库解析网页筛选ip # -*- coding: utf- -*- import requests from bs4 import Beautiful ...
- VCL界面控件DevExpress VCL Controls发布v18.2.5|附下载
DevExpress VCL Controls是 Devexpress公司旗下最老牌的用户界面套包.所包含的控件有:数据录入,图表,数据分析,导航,布局,网格,日程管理,样式,打印和工作流等,让您快速 ...
- DevExpress WinForms v18.2新版亮点(八)
买 DevExpress Universal Subscription 免费赠 万元汉化资源包1套! 限量15套!先到先得,送完即止!立即抢购>> 行业领先的.NET界面控件2018年第 ...
- DevExpress v18.1新版亮点——DevExtreme篇(二)
用户界面套包DevExpress v18.1日前终于正式发布,本站将以连载的形式为大家介绍各版本新增内容.本文将介绍了DevExtreme JavaScript Controls v18.1 的新功能 ...
- Linux(centos) 下curl模拟Http get / post请求 [ curl ]
一.get请求 curl "http://www.baidu.com" 如果这里的URL指向的是一个文件或者一幅图都可以直接下载到本地 curl -i "http:// ...
- normalization 阅读笔记
https://zhuanlan.zhihu.com/p/33173246 阅读笔记 1. normalization whiting - PCA 2. Internal Covariate Shif ...