蓝桥杯历届试题——九宫重排(bfs)

来源:互联网 发布:国内腊肉市场销售数据 编辑:程序博客网 时间:2024/05/22 05:20

问题描述
  如下面第一个图的九宫格中,放着 1~8 的数字卡片,还有一个格子空着。与空格子相邻的格子中的卡片可以移动到空格中。经过若干次移动,可以形成第二个图所示的局面。
这里写图片描述这里写图片描述
  我们把第一个图的局面记为:12345678.
  把第二个图的局面记为:123.46758
  显然是按从上到下,从左到右的顺序记录数字,空格记为句点。
  本题目的任务是已知九宫的初态和终态,求最少经过多少步的移动可以到达。如果无论多少步都无法到达,则输出-1。
输入格式
  输入第一行包含九宫的初态,第二行包含九宫的终态。
输出格式
  输出最少的步数,如果不存在方案,则输出-1。
样例输入
12345678.
123.46758
样例输出
3
样例输入
13524678.
46758123.
样例输出
22

明显要用搜索,我这里用了bfs
一定要剪掉许多不可行的情况,比如某个排列曾经出现的话就不用再继续搜了,寻找下一种情况的时候也不用急着放进队列,有可能这时就已经找到了答案,如果还要放进队列就又会多出许多种情况

#include <iostream>#include <cstring>#include <string>#include <vector>#include <queue>#include <cstdio>#include <set>#include <math.h>#include <algorithm>#include <queue>#define INF 0x3f3f3f3f#define MAXN 100005#define Mod 1000000007using namespace std;char hajime[5][5],owali[5][5];struct Node{    int x,y;    long long step;    char pre;    char map[5][5];};bool check(Node a){    for(int i=0; i<3; ++i)        for(int j=0; j<3; ++j)            if(a.map[i][j]!=owali[i][j])                return false;    return true;}set<string> hsh;bool getvis(Node a){    string tmp="";    for(int i=0; i<3; ++i)        for(int j=0; j<3; ++j)        {            tmp+=a.map[i][j];        }    if(hsh.find(tmp)!=hsh.end())        return false;    hsh.insert(tmp);    return true;}bool bfs(int x,int y){    Node start;    start.x=x,start.y=y;    start.step=0;    start.pre='X';    for(int i=0; i<3; ++i)        for(int j=0; j<3; ++j)            start.map[i][j]=hajime[i][j];    queue<Node> q;    q.push(start);    while(!q.empty())    {        Node tmp=q.front(),tmp1;        q.pop();        if(check(tmp))        {            cout<<tmp.step<<endl;            return true;        }        tmp1=tmp;        if(tmp1.pre!='U'&&tmp1.x+1<3)        {            tmp1.step++;            tmp1.pre='D';            swap(tmp1.map[tmp1.x][tmp1.y],tmp1.map[tmp1.x+1][tmp1.y]);            tmp1.x++;            if(getvis(tmp1))            {                if(check(tmp1))                {                    cout<<tmp1.step<<endl;                    return true;                }                q.push(tmp1);            }        }        tmp1=tmp;        if(tmp1.pre!='D'&&tmp1.x-1>=0)        {            tmp1.step++;            tmp1.pre='U';            swap(tmp1.map[tmp1.x][tmp1.y],tmp1.map[tmp1.x-1][tmp1.y]);            tmp1.x--;            if(getvis(tmp1))            {                if(check(tmp1))                {                    cout<<tmp1.step<<endl;                    return true;                }                q.push(tmp1);            }        }        tmp1=tmp;        if(tmp1.pre!='L'&&tmp1.y+1<3)        {            tmp1.step++;            tmp1.pre='R';            swap(tmp1.map[tmp1.x][tmp1.y],tmp1.map[tmp1.x][tmp1.y+1]);            tmp1.y++;            if(getvis(tmp1))            {                if(check(tmp1))                {                    cout<<tmp1.step<<endl;                    return true;                }                q.push(tmp1);            }        }        tmp1=tmp;        if(tmp1.pre!='R'&&tmp1.y-1>=0)        {            tmp1.step++;            tmp1.pre='L';            swap(tmp1.map[tmp1.x][tmp1.y],tmp1.map[tmp1.x][tmp1.y-1]);            tmp1.y--;            if(getvis(tmp1))            {                if(check(tmp1))                {                    cout<<tmp1.step<<endl;                    return true;                }                q.push(tmp1);            }        }    }    return false;}int main(){    char tmp[100];    scanf("%s",tmp);    int x,y;    for(int i=0; tmp[i]!='\0'; ++i)    {        hajime[i/3][i%3]=tmp[i];        if(tmp[i]=='.')        {            x=i/3;            y=i%3;            hajime[i/3][i%3]='0';        }    }    scanf("%s",tmp);    for(int i=0; tmp[i]!='\0'; ++i)    {        owali[i/3][i%3]=tmp[i];        if(tmp[i]=='.')            owali[i/3][i%3]='0';    }    if(!bfs(x,y))        cout<<-1<<endl;    return 0;}
1 0
原创粉丝点击