zoukankan      html  css  js  c++  java
  • [搜索]Connect3

    题目描述

    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
     

    思路:暴搜,O(16*2^16)

    AC代码:

    #include <iostream>
    #include<cstdio>
    #include<map>
    using namespace std;
    
    int x,a,b;
    int ans=0;
    int board[10][10];
    int black,white;
    map<pair<int,int>,int> mp;
    
    int get_loc(int x,int y) {return 4*(x-1)+y;}
    
    bool check(int p){
      for(int i=1;i<=4;i++){
        for(int j=1;j<=4;j++){
            if(i<=2&&board[i][j]==p&&board[i+1][j]==p&&board[i+2][j]==p) return 1;
            if(j<=2&&board[i][j]==p&&board[i][j+1]==p&&board[i][j+2]==p) return 1;
            if(i<=2&&j<=2&&board[i][j]==p&&board[i+1][j+1]==p&&board[i+2][j+2]==p) return 1;
            if(i<=2&&j>=3&&board[i][j]==p&&board[i+1][j-1]==p&&board[i+2][j-2]==p) return 1;
        }
      }
      return 0;
    }
    
    void dfs(int x,int y,int turn){
      board[x][y]=turn;
      int cop1=black,cop2=white;
      black=black|((turn==2)<<(get_loc(x,y)-1));
      white=white|((turn==3)<<(get_loc(x,y)-1));
      if(check(2)){
        board[x][y]=0,black=cop1,white=cop2;
        return;
      }
      if(check(3)){
        if(x==a&&y==b) ans++;
        board[x][y]=0,black=cop1,white=cop2;
        return;
      }
      if(mp[make_pair(black,white)]){//判重必需在以上两个check之后
        board[x][y]=0,black=cop1,white=cop2;
        return;
      }
      mp[make_pair(black,white)]=1;
      for(int j=1;j<=4;j++){
        for(int i=1;i<=4;i++){
            if(!board[i][j]&&(i==1||(i>=2&&board[i-1][j]))){
                dfs(i,j,turn^1);
            }
        }
      }
      board[x][y]=0,black=cop1,white=cop2;
    }
    
    int main()
    {
        scanf("%d%d%d",&x,&a,&b);
        dfs(1,x,2);
        printf("%d
    ",ans);
        return 0;
    }
    转载请注明出处:https://www.cnblogs.com/lllxq/
  • 相关阅读:
    SQL Server查询死锁并KILL
    android之Widget01
    android之RatingBar控件用法
    android之SeekBar控件用法
    android之ExpandableListActivity
    【原创】开源.NET排列组合组件KwCombinatorics使用(一)—组合生成
    【吐槽】博客园新的原创文章在搜索引擎的排名不及转载的站点
    【资源】108个大数据文档PDF开放下载-整理后打包下载
    【原创】.NET平台机器学习组件-Infer.NET连载(二)贝叶斯分类器
    【原创】.NET平台机器学习组件-Infer.NET连载(一)介绍
  • 原文地址:https://www.cnblogs.com/lllxq/p/9744544.html
Copyright © 2011-2022 走看看