链接:https://ac.nowcoder.com/acm/problem/51032
来源:牛客网
注意:题目输出不是唯一的,即这ulrd四个字母没有优先级!如果你的代码写错了,并不是优先级的问题,而是其他地方写错了!
来源:牛客网
题目描述
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:
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 4 5 6 7 8 9 10 11 12 13 14 15 xwhere 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.
输入描述:
You will receive a description of a configuration of the 8 puzzle. The 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
输出描述:
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.
还有就是记得无解时输出"unsolvable"(不含引号)
思路
看到这个数据范围是3*3(相当于n==9),再看到题目每次移动相当于从上下左右四个方向走一遍-->BFS求解
(如果不懂BFS的大致模板,请自学)
来看看判断条件
1.BFS结束执行的条件,即变成“123456789”的样子(本来是“12345678x”的,但是这里我们发现,12345678是数字,x是字符,所以可以把x读入时变成9,相当于把字符串比较转化成了数字比较,相对方便一些)
2.每次BFS搜索的位置:上下左右,上为u,下为d,左为l,右为r,依次考虑即可
3.每次BFS后是否要回溯原来的点:是,因为部分格子存在重复经过的情况
4.是否需要剪枝:是,因为可能会出现循环移动的情况(比如说右下左上)
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
明白了这些判断条件,我们看看,如何将这些条件转化成代码(可以结合下面的代码食用)
先看第二个条件,很简单,先开一个方向数组(代码中的dir数组),然后for循环每个方向就行,记得判断位置坐标是否合法。
再看第一个条件,由于在队列中每一次都会存储当前的mp的状态,所以每次判断是否结束时,直接把这个状态拿出来,与123456789直接比较是否相等即可,只有当某一次相等了才说明有解。
如果队列中的状态已经全部遍历完了还没有解,那么必定无解。
再看第三个条件,由于一开始输入的数字存在了mp数组里,相当于一张图,所以需要将图中的指定坐标交换(即相当于执行移动操作)之后,先转化成数字串(不是字符串!)的形式,方便存入队列中;之后拿出来使用时,再转化成图的形式即可。
下面的代码中的zh()函数即为图到数字串的转化,zhn()函数即为数字串到图的转化
最后看剪枝,这里其实用map或者set都行(因为set中的元素不会重复嘛),下面代码里用的是map,大致思路为:每次看看map中已经存储的数字,是否为swap()之后的新数字,不是的话,就可以将这个数字存入队列中,是的话就不存,这样剪枝,有效的避免了死循环的发生。
好了,差不多分析完了,可以开始写代码了
AC代码如下
(与其他题解不同,没有哈希,unordered_map是<int,int>型的)
#include<bits/stdc++.h>
using namespace std;
struct node {
int num,x,y;
string move;
};
queue<node>q;
unordered_map<int,int>d;
int mp[4][4];
int dir[4][2]= {{1,0},{0,1},{-1,0},{0,-1}};
string ans;
int zh() {
int sum=0;
for(int i=1; i<=3; i++) {
for(int j=1; j<=3; j++) {
sum=sum*10+mp[i][j];
}
}
return sum;
}
void zhn(int n){
int sum=n;
for(int i=3;i>=1;i--){
for(int j=3;j>=1;j--){
mp[i][j]=(sum%10);
sum/=10;
}
}
}
string BFS() {
while(!q.empty()) {
node tmp=q.front();
q.pop();
if(tmp.num==123456789) {
return tmp.move;
}
zhn(tmp.num);
for(int i=0; i<4; i++) {
int xx=tmp.x+dir[i][0];
int yy=tmp.y+dir[i][1];
if(xx<1 || xx>3 || yy<1 || yy>3) continue;
swap(mp[tmp.x][tmp.y],mp[xx][yy]);
int nowtmp=zh();
if(!d.count(nowtmp)) {
d[nowtmp]++;
if(i==0) q.push(node {nowtmp,xx,yy,tmp.move+"d"});
if(i==1) q.push(node {nowtmp,xx,yy,tmp.move+"r"});
if(i==2) q.push(node {nowtmp,xx,yy,tmp.move+"u"});
if(i==3) q.push(node {nowtmp,xx,yy,tmp.move+"l"});
}
swap(mp[tmp.x][tmp.y],mp[xx][yy]);
}
}
cout<<"unsolvable"<<endl;
return " ";
}
int main() {
for(int i=1; i<=3; i++) {
for(int j=1; j<=3; j++) {
char tmp;
cin>>tmp;
if(tmp=='x') mp[i][j]=9;
else mp[i][j]=(tmp-48);
}
}
for(int i=1; i<=3; i++) {
for(int j=1; j<=3; j++) {
if(mp[i][j]==9) {
int sumtt=zh();
q.push(node {sumtt,i,j,""});
ans=BFS();
}
}
}
if(ans!=" ") cout<<ans<<endl;
return 0;
}
using namespace std;
struct node {
int num,x,y;
string move;
};
queue<node>q;
unordered_map<int,int>d;
int mp[4][4];
int dir[4][2]= {{1,0},{0,1},{-1,0},{0,-1}};
string ans;
int zh() {
int sum=0;
for(int i=1; i<=3; i++) {
for(int j=1; j<=3; j++) {
sum=sum*10+mp[i][j];
}
}
return sum;
}
void zhn(int n){
int sum=n;
for(int i=3;i>=1;i--){
for(int j=3;j>=1;j--){
mp[i][j]=(sum%10);
sum/=10;
}
}
}
string BFS() {
while(!q.empty()) {
node tmp=q.front();
q.pop();
if(tmp.num==123456789) {
return tmp.move;
}
zhn(tmp.num);
for(int i=0; i<4; i++) {
int xx=tmp.x+dir[i][0];
int yy=tmp.y+dir[i][1];
if(xx<1 || xx>3 || yy<1 || yy>3) continue;
swap(mp[tmp.x][tmp.y],mp[xx][yy]);
int nowtmp=zh();
if(!d.count(nowtmp)) {
d[nowtmp]++;
if(i==0) q.push(node {nowtmp,xx,yy,tmp.move+"d"});
if(i==1) q.push(node {nowtmp,xx,yy,tmp.move+"r"});
if(i==2) q.push(node {nowtmp,xx,yy,tmp.move+"u"});
if(i==3) q.push(node {nowtmp,xx,yy,tmp.move+"l"});
}
swap(mp[tmp.x][tmp.y],mp[xx][yy]);
}
}
cout<<"unsolvable"<<endl;
return " ";
}
int main() {
for(int i=1; i<=3; i++) {
for(int j=1; j<=3; j++) {
char tmp;
cin>>tmp;
if(tmp=='x') mp[i][j]=9;
else mp[i][j]=(tmp-48);
}
}
for(int i=1; i<=3; i++) {
for(int j=1; j<=3; j++) {
if(mp[i][j]==9) {
int sumtt=zh();
q.push(node {sumtt,i,j,""});
ans=BFS();
}
}
}
if(ans!=" ") cout<<ans<<endl;
return 0;
}