Eight

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 35625    Accepted Submission(s): 9219
Special Judge

Problem Description
The 15-puzzle has been around for over 100 years; even if you don't know it by that name, you've seen it. It is constructed with 15 sliding tiles, each with a number from 1 to 15 on it, and all packed into a 4 by 4 frame with one tile missing. Let's call the missing tile 'x'; the object of the puzzle is to arrange the tiles so that they are ordered as:

 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.

 
Input
You will receive, several descriptions of configuration of the 8 puzzle. One description is just a list of the tiles in their initial positions, with the rows listed from top to bottom, and the tiles listed from left to right within a row, where the tiles are represented by numbers 1 to 8, plus 'x'. For example, this puzzle

1 2 3 
x 4 6 
7 5 8

is described by this list:

1 2 3 x 4 6 7 5 8

 
Output
You will print to standard output either the word ``unsolvable'', if the puzzle has no solution, or a string consisting entirely of the letters 'r', 'l', 'u' and 'd' that describes a series of moves that produce a solution. The string should include no spaces and start at the beginning of the line. Do not print a blank line between cases.
 
Sample Input
2 3 4 1 5 x 7 6 8
 
Sample Output
ullddrurdllurdruldr
 
思路:使用一维的string来表示当前局面,下标从0开始。
     但是代码超内存,可能是由于无解的情况导致的,待更新......
 #include <iostream>
#include <queue>
#include <cstring>
#include <cstdio>
#include <string>
#include <algorithm>
#include <set>
#include <map> using namespace std; map<string, char> mp; // 存储当前局面和方向
map<string, string>pre; // 存储当前局面和上一局面 int flag = ; struct node
{
int cur; // x在string中的下标
string s; // 当前局面
}nod; string Swap(string s, int x, int y)
{
swap(s[x], s[y]);
return s;
} void Print(string str) // 递归打印结果
{
if(mp[str] == '#')
return;
Print(pre[str]);
cout << mp[str];
} void bfs()
{
queue<node> Q; Q.push(nod);
mp[nod.s] = '#'; node p,t;
while(!Q.empty())
{
p = Q.front();
Q.pop(); if(p.s == "12345678x")
{
flag = ;
Print("12345678x");
} for(int i = ; i < ; ++i)
{
if(i == ) // 向左
{
if(p.cur % != ) // 下标为0,3,6的不能向左移动
{
t.s = Swap(p.s, p.cur, p.cur-);
if(mp.count(t.s) == )
{
mp[t.s] = 'l';
pre[t.s] = p.s;
t.cur = p.cur - ;
Q.push(t);
} }
}
else if(i == ) // 向右
{
if(p.cur % != ) // 下标为2,5,8的不能向右移动
{
t.s = Swap(p.s, p.cur, p.cur+);
if(mp.count(t.s) == )
{
mp[t.s] = 'r';
pre[t.s] = p.s;
t.cur = p.cur + ;
Q.push(t);
} }
}
else if(i == ) // 向上
{
if(p.cur > ) // 下标为0,1,2的不能向上移动
{
t.s = Swap(p.s, p.cur, p.cur-);
if(mp.count(t.s) == )
{
mp[t.s] = 'u';
pre[t.s] = p.s;
t.cur = p.cur - ;
Q.push(t);
} }
}
else if(i == ) // 向下
{
if(p.cur < ) // 下标为6,7,8的不能向下移动
{
t.s = Swap(p.s, p.cur, p.cur+);
if(mp.count(t.s) == )
{
mp[t.s] = 'd';
pre[t.s] = p.s;
t.cur = p.cur + ;
Q.push(t);
}
}
} }
}
} int main()
{ char c;
string str;
int k;
for(int i = ; i < ; ++i)
{
cin >> c;
str += c;
if(c == 'x')
k = i; // 记录x的初始下标
} nod.s = str;
nod.cur = k; bfs();
if(flag == )
cout << "unsolvable";
cout << endl; return ;
}

