1. 程式人生 > >Connect3(dfs模擬+map存圖)

Connect3(dfs模擬+map存圖)

                                                         時間限制: 1 Sec  記憶體限制: 256 MB                                                                            提交: 42  解決: 19                                                          [提交] [狀態] [討論版] [命題人:admin]

題目描述

Connect3 is a simplified version of a well-known Connect4 game. Connect3 is a game for two players, black and white, who take turns placing their colored stones in a 4 x 4 grid board shown in Fig.B.1. Each square (or box) in the grid is represented by a pair of numbers (a, b) where a is for a row and b is for a column. The lower left corner of the board is (1, 1), and the upper right corner is (4, 4). Each player selects a column to place a stone which is then placed on the lowest empty square in the column. For example, square (3, 1) is to be taken only when squares (2, 1) and (1, 1) are occupied beforehand. The game ends if three stones with the same color connect in either horizontally, diagonally, or vertically in a row and the player of the color wins.

                

The game starts by a player placing a black stone on square (1, x). If the game ends by the white player placing a stone on square (a, b), let the final state of the board be s. You are to write a program to find the number of all possible unique states of s. Note that the order of stones placed is irrelevant.

輸入

Your program is to read from standard input. The input starts with a line containing an integer x (1 ≤ x ≤ 4), representing the column of the first stone placed on the board. The next line of input shows two integers, a and b for square (a, b) which is the position of the last stone placed on the board.

輸出

Your program is to write to standard output. Print exactly one number that corresponds to the answer.

樣例輸入

2
2 3

樣例輸出

516

                                                                                       [提交] [狀態]

題意:

給出先手黑棋的位置(1,x)和最後一子白棋的位置(x1,y1),三子相連即勝利,問最後白棋獲勝的可能狀態有多少種,每次只能下在某一列的最低位置處。

題解:

純粹的dfs模擬放置棋子的過程。注意當某一列棋子放滿之後就不能繼續放了,而且若當前局面已有一方勝利或者有棋子放置在終點的時候要return。若放置在終點的棋子為白棋並且白棋此時能贏,則ans++。另外要用map來儲存當前局面的狀態,以防重複。

重點來了 如何儲存局面的狀態  

ll recheck()
{
    ll sum=0;
    for(int i=0; i<4; i++)
        for(int j=0; j<4; j++)
            sum=sum<<2 | a[j][i];//確保狀態唯一性
     return sum;
}

sum<<2 表示 sum左移2位 因為 1表示黑子,2表示白子 1的二進位制為 01     2的二進位制為 10   其左移2位 分別變成了 0100 和 1000   在和a[i][j] 或 一下 因為後兩位為0 所以或出來的結果為 一個四位的01串  所以就把矩陣轉換成了 一個0 1串 且僅表示一個數  並且這個數的是唯一的 就可以用它來判斷當前局面

#include<bits/stdc++.h>
using  namespace std;
typedef long long ll;
const int N=6;
int a[N][N],cnt[N];//cnt用來記錄每列已經下了多少棋子
int X0,X1,Y1,ans;
map<ll,int> vis;

ll recheck()
{
    ll sum=0;
    for(int i=0; i<4; i++)
        for(int j=0; j<4; j++)
            sum=sum<<2 | a[j][i];//確保狀態唯一性
     return sum;
}
int judge(int p)
{

    for(int i=0; i<4; i++)
        for(int j=0; j<4; j++)
        {
            if(i+2<=3&&a[i][j]==p&&a[i+1][j]==p&&a[i+2][j]==p)return 1;//豎
            if(j+2<=3&&a[i][j]==p&&a[i][j+1]==p&&a[i][j+2]==p)return 1;//橫
            if(i+2<=3&&j+2<=3&&a[i][j]==p&&a[i+1][j+1]==p&&a[i+2][j+2]==p)return 1;//右上
            if(i-2>=0&&j+2<=3&&a[i][j]==p&&a[i-1][j+1]==p&&a[i-2][j+2]==p)return 1;//左上
        }
    return 0;
}
void dfs(int p)
{

    unsigned int k=recheck();
    if(vis[k])return;
    vis[k]=1;
    if(judge(1)||judge(2)||a[X1][Y1])
    {
        if(judge(2)&&a[X1][Y1]==2)ans++;//以白棋在x1,y1處結尾且白棋連續
        return;
    }

    for(int i=0; i<4; i++)//四列分別放棋
    {

        if(cnt[i]==4)continue;
        a[i][cnt[i]++]=p;
        dfs(3-p);     //   dfs(p^3)  p=2時p^3為1,反之為2(按位異或,不同為1,相同為0)
        a[i][--cnt[i]]=0;
    }
}
int main()
{

    scanf("%d%d%d",&X0,&Y1,&X1); 
    X0--,Y1--,X1--;//下標從0開始
    a[X0][cnt[X0]++]=1;//1代表黑,2代表白
    dfs(2);
    printf("%d\n",ans);
    return 0;
}