Eight HDU-1043 (bfs)
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
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 <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)的更多相关文章
- 非常可乐---hdu 1495(BFS)
http://acm.hdu.edu.cn/showproblem.php?pid=1495 题意: 有3个杯子a b c:a=b+c:然后刚开始时只有a是满的,其它为空的,然后a b c三个之间互相 ...
- hdu 5012(bfs)
题意:给你2个 骰子,让你通过翻转使第一个变成第二个,求最少翻转数 思路:bfs #include<cstdio> #include<iostream> #include< ...
- 逃离迷宫 HDU - 1728(bfs)
逃离迷宫 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- Find a way HDU - 2612(bfs)
Find a way Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- 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 ...
- 牛客假日团队赛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 ...
- HDU.2612 Find a way (BFS)
HDU.2612 Find a way (BFS) 题意分析 圣诞节要到了,坤神和瑞瑞这对基佬想一起去召唤师大峡谷开开车.百度地图一下,发现周围的召唤师大峡谷还不少,这对基佬纠结着,该去哪一个...坤 ...
- 深搜(DFS)广搜(BFS)详解
图的深搜与广搜 一.介绍: p { margin-bottom: 0.25cm; direction: ltr; line-height: 120%; text-align: justify; orp ...
- 【算法导论】图的广度优先搜索遍历(BFS)
图的存储方法:邻接矩阵.邻接表 例如:有一个图如下所示(该图也作为程序的实例): 则上图用邻接矩阵可以表示为: 用邻接表可以表示如下: 邻接矩阵可以很容易的用二维数组表示,下面主要看看怎样构成邻接表: ...
- 深度优先搜索(DFS)与广度优先搜索(BFS)的Java实现
1.基础部分 在图中实现最基本的操作之一就是搜索从一个指定顶点可以到达哪些顶点,比如从武汉出发的高铁可以到达哪些城市,一些城市可以直达,一些城市不能直达.现在有一份全国高铁模拟图,要从某个城市(顶点) ...
随机推荐
- <随便写>佛祖,哈哈!
print(" _ooOoo_ ") print(" o8888888o ") print(" 88 . 88 ") print(" ...
- gulpfile.js demo
var config = require("./build.config") //获取build.config.js文件里的内容 var gulp = require(" ...
- Tornado demo3 - tcpecho分析
在这个demo中,主要是使用了Tornado中异步的TCP client和server来实现一个简单的echo效果(即客户端发送的message会从server端返回到client).代码的githu ...
- win 7下安装mysql zip格式
mysql 下载地址:https://dev.mysql.com/downloads/mysql/5.5.html#downloads 下载的mysql格式为zip: 下载完成放在除C盘以外的盘. 一 ...
- 基于Skyline与ArcGIS Server的二三维联动功能实现
基于Skyline与ArcGIS Server的二三维联动功能实现主要利用WEB技术.ArcGIS for JavaScript.Skyline 二次开发以及ArcGIS 10.1 桌面工具. 利用A ...
- CentOS 6.5 Apache、MySQL、PHP环境配置(LAMP)
yum -y install httpd mysql-server php #安装apache.mysql和PHP yum -y install php-mysql php-gd php-mbstri ...
- [Day4] Nginx Http模块一
之前介绍了Nginx作为静态资源服务器的用法,除此之外,Nginx更多的场景是作为反向代理服务器,提高网站的并发和可用性.下面几节着重说一下作为反向代理的http模块,并且了解一些Nginx的架构. ...
- Django项目:CRM(客户关系管理系统)--21--13PerfectCRM实现King_admin分页页数
{#table_data_list.html#} {## ————————08PerfectCRM实现King_admin显示注册表的字段表头————————#} {% extends 'king_m ...
- MAC中怎么安装python
转自:https://blog.csdn.net/hou_manager/article/details/79555809 一.Python 介绍 Python介绍 Python3在2008年12月3 ...
- 一次完整的HTTP事务的过程、从输入URL到网页展示,浏览器都经历了什么?
详细介绍:老生常谈-从输入url到页面展示到底发生了什么 (1)一次完整的HTTP事务的过程 基本流程: a. 域名解析 b. 发起TCP的3次握手 c. 建立TCP连接后发起http请求 d. 服务 ...