codevs1004 四子连棋

题目描述 Description

在一个4*4的棋盘上摆放了14颗棋子,其中有7颗白色棋子,7颗黑色棋子,有两个空白地带,任何一颗黑白棋子都可以向上下左右四个方向移动到相邻的空格,这叫行棋一步,黑白双方交替走棋,任意一方可以先走,如果某个时刻使得任意一种颜色的棋子形成四个一线(包括斜线),这样的状态为目标棋局。

 
 
输入描述 Input Description
从文件中读入一个4*4的初始棋局,黑棋子用B表示,白棋子用W表示,空格地带用O表示。
输出描述 Output Description

用最少的步数移动到目标棋局的步数。

样例输入 Sample Input

BWBO
WBWB
BWBW
WBWO

样例输出 Sample Output

5

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>

using namespace std;
const int maxn = 200000;

struct member{
    int last,step;
    int board[6][6];//[y][x]
};
member first,q[maxn];
int tot;
int dx[4] = {0,-1,0,1};
int dy[4] = {-1,0,1,0};

bool judge(member temp){
    int r1 = 1,r2 = 1;
    for(r1 = 1;r1 <= 4;r1++){
        bool sign = true;
        for(r2 = 1;r2 <= 3;r2++){
            if(temp.board[r1][r2] != temp.board[r1][r2+1]) sign = false;
        }
        if(sign) return true;
    }
    for(r1 = 1;r1 <= 4;r1++){
        bool sign = true;
        for(r2 = 1;r2 <= 3;r2++){
            if(temp.board[r2][r1] != temp.board[r2+1][r1]) sign = false;
        }
        if(sign) return true;
    }
    if(temp.board[1][1] == temp.board[2][2] && temp.board[2][2] == temp.board[3][3] && temp.board[3][3] == temp.board[4][4]) return true;
    if(temp.board[1][4] == temp.board[2][3] && temp.board[2][3] == temp.board[3][2] && temp.board[3][2] == temp.board[4][1]) return true;
    return false;
}

int bfs(){
    int h = 1,t = 1,nx,ny;
    member tailer;
    while(h <= t){
        if(judge(q[h])) return q[h].step;
        int f1 = 1,f2 = 1,ya,xa,yb,xb,sign = 1;
        for(f1 = 1;f1 <= 4;f1++){
            for(f2 = 1;f2 <= 4;f2++){
                if(q[h].board[f1][f2] == 0){
                    if(sign == 1){
                        sign = 2;
                        ya = f1;
                        xa = f2;
                    }else if(sign == 2){
                        yb = f1;
                        xb = f2;
                    }
                }
            }
        }
        f1 = f2 = 0;
        for(f1 = 0;f1 < 4;f1++){
            nx = xa + dx[f1];
            ny = ya + dy[f1];
            if(nx <= 4 && nx >= 0 && ny <=4 && ny >= 0 && q[h].board[ny][nx] != q[h].last){
                t++;
                tailer = q[h];
                tailer.last = tailer.board[ny][nx];
                swap(tailer.board[ny][nx],tailer.board[ny - dy[f1]][nx - dx[f1]]);
                tailer.step++;
                q[t] = tailer;
                
            }
            nx = xb + dx[f1];
            ny = yb + dy[f1];
            if(nx <= 4 && nx >= 0 && ny <=4 && ny >= 0 && q[h].board[ny][nx] != q[h].last){
                t++;
                tailer = q[h];
                tailer.last = tailer.board[ny][nx];
                swap(tailer.board[ny][nx],tailer.board[ny - dy[f1]][nx - dx[f1]]);
                tailer.step++;
                q[t] = tailer;
                
            }
            
        }
        h++;
    }
    
}

int main(){
    char cmd;
    int r1,r2,ans;
    for(r1 = 1;r1 <= 4;r1++){
        for(r2 = 1;r2 <= 4;r2++){
            cin>>cmd;
            if(cmd == 'O') first.board[r1][r2] = 0;
            if(cmd == 'B') first.board[r1][r2] = 1;
            if(cmd == 'W') first.board[r1][r2] = 2;
        }
    }
    first.step = 0;
    first.last = 0;
    q[1] = first;
    ans = bfs();
    cout<<ans;
    return 0;
}
原文地址:https://www.cnblogs.com/hyfer/p/5845975.html