zoukankan      html  css  js  c++  java
  • 【codevs1004】四子连棋

    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<<"
    ";
        return 0;
    }
  • 相关阅读:
    eclipse中maven插件,改变默认仓库位置
    maven ArtifactTransferException:failure
    抽象工厂模式
    简单工厂模式,利用反射机制让简单工厂模式变得更灵活
    Android开发之获取系统版本号
    Android开发之ProgressDialog与ProgressBar
    Failed to initialize monitor Thread: Unable to establish loopback connection解决方法
    Android开发之通过反射获取到挂断电话的API
    Android Studio常用插件
    Proxifier设置代理
  • 原文地址:https://www.cnblogs.com/gwj1314/p/9444769.html
Copyright © 2011-2022 走看看