黑白棋遊戲

來源:互聯網
上載者:User

黑白棋遊戲

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.