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/
  • 相关阅读:
    css最简单的在背景图片上显示模糊背景色的方法
    css添加网格背景
    获取bing必应图片
    JavaScript超过一定高度导航添加类名
    6行css就可以解决的瀑布流布局的方法
    css实现背景图横向滚动
    JavaScript根据一个元素的显示隐藏控制另一个元素的显示和隐藏
    JavaScript判断地址栏链接与导航栏链接是否一致并给导航添加class
    JavaScript实现选中文字自动复制
    Day 74 算法基础(二)
  • 原文地址:https://www.cnblogs.com/lllxq/p/9744544.html
Copyright © 2011-2022 走看看