码迷,mamicode.com
首页 > 其他好文 > 详细

codevs 1004 四子连棋

时间:2017-11-08 22:23:22      阅读:103      评论:0      收藏:0      [点我收藏+]

标签:include   span   题目   wap   const   stream   pre   can   strong   

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] << \n;
                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;
}

 

codevs 1004 四子连棋

标签:include   span   题目   wap   const   stream   pre   can   strong   

原文地址:http://www.cnblogs.com/Loizbq/p/7806346.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!