Gym - 101667B Connect3 [DFS 模拟]
问题 B: Connect3
时间限制: 1 Sec 内存限制: 256 MB
提交: 39 解决: 16
[提交] [状态] [讨论版] [命题人: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
题意:
题意:两个人轮流从棋盘上分别放置黑白两色的棋子,每次只能从最低位放置。给你规定第一枚黑棋和最后一枚白棋放置的位置,让你求出总共有多少种局面,使得该局面白棋胜利。(横竖斜至少有一个方向存在连续的三枚同色棋子即可胜利)
直接模拟,若当前局面已有一方胜利或者有棋子放置在终点的时候要return。若放置在终点的棋子为白棋并且白棋此时能赢,则++ans。因为图上存0 1 2 所以用三进制最多3^16次方表示这个状态用map标记
代码:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int x,end_a,end_b;
int mmp[10][10];
int line[10];
map<ll,int>fuck_szh_hhz;
ll ans;
ll qpow(ll a,ll b){
ll ans=1;
while(b){
if(b&1){
ans=ans*a;
}
a=a*a;
b>>=1;
}
return ans;
}
ll resert()
{
ll k = 0;
int cnt = 0;
for(int i = 0;i <= 3;i++)
{
for(int j = 0;j <= 3;j++)
{
k += mmp[i][j]*qpow(3,cnt);
cnt++;
}
}
return k;
}
bool judge(int flag)
{
for(int x = 0;x <= 3;x++)
{
for(int y = 0;y <= 3;y++)
{
if(y+2<=3)
{
if(mmp[x][y] == mmp[x][y+1] &&mmp[x][y+1] == mmp[x][y+2] &&flag == mmp[x][y+2])
{
//cout<<"1"<<x<<" "<<y<<endl;
return true;
}
}
if(x+2<=3)
{
if(mmp[x][y] == mmp[x+1][y] && mmp[x+1][y] == mmp[x+2][y] && mmp[x+2][y] == flag)
{
//cout<<mmp[x][y] << mmp[x+1][y] << mmp[x+2][y]<<endl;
//cout<<"2"<<x<<" "<<y<<endl;
return true;
}
}
if(x+2 <= 3 && y+2 <= 3)
{
if(mmp[x][y] == mmp[x+1][y+1] && mmp[x+1][y+1] == mmp[x+2][y+2] && flag == mmp[x+2][y+2])
{
//cout<<"3"<<x<<" "<<y<<endl;
return true;
}
}
if(x-2 >= 0 && y+2 <= 3)
{
if(mmp[x][y] == mmp[x-1][y+1] && mmp[x-1][y+1] == mmp[x-2][y+2] && flag == mmp[x-2][y+2])
{
//cout<<"4"<<x<<" "<<y<<endl;
return true;
}
}
}
}
return false;
}
void dfs(int step)
{
ll cur = resert();
//cout<<cur<<endl;
if(fuck_szh_hhz[cur])
return ;
fuck_szh_hhz[cur] = 1;
if(judge(1) || judge(2) || mmp[end_a][end_b])
{
if(judge(2) && mmp[end_a][end_b] == 2)
ans++;
return ;
}
for(int i = 0;i <= 3;i++)
{
if(line[i] == 4)
continue;
int p = step%2;
if(p == 0)
p = 2;
mmp[i][line[i]] = p;
line[i]++;
step++;
dfs(step);
step--;
line[i]--;
mmp[i][line[i]] = 0;
}
}
int main()
{
scanf("%d%d%d",&x,&end_b,&end_a);
x--;
end_a--,end_b--;
mmp[x][line[x]] = 1;
line[x]++;
dfs(2);
printf("%lld\n",ans);
return 0;
}