zoukankan      html  css  js  c++  java
  • codevs 1004 四子连棋

    bfs

    题目链接:http://codevs.cn/problem/1004/

    搜索不解释了。

    不过好像我写的queue还不如不写QAQAQ

    Codes:

    #include<iostream>
    #include<queue>
    #include<cstring>
    #include<cstdio>
    
    using namespace std;
    const int p = 3733799;
    struct data{
        int mp[5][5];
    }d[5000 + 10];
    int nxt[5000] = {1,2};//下一步走哪个颜色 1:w 2:b 0:o 
    int step[5000 + 10];
    bool hashh[40000000 + 10];
    char s[5][5];
    int xx[] = {0,0,0,1,-1},yy[] = {0,1,-1,0,0};
    int head = 0,tail,f = 0;
    
    bool same(int a1,int a2,int a3,int a4){
        if(a1 != a2 || a2 != a3 || a3 != a4 || a4 != a1)
            return 0;
        return 1;
    }
    bool check(){
        for(int i = 1;i <= 4;++ i){
            if(same(d[tail].mp[i][1],d[tail].mp[i][2],d[tail].mp[i][3],d[tail].mp[i][4])) return 1;
            if(same(d[tail].mp[1][i],d[tail].mp[2][i],d[tail].mp[3][i],d[tail].mp[4][i])) return 1;
        }
        if(same(d[tail].mp[1][1],d[tail].mp[2][2],d[tail].mp[3][3],d[tail].mp[4][4])) return 1;
        if(same(d[tail].mp[1][4],d[tail].mp[2][3],d[tail].mp[3][2],d[tail].mp[4][1])) return 1;
        return 0;
    }
    int Hash(){
        int ans = 0;
        for(int i = 1;i <= 4;++ i){
            for(int j = 1;j <= 4;++ j){
                ans = ans * 3 + d[tail].mp[i][j];
            }
        }
        ans %= p;
        if(!hashh[ans]){
            hashh[ans] = 1;
            return 1;
        }
        return 0;
    }
    bool cana(int x,int y){
        int k = nxt[head];
        if(x > 4 || x < 1 || y > 4 || y < 1) return false;
        if(d[head].mp[x][y] == k) return true;
        return false;
    }
    queue <int> q;
    void move(int x,int y){
        //head = q.front();
        for(int i = 1;i <= 4;++ i){
            int lx = x + xx[i],ly = y + yy[i];
            if(cana(lx,ly)){
                for(int j = 1;j <= 4;++ j){
                    for(int k = 1;k <= 4;++ k){
                        d[tail].mp[j][k] = d[head].mp[j][k];
                    }
                }
                swap(d[tail].mp[x][y],d[tail].mp[lx][ly]);
                step[tail] = step[head] + 1;
                if(check()){
                    cout << step[tail] << '
    ';
                    f = 1;
                    return;
                }
                if(Hash()){
                    if(nxt[head] == 1){
                        nxt[tail] = 2;
                        tail ++;
                        q.push(tail);
                    } 
                    if(nxt[head] == 2){
                        nxt[tail] = 1;
                        tail ++;
                        q.push(tail);
                    }
                }
            }
        }
    }
    void search(){
        q.push(0);
        q.push(1);
        head = q.front();
        tail = 2;
        while(!q.empty()){
            head = q.front();
            for(int i = 1;i <= 4;++ i)
                for(int j = 1;j <= 4;++ j){
                    if(!d[head].mp[i][j])
                        move(i,j);
                    if(f)
                        return;
                }
            q.pop();
        }
    }
    
    int main(){
        for(int i = 1;i <= 4;++ i)
            scanf("%s",s[i] + 1);
        for(int i = 1;i <= 4;++ i){
            for(int j = 1;j <= 4;++ j){
                if(s[i][j] == 'W')
                    d[0].mp[i][j] = d[1].mp[i][j] = 1;
                if(s[i][j] == 'B')
                    d[0].mp[i][j] = d[1].mp[i][j] = 2;    
            }
        }
        search();
        return 0;
    }
  • 相关阅读:
    测试软件—禅道BUG管理工具
    C语言 线性表的操作~(未完)
    数据库考纲~
    圣杯布局和双飞翼布局总局
    总结布局用法
    springboot~入门第三篇~与mybatis整合~(未完)
    微信小程序里 wx:for和wx:for-item区别(补充下wx:key)
    对比下小程序语法和Vue语法异同
    视频转换 rtsp 流 转rtmp流播放(待完善)
    Vue钩子函数~
  • 原文地址:https://www.cnblogs.com/Loizbq/p/7806346.html
Copyright © 2011-2022 走看看