黑白棋游戏

黑白棋游戏

Time Limit:10000MS  Memory Limit:65536K
Total Submit:9 Accepted:5 
Case Time Limit:1000MS

Description

【问题描述】 
黑白棋游戏的棋盘由4×4方格阵列构成。棋盘的每一方格中放有1枚棋子,共有8枚白棋子和8枚黑棋子。这16枚棋子的每一种放置方案都构成一个游戏状态。在棋盘上拥有1条公共边的2个方格称为相邻方格。一个方格最多可有4个相邻方格。在玩黑白棋游戏时,每一步可将任何2个相邻方格中棋子互换位置。对于给定的初始游戏状态和目标游戏状态,编程计算从初始游戏状态变化到目标游戏状态的最短着棋序列。 
【输入】 
输入文件共有8行。前四行是初始游戏状态,后四行是目标游戏状态。每行4个数分别表示该行放置的棋子颜色。“0”表示白棋;“1”表示黑棋。 
【输出】 
输出文件的第一行是着棋步数n。接下来n行,每行4个数分别表示该步交换棋子的两个相邻方格的位置。例如,abcd表示将棋盘上(a,b)处的棋子与(c,d)处的棋子换位。 

Input

1111
0000
1110
0010
1010
0101 
1010 
0101 

Output


1222 
1424 
3242 
4344


水题。。直接bFS


下面是AC代码:

#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int maxn = 5;
char s_pos[maxn][maxn];
char e_pos[maxn][maxn];
int n;
bool vis[140000];
int dx[]= {1,-1,0,0};
int dy[]= {0,0,-1,1};
struct node
{
    char m[maxn][maxn];
    int step;
    char ans[100][5];
} start,ans;
bool cheack(int x,int y)
{
    return x>=0&&x<4&&y>=0&&y<4;
}
bool equal(char m[maxn][maxn])
{
    for(int i=0; i<4; i++) for(int j=0; j<4; j++)
            if(m[i][j]!=e_pos[i][j]) return false;

    return true;
}
int hash(char m[maxn][maxn])
{
    int res=0;
    for(int i=0; i<4; i++) for(int j=0; j<4; j++)
        {
           // if(m[i][j]=='1')
           // res+= 1<<(i*4+j);
           res=(res<<1)+(m[i][j]-'0');
        }
    return res;
}
void bfs()
{
    memset(vis,false,sizeof(vis));
    queue<node > q;
    q.push(start);
    int t=hash(s_pos);
    vis[t]=true;
    while(!q.empty())
    {
        node now = q.front();  q.pop();
        if(equal(now.m)){
            ans=now;
            return ;
        }
        //printf("4344\n");
        for(int i=0; i<4; i++) for(int j=0; j<4; j++)
                for(int k=0; k<4; k++)
                {
                    node next = now;
                    next.step+=1;
                    int x=i+dx[k],y=j+dy[k];
                    if(cheack(x,y))
                    {
                        char temp=next.m[i][j];

                        next.m[i][j]=next.m[x][y];
                        next.m[x][y]=temp;
                        next.ans[next.step-1][0]=i+'1';
                        next.ans[next.step-1][1]=j+'1';
                        next.ans[next.step-1][2]=x+'1';
                        next.ans[next.step-1][3]=y+'1';
                        next.ans[next.step-1][4]='\0';
                        if(equal(next.m))
                        {
                        //    printf("%d\n",ans.step);
                            ans=next;
                            return ;
                        }
                        t=hash(next.m);
                        if(!vis[t])
                        {
                            vis[t]=true;
                            q.push(next);
                        }
                    }
                }
    }

}
int main()
{
    for(int i=0; i<4; i++)  scanf("%s",s_pos[i]);
    for(int i=0; i<4; i++)  scanf("%s",e_pos[i]);
    for(int i=0; i<4; i++) for(int j=0; j<4; j++)
            start.m[i][j]=s_pos[i][j];
    start.step=0;
    bfs();

    printf("%d\n",ans.step);
    for(int i=0; i<ans.step; i++)
        printf("%s\n",ans.ans[i]);

    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值