POJ 1077 八数码 BFS +A*(复习的时候看过来!这道题值的看啊啊啊~!!!)

来源:互联网 发布:知乎联合创始人张亮 编辑:程序博客网 时间:2024/05/22 06:14

D - Eight
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
Submit Status

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

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.

Sample Input

 2  3  4  1  5  x  7  6  8 

Sample Output

ullddrurdllurdruldr


题目大意:自己看啦


提交情况:T T T TT T T T T T T T T T T TT T AC...


目前只是用了BFS做了,有空要把A*补上

该说的都在注释里面

这里说一句,如果循环很多次的话,一个微小的声明的区别都会导致250ms的差别,

只能说姿势很重要,如果姿势不过关只能考研自己的人品看看能不能卡着数据过了


下面是BFS的AC代码


#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<queue>#include<vector>#include<cmath>#define aim 0using namespace std;bool cantor[400000];int fac[10]={1,1,2,6,24,120,720,5040,40320,362880};//0!1!2!3!4!  5!  6!   7!  8!     9!int xd[4]={0,0,-1,1},yd[4]={-1,1,0,0};   //  移动方向上下左右,与下面的dir相意义对应char dir[4]={'u','d','l','r'};    //上下左右struct node{int s[10];int pos,a,b;int state;string str;node(){        memset(s,0,sizeof(s));    }}start,e;int hashint(int C[]){    int i,j;    int sum=0;    for(i=0;i<9;i++){        int res=0;        for(j=i+1;j<9;j++){            if(C[j]<C[i]){                res++;            }        }        sum+=res*fac[8-i];    }    return sum;}int main(){    int x;    char ch;    for(int k=0;k<9;){        scanf("%c",&ch);        if(ch=='x'){            start.s[k]=9;            start.pos=k++;        }        else if(ch<='9'&&ch>='1'){            start.s[k++]=ch-'0';        }    }    start.a=start.pos%3;    start.b=start.pos/3;    memset(cantor,false,sizeof(cantor));    start.state=hashint(start.s);    cantor[start.state]=1;    queue<node> q;    q.push(start);    while(!q.empty()){        node cur=q.front();        /*这道题果真还是应该用A*来解,        单纯bfs的时间卡得非常的紧,        牛神拿了我原来T的代码看了之后就按照我的思路敲了一遍结果AC了,        经过我无数遍的比对,发现问题就在这一句,如果使用这一句来申明队列,        那么提交时间是750ms,如果使用下面那两句,则是T,        这个真的是看人品...只能说出题人是想要卡bfs的只是卡得还不够紧。        看来还是姿势第一啊。没有姿势就只能赌人品了        */        /*node cur;        cur=q.front();*/        q.pop();        for(int i=0;i<4;i+=1){            int x,y;            x=cur.a+xd[i],y=cur.b+yd[i];//数组不越界            if(x<3&&y<3&&x>=0&&y>=0){                 node tempt=cur;                 tempt.s[y*3+x]=cur.s[cur.b*3+cur.a];                 tempt.s[cur.b*3+cur.a]=cur.s[y*3+x];                 tempt.state=hashint(tempt.s);                 if(!cantor[tempt.state]){                     cantor[tempt.state]=true;                     tempt.str=cur.str+dir[i];                     tempt.a=x;                     tempt.b=y;                     if(tempt.state==aim){                        e=tempt;                        cout<<e.str<<endl;                        return 0;                     }                     q.push(tempt);                  }             }        }    }    cout<<"unsolvable"<<endl;    return 0;}


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 大把掉发头发稀少怎么办 头发掉厉害洗生发灵更掉怎么办? 额头两侧和头顶头发少怎么办 额头前面头发少怎么办天生的 头两边的碎头发怎么办 30岁后头发少怎么办 生完孩子头发少怎么办 1岁宝宝头发少怎么办 头顶上的头发少怎么办呢 22岁头顶头发稀少怎么办 头发又稀又少怎么办 头顶的头发越来越少怎么办 四岁宝宝头发稀少怎么办 一岁宝宝头发稀少怎么办 三岁宝宝头发稀少怎么办 头发油急着出门怎么办 长出的头发毛糙弯曲怎么办 头发薄还掉头发怎么办 头发少掉的厉害怎么办 一洗头就掉很多头发怎么办 生完孩子掉头发怎么办 甲减引起的肥胖怎么办 18岁掉头发严重怎么办 5岁儿童掉头发严重怎么办 18头发掉的严重怎么办 甲癌碘131后腮腺肿大怎么办 头发出油发丝细怎么办 25岁总掉头发怎么办 25岁掉头发厉害怎么办 我25岁经常掉头发怎么办 25岁掉头发很厉害怎么办 我今年25岁掉头发怎么办 头顶头发稀少怎么办有方法吗 头发又干又黄怎么办 2岁宝宝头发细软怎么办 头发突然变得稀疏了怎么办 孕期掉头发很厉害怎么办 冬天头发掉的厉害怎么办 怀孕期间掉头发比较严重怎么办 头发油腻容易掉发怎么办 最近头发掉的厉害怎么办