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. 唤起qqApp

    <a href="tencent://message/?uin=xxxxxxxxxxxxxx&Site=400301.com&Menu=yes">联系客 ...

  2. 实战课堂 | MongoDB如何使用内存?内存满了怎么破?

    最近接到多个MongoDB内存方面的线上case及社区问题咨询,主要集中在: 为什么我的 MongoDB 使用了 XX GB 内存? 一个机器上部署多个 Mongod 实例/进程,WiredTiger ...

  3. 深入浅出 Java Concurrency - 目录 [转]

    这是一份完整的Java 并发整理笔记,记录了我最近几年学习Java并发的一些心得和体会. J.U.C 整体认识 原子操作 part 1 从AtomicInteger开始 原子操作 part 2 数组. ...

  4. Java虚拟机原理图解-- 1.2.2、Class文件中的常量池详解(上)[转]

    NO1.常量池在class文件的什么位置? 我的上一篇文章<Java虚拟机原理图解> 1.class文件基本组织结构中已经提到了class的文件结构,在class文件中的魔数.副版本号.主 ...

  5. JEECMS自定义标签

    查看JEECMS的源代码发现开发者版本还没有类似现成的统计标签,一种解决的办法是使用现有的JEECMS标签,像这样Struts( [@cms_content_list channel=id]${tag ...

  6. Java常用的数据结构

    collection : List:arrayList,linkedList,vector set:treeSet ,hashSet; map: hashMap treeMap linkedHashM ...

  7. webServices学习四(---WebService监听工具)

    之前我们使用过HttpWatch获取的HTTP的调用过程,并获得了HTTP的请求头及其他请求的详细信息. 既然WebServie也是通过HTTP进行通信的,能不使用HTTPWatch来获取它的请求过程 ...

  8. 【Java爬虫】爬取南通大学教务处成绩

    没使用自动登录,所以获取是比较麻烦.. 1.http://jwgl.ntu.edu.cn/cjcx    进入官网,进行账号密码登录 2.点击全部成绩查询(也一定要点进去,不然cookie不会返回值) ...

  9. macOS下安装openCV+Xcode配置

    macOS下安装openCV+Xcode配置打开终端 /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Hom ...

  10. 安装mysql-workbench

    sudo apt-get install mysql-workbench