2x3移动格子和九宫格问题

2X3移动格子:

2×3=6个方格中放入ABCDE五个字母,右下角的那个格空着。如图所示。

《2x3移动格子和九宫格问题》

和空格子相邻的格子中的字母可以移动到空格中,比如,图中的C和E就可以移动,移动后的局面分别是:

A B
D E C

A B C
D E

为了表示方便,我们把6个格子中字母配置用一个串表示出来,比如上边的两种局面分别表示为:

AB*DEC
ABCD*E

题目的要求是:请编写程序,由用户输入若干表示局面的串,程序通过计算,输出是否能通过对初始状态经过若干次移动到达该状态。可以实现输出1,否则输出0。初始状态为:ABCDE*

用户输入的格式是:先是一个整数n,表示接下来有n行状态。程序输出也应该是n行1或0

例如,用户输入:

3
ABCDE*
AB*DEC
CAED*B

则程序应该输出:
1
1
0


//#include<stdio.h>
//#include<string>
//#include<queue>
//#include<map>
//#include<algorithm>
//using namespace std;
//string a,s=”ABCDE*”;
//map<string,int> Map;
//int dx[]= {-1,0,1,0};
//int dy[]= {0,1,0,-1};
//int bfs()
//{
//    int i,j;
//    string str1,str2;
//    queue<string> q;
//    q.push(a);
//    Map[a]=1;
//    while(!q.empty())
//    {
//        str1=q.front();
//             q.pop();
//        if(str1.compare(s)==0)
//            return 1;
//        for(j=0; j<6; j++)
//            if(str1[j]==’*’)
//                break;
//        int x1=j/3;
//        int y1=j%3;
//        int xzb;
//        for(i=0; i<4; i++)
//        {
//            int x2=x1+dx[i];
//            int y2=y1+dy[i];
//            if(x2>0 && x2<3 && y2>0 && y2<2)
//            {
//                xzb=x2*3+y2;
//                str2=str1;
//                str2[xzb]=str1[j];
//                str2[j]=str1[xzb];
//                if(Map[str2]!=1)
//                {
//                    q.push(str2);
//                    Map[str2]=1;
//                }
//            }
//        }
//    }
//    return 0;
//}
//int main()
//{
//    int t;
//    char b[7];
//    scanf(“%s”,b);
//    a=b;
//    t=bfs();
//    printf(“%d\n”,t);
//    return 0;
//}
//


九宫格问题:

编号为1 ~ 8的8个正方形滑块被摆成3行3列(有一个格子留空),每次可以把与空格相邻的滑块移到空格中,而它原来的位置变为空格。给定初始局面和目标局面(用0表示空格),编写代码计算出最少移动的步数。


#include<stdio.h>
#include<string>
#include<queue>
#include<map>
#include<algorithm>
using namespace std;
string a,s;
int dist[1000000];
int dx[]= {-1,0,1,0};
int dy[]= {0,1,0,-1};
int bfs()
{
    int i,j;
    string str1,str2;
    queue<string> q;
    map<string,int> Map;
    q.push(a);
    //memset(dist,0,sizeof(dist));
    for(int i=0;i<1000000;i++)
     dist[i]=0;
    int step=0,end=1;
    while(!q.empty())
    {
        str1=q.front();
        q.pop();
        if(str1.compare(s)==0)
            return dist[step];
        for(j=0; j<9; j++)
            if(str1[j]==’0′)
                break;
        int x1=j/3;
        int y1=j%3;
        int xzb;
        for(i=0; i<4; i++)
        {
            int x2=x1+dx[i];
            int y2=y1+dy[i];
            if(x2>=0 && x2<3 && y2>=0 && y2<3)
            {
                xzb=x2*3+y2;
                str2=str1;
                str2[xzb]=str1[j];
                str2[j]=str1[xzb];
                //在前一种状态下加1
                dist[end]=dist[step]+1;
                //如果这种状态已出现过了,就不需需入队列了.
                if(Map[str2]!=1)
                {
                    q.push(str2);
                    Map[str2]=1;
                    end++;
                }
            }
        }
        step++;
    }
    return -1;
}
int main()
{
    int t,n;
    char b[10];
    scanf(“%d”,&n);
    while(n–)
    {
        scanf(“%s”,b);
        s=b;
        scanf(“%s”,b);
        a=b;
        t=bfs();
        if(t==-1)
            printf(“Impossible\n”);
        else
            printf(“%d\n”,t);
    }
    return 0;
}

    原文作者:九宫格问题
    原文地址: https://blog.csdn.net/kangyan__/article/details/78935041
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