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 ;
}

最新文章

  1. clover 在win10下工作不正常
  2. 【转】OPENGL基本API概述
  3. ruby + watir 自动化上传图片文件解决方案
  4. 想要愉快入住酒店?缺了它还真不行!(含PPT)
  5. Android中空格及换行
  6. 使用PHPExcel报错 Fatal error: Class &#39;ZipArchive&#39; not found
  7. Ubuntu 14.04远程登录服务器--openssh的安装和配置简明步骤
  8. CDN云主机与传统虚拟主机功能对比
  9. stack 集合栈计算机 (摘)
  10. cornerstone the working copy is locked due to a previous文件lock解决办法
  11. 对web.config加密,和解密码详细说明
  12. JVM基础(4)-编译
  13. 性能测试平台效率优化的一次经验(python版)
  14. 最长上升子序列(LIS) dp学习~3
  15. 【Unity3D】Unity3D开发《我的世界》之七、番外篇
  16. Xshell与虚拟机不能正常连接
  17. 【开源】OSharpNS,轻量级.net core快速开发框架发布
  18. JSOI2019 Round2 极限生还
  19. Spring Boot 启动:No active profile set, falling back to default profiles: default
  20. nginx的proxy_pass路径转发规则最后带/问题

热门文章

  1. Pandas对于CSV的简单操作
  2. Activiti流程定义部署、删除
  3. Install- Linux必学的60个命令
  4. 全栈之路-杂篇-前端Http请求封装优化
  5. Nginx 的常用命令
  6. spring定时任务scheduler集群环境下指定运行服务器防止多服务器多次执行
  7. umask权限使用
  8. Win10操作系统安装—U盘作为启动盘—系统安装到固态硬盘中
  9. Luogu P3295 [SCOI2016]萌萌哒(并查集+倍增)
  10. Spring Cloud Alibaba迁移指南(一):一行代码从 Hystrix 迁移到 Sentinel