Eight HDU-1043 (bfs)的更多相关文章

  1. 非常可乐---hdu 1495(BFS)

    http://acm.hdu.edu.cn/showproblem.php?pid=1495 题意: 有3个杯子a b c:a=b+c:然后刚开始时只有a是满的,其它为空的,然后a b c三个之间互相 ...

  2. hdu 5012(bfs)

    题意:给你2个 骰子,让你通过翻转使第一个变成第二个,求最少翻转数 思路:bfs #include<cstdio> #include<iostream> #include< ...

  3. 逃离迷宫 HDU - 1728(bfs)

    逃离迷宫 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  4. Find a way HDU - 2612(bfs)

    Find a way Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  5. ACM-ICPC 2017 沈阳赛区现场赛 G. Infinite Fraction Path && HDU 6223(BFS)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6223 参考题解:https://blog.csdn.net/qq_40482495/article/d ...

  6. 牛客假日团队赛5 L Catch That Cow HDU 2717 (BFS)

    链接:https://ac.nowcoder.com/acm/contest/984/L 来源:牛客网 Catch That Cow 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 3 ...

  7. HDU.2612 Find a way (BFS)

    HDU.2612 Find a way (BFS) 题意分析 圣诞节要到了,坤神和瑞瑞这对基佬想一起去召唤师大峡谷开开车.百度地图一下,发现周围的召唤师大峡谷还不少,这对基佬纠结着,该去哪一个...坤 ...

  8. 深搜(DFS)广搜(BFS)详解

    图的深搜与广搜 一.介绍: p { margin-bottom: 0.25cm; direction: ltr; line-height: 120%; text-align: justify; orp ...

  9. 【算法导论】图的广度优先搜索遍历(BFS)

    图的存储方法:邻接矩阵.邻接表 例如:有一个图如下所示(该图也作为程序的实例): 则上图用邻接矩阵可以表示为: 用邻接表可以表示如下: 邻接矩阵可以很容易的用二维数组表示,下面主要看看怎样构成邻接表: ...

  10. 深度优先搜索(DFS)与广度优先搜索(BFS)的Java实现

    1.基础部分 在图中实现最基本的操作之一就是搜索从一个指定顶点可以到达哪些顶点,比如从武汉出发的高铁可以到达哪些城市,一些城市可以直达,一些城市不能直达.现在有一份全国高铁模拟图,要从某个城市(顶点) ...

随机推荐

  1. php 7.2下mcrypt扩展支持附带的问题

    按照网上提供的mcrypt扩展编译支持方法,完成了扩展编译,也确实可以正常加密/解密了 但是如果php.ini中配置为: error_reporting = E_ALL display_errors ...

  2. 封装了一个HOOKAPI的类。。。

    我惊奇地发现,我手竟然这么生了.....   很久很久没有写这方面的东西,现在写着竟然这么手生....哎....   这玩艺,还得天天练....

  3. Java超简明入门学习笔记(零)

    Java编程思想第4版学习笔记(零) 前言          这个笔记本主要记录了我在学习Java编程思想(第4版,中文版)的过程中遇到的重难点及其分析.主要参考了C++11版本的C++语言,对比了它 ...

  4. Python实现单神经元分类图片的训练

    1.加载包和数据 numpy is the fundamental package for scientific computing with Python. h5py is a common pac ...

  5. extern “C”的用法

    引言 由于不同的代码互相调用起来很容易出错,甚至同一种代码但由不同的编译器编译,为实现C++代码调用其他C语言代码,会在C语言代码的部分加上extern "C",表明这段代码需要按 ...

  6. 矩阵快速幂3 k*n铺方格

    #include <iostream> #include <cstdlib> #include <cstring> #include <queue> # ...

  7. windows下docker 启动jenkins成功,浏览器无法访问,拒绝了我们的连接

    [问题现象] 在Windows下使用docker启动了一个jenkins,翻越了无数的坑,最后的启动命令为 docker run --name jenkins -u root -p 8000:8000 ...

  8. jeecms v9导入myeclipse 2015 ehcache.xml报错问题

    1.找不到ehcache.xml文件问题 cache-context.xml <property name="configLocation"> <value> ...

  9. 转:PLL 锁相环

    原地址:http://fangjian0518.blog.163.com/blog/static/559196562011210103455430/  PLL的作用? 答:LPC2000系列ARM内部 ...

  10. IO流16 --- 对象流操作字符串 --- 技术搬运工(尚硅谷)

    序列化 @Test public void test12() throws IOException { ObjectOutputStream oos = new ObjectOutputStream( ...