Connect3
时间限制: 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;
}