ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

【codevs1004】四子连棋

2021-11-15 14:01:42  阅读:123  来源: 互联网

标签:四子 return int ch 连棋 && ok codevs1004 check


problem

solution

codes

//思路:把空白当棋,交替黑白走。
//实现:BFS, 打表判断是否成立
#include<iostream>
#include<algorithm>
#include<string>
#include<queue>
using namespace std;
string s;
struct node{
    string ma;  int step;  char next;
    node(string x, int y, char ch):ma(x),step(y),next(ch){}
};
queue<node>q;
int dz[] = {4,-4,1,-1};
char change(char ch){
    if(ch == 'B')return 'W';
    if(ch == 'W')return 'B';
}
int check(string s){
    //check diagonal 1
    if(s[0]==s[5] && s[5]==s[10] && s[10]==s[15])return 1;
    //check diagonal 2
    if(s[3]==s[6] && s[6]==s[9] && s[9]==s[12])return 1;
    //check row
    for(int i = 0; i < 4; i++){
        int ok = 1, t = 4*i;
        for(int j = 0; j < 4; j++)
            if(s[t] != s[t+j])ok = 0;
        if(ok)return 1;
    }
    //check col
    for(int i = 0; i < 4; i++){
        int ok = 1, t = i;
        for(int j = 0; j < 4; j++)
            if(s[t] != s[t+j*4])ok = 0;
        if(ok)return 1;
    }
    return 0;
}
int bfs(){
    while(q.size()){
        string t = q.front().ma;
        int st = q.front().step;
        char ch = q.front().next;
        q.pop();
        //check
        if(check(t))return st;
        //find O
        int o1=-1, o2;
        for(int i = 0; i < 16; i++){
            if(t[i]=='O'){
                if(o1==-1)o1 = i;
                else o2 = i;
            }
        }
        //o1go
        for(int i = 0; i < 4; i++){
            if(dz[i]==1 && o1%4==3)continue;
            if(dz[i]==-1 && o1%4==0)continue;
            int nz = o1+dz[i];
            if(nz>=0 && nz<16 && t[nz]==ch){
                string nt = t;
                swap(nt[o1],nt[nz]);
                q.push(node(nt,st+1,change(ch)));
            }
        }
        //o2go
        for(int i = 0; i < 4; i++){
            if(dz[i]==1 && o2%4==3)continue;
            if(dz[i]==-1 && o2%4==0)continue;
            int nz = o2+dz[i];
            if(nz>=0 && nz<16 && t[nz]==ch){
                string nt = t;
                swap(nt[o2],nt[nz]);
                q.push(node(nt,st+1,change(ch)));
            }
        }
    }
}
int main(){
    ios::sync_with_stdio(false);
    for(int i = 0; i < 4; i++){
        string t;  cin>>t;  s += t;
    }
    if(check(s)){ cout<<"0"; return 0;}
    int ans = 0xffffff;
    q.push(node(s,0,'W'));
    ans = min(ans, bfs());
    while(q.size())q.pop();
    q.push(node(s,0,'B'));
    ans = min(ans, bfs());
    cout<<ans<<"\n";
    return 0;
}

 

标签:四子,return,int,ch,连棋,&&,ok,codevs1004,check
来源: https://www.cnblogs.com/gwj13114/p/15555982.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有