Connect3

9311: Connect3

时间限制: 1 Sec  内存限制: 256 MB
提交: 50  解决: 24
[提交] [状态] [讨论版] [命题人: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

来源/分类

emmm,dfs,不太会,想了好久,本来以为自己还可以来着

优化的地方是dfs里面,本来要两个for嵌套,用一个t【】优化,减少了一个for

然后嘞,矩阵转化成了一个数,用map标记一下去重

#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
int mmp[6][6];
int a,b,c,p,t[6];
map<ll,bool> M;
void pr(){
    for(int i=4;i;i--)
    {
        for(int j=1;j<=4;j++)
            printf("%3d",mmp[i][j]);
        puts("");
    }
    puts("----------------->");
    getchar();
}
bool jude(){
    for(int i=1;i<=4;i++){
        for(int j=1;j<=4;j++){
            if(mmp[i][j]&& mmp[i][j] == mmp[i+1][j] && mmp[i][j] == mmp[i-1][j])
                return 1;
            if(mmp[i][j]&& mmp[i][j] == mmp[i][j+1] && mmp[i][j] == mmp[i][j-1])
                return 1;
            if(mmp[i][j]&& mmp[i][j] == mmp[i+1][j+1] && mmp[i][j] == mmp[i-1][j-1])
                return 1;
            if(mmp[i][j]&& mmp[i][j] == mmp[i-1][j+1] && mmp[i][j] == mmp[i+1][j-1])
                return 1;
        }
    }
    return 0;
}
void dfs(int u,int x,int y)
{
    //printf("%d\n",u);
    if(jude())
    {
        ll sum = 0;
        for(int i=1;i<=4;i++)
            for(int j=1;j<=4;j++){
                sum <<= 2;
                if(mmp[i][j]==1)sum |= 2;
                else if(mmp[i][j]==-1)sum |= 3;
                else sum |= 0;
            }
        //printf("over\n");
        if(u==1&&x==b&&y==c&&!M.count(sum))
            p++,M[sum] = 1;//,printf("---->win\n");
        return;
    }
    for(int i=1;i<=4;i++)
    {
        if(t[i]==5)continue;
        mmp[t[i]][i] = u;
        t[i]++;
        dfs(-u,t[i]-1,i);
        t[i]--;
        mmp[t[i]][i] = 0;
    }
}
int main()
{
    scanf("%d%d%d",&a,&b,&c);//getchar();
    for(int i=1;i<=4;i++)t[i] = 1;
    mmp[1][a] = 1;t[a] = 2;
 
    dfs(-1,a,1);
    printf("%d\n",p);
    return 0;
}
 

还有改出来了一个不太懂的代码

不清楚为什么mmp【b】【c】那里需要return

但是去掉后答案却减少了好多

#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
int mmp[6][6];
int a,b,c,p,t[6];
map<ll,bool> M;
bool jude(){
    for(int i=1;i<=4;i++){
        for(int j=1;j<=4;j++){
            if(mmp[i][j]&& mmp[i][j] == mmp[i+1][j] && mmp[i][j] == mmp[i-1][j])
                return 1;
            if(mmp[i][j]&& mmp[i][j] == mmp[i][j+1] && mmp[i][j] == mmp[i][j-1])
                return 1;
            if(mmp[i][j]&& mmp[i][j] == mmp[i+1][j+1] && mmp[i][j] == mmp[i-1][j-1])
                return 1;
            if(mmp[i][j]&& mmp[i][j] == mmp[i-1][j+1] && mmp[i][j] == mmp[i+1][j-1])
                return 1;
        }
    }
    return 0;
}
void init(ll &sum){
    for(int i=1;i<=4;i++){
        for(int j=1;j<=4;j++){
            sum = sum<<2 | (mmp[i][j]+1);
        }
    }
}
void dfs(int u,int x,int y)
{
    ll sum = 0;
    init(sum);
    if(M.count(sum))return;
    M[sum] = 1;
    if(jude()||mmp[b][c])
    {
        if(jude() && u==1 && x==b && y==c)
            p++;
        return;
    }
    for(int i=1;i<=4;i++)
    {
        if(t[i]>4)continue;
        mmp[t[i]++][i] = u;
        dfs(-u,t[i]-1,i);
        mmp[--t[i]][i] = 0;
    }
}
int main()
{
    scanf("%d%d%d",&a,&b,&c);
    for(int i=1;i<=4;i++)t[i] = 1;
    mmp[1][a] = 1;t[a] = 2;
    dfs(-1,a,1);
    printf("%d\n",p);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Du_Mingm/article/details/82941019