zoukankan      html  css  js  c++  java
  • CODE[VS] 1004 四子连棋

    题目描述 Description

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

     
     

     

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

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

    样例输入 Sample Input

    BWBO
    WBWB
    BWBW
    WBWO

    样例输出 Sample Output

    5


    这是一个BFS的搜索题目,但是要涉及到的内容有一点是对当前状态的判重,我们可以将其转化为3进制,对每个把二维数组理解为一个一维数组,这个一维数组从第一个下标开始,对应3进制的一个位,最后对这个三进制转化的十进制进行mod大质数3733799判重

    BFS的过程是找到空格,然后对能达到空格的点进行判断,是否是当前要走的点。如果是要走的点的话交换。否则继续遍历。

    封尾就是当第一次到达状态四子连线的时候。

    代码如下:
    /*************************************************************************
        > File Name: 四子连棋.cpp
        > Author: zhanghaoran
        > Mail: chilumanxi@gmail.com 
        > Created Time: 2015年07月20日 星期一 09时00分54秒
     ************************************************************************/
    
    #include <iostream>
    #include <algorithm>
    #include <cstdio>
    #include <cstring>
    
    using namespace std;
    
    struct node{
        int map[5][5];
    }q[10000];
    
    int next[10000] = {1, 2};
    int step[10000];
    bool hash[5000000];
    int dx[4] = {0, 0, 1, -1};
    int dy[4] = {1, -1, 0, 0};
    
    int t = 0;
    int w = 2;
    bool flag = false;
    
    void swap(int &a, int &b){
        int t = a;
        a = b;
        b = t;
    }
    
    bool equ(int x1,int x2, int x3, int x4){
        if(x1 != x2 || x2 != x3 || x3 != x4 || x4 != x1)
            return false;
        else 
            return true;
    }
    
    bool check_end(){
        for(int i = 1; i <= 4; i ++){
            if(equ(q[w].map[i][1], q[w].map[i][2], q[w].map[i][3], q[w].map[i][4]))
                return true;
            if(equ(q[w].map[1][i], q[w].map[2][i], q[w].map[3][i], q[w].map[4][i]))
                return true;
        }
            if(equ(q[w].map[1][1], q[w].map[2][2], q[w].map[3][3], q[w].map[4][4]))
                return true;
            if(equ(q[w].map[4][1], q[w].map[3][2], q[w].map[2][3], q[w].map[1][4]))
                return true;
        return false;
    }
    
    bool Hash(){
        int s = 0;
        int temp = 1;
        for(int i = 1; i <= 4; i ++){
            for(int j = 1; j <= 4; j ++){
                s += q[w].map[i][j] * temp;
                temp *= 3;
            }
        }
        s %= 3733799;
        if(!hash[s]){
            hash[s] = true;
            return true;
        }
        else 
            return false;
    }
    
    bool check_move(int x, int y){
        int k = next[t];
        if(x > 4 || y > 4 || x == 0 || y == 0)
            return false;
        else if(q[t].map[x][y] == k)
            return true;
        return false;
    }
    
    void move(int x, int y){
        for(int i = 0; i < 4; i ++){
            int xx = x + dx[i];
            int yy = y + dy[i];
            if(check_move(xx, yy)){
                for(int j = 1; j <= 4; j ++){
                    for(int k = 1; k <= 4; k ++){
                        q[w].map[j][k] = q[t].map[j][k];
                    }
                }
                swap(q[w].map[x][y], q[w].map[xx][yy]);
                step[w] = step[t] + 1;
                if(check_end()){
                    cout << step[w] << endl; 
                    flag = true;
                    return ;
                }
                if(Hash()){
                    if(next[t] == 1) 
                        next[w ++] = 2;
                    else if(next[t] == 2)
                        next[w ++] = 1; 
                }
            }
        }
    }
    
    
    int main(void){
        char c;
        memset(q, 0, sizeof(q));
        for(int i = 1; i <= 4; i ++){
            for(int j = 1; j <= 4; j ++){
                cin >> c;
                if(c == 'W')
                    q[0].map[i][j] = q[1].map[i][j] = 1;
                else if(c == 'B')
                    q[0].map[i][j] = q[1].map[i][j] = 2;
            }
        }
        while(t < w){
            for(int i = 1; i <= 4; i ++){
                for(int j = 1; j <= 4; j ++){
                    if(q[t].map[i][j] == 0)
                        move(i, j);
                    if(flag)
                        return 0;
                }
            }
            t ++;
        }
    }


  • 相关阅读:
    软件开发 [CJOJ 1101] [NOIP 模拟]
    OI中卡常数技巧
    疫情控制 [NOIP2012]
    开车旅行 [NOIP 2012]
    观光公交 [NOIP 2011] [思维推导]
    选择客栈 [NOIP 2011]
    2016级算法期末上机-F.中等·AlvinZH's Fight with DDLs II
    2016级算法期末上机-E.中等·ModricWang's Fight with DDLs II
    2016级算法期末上机-D.简单·AlvinZH's Fight with DDLs I
    2016级算法期末上机-C.简单·Bamboo's Fight with DDLs III
  • 原文地址:https://www.cnblogs.com/chilumanxi/p/5136106.html
Copyright © 2011-2022 走看看