HDU 5546 Ancient Go (搜索)

题意:

Alice和Bob正在下古代围棋,规则如下:
  • 棋盘有8×8个格子,棋子下在棋盘的交叉点上,故可以有9×9个落子的位置
  • Alice执黑棋Bob执白棋轮流落子
  • 与棋子直线相连的空白交叉点叫做气。当这些气都被对方棋子占据后,该棋子就没有了“气”,要被从棋盘上提掉。如果棋子的相邻(仅上下左右)直线交叉点上有了同色的棋子,则这两个棋子被叫做相连的。任意多个棋子可以以此方式联成一体,连成一体的棋子的气的数目是所有组成这块棋的单个棋子气数之和。如果这些气都被异色棋子占领,这块棋子就要被一起提掉。
  • 当一方落子后,先检查对手的棋子,将没有“气”的对手的棋子从棋盘上提掉,再检查该方的棋子,将没有“气”的棋子提掉
现在该Alice落子,请判断Alice能否在某处落子使Bob至少有一颗棋子 被 从棋盘上提掉
析:由于格子只是9*9而已,直接枚举每个点,然后判断能不能提掉Bob的至少一个棋子即可。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 10 + 10;
const int mod = 1e9 + 7;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
  return r >= 0 && r < n && c >= 0 && c < m;
}
char s[maxn][maxn];
bool vis[maxn][maxn];

bool dfs(int r, int c){
  if(s[r][c] == '.')  return false;
  vis[r][c] = 1;
  for(int i = 0; i < 4; ++i){
    int x = r + dr[i];
    int y = c + dc[i];
    if(!is_in(x, y) || vis[x][y] || s[x][y] == 'x')  continue;
    if(!dfs(x, y))  return false;
  }
  return true;
}

bool solve(){
  for(int i = 0; i < n; ++i)
    for(int j = 0; j < m; ++j)
      if(s[i][j] == 'o'){
        memset(vis, 0, sizeof vis);
        if(dfs(i, j))  return true;
      }
  return false;
}

int main(){
  int T;  cin >> T;
  for(int kase = 1; kase <= T; ++kase){
    n = m = 9;
    for(int i = 0; i < n; ++i)  scanf("%s", s+i);
    bool ok = false;
    for(int i = 0; i < 9 && !ok; ++i)
      for(int j = 0; j < 9 && !ok; ++j){
        if(s[i][j] == '.'){
          s[i][j] = 'x';
          ok = solve();
          s[i][j] = '.';
        }
      }
    printf("Case #%d: %s
", kase, ok ? "Can kill in one move!!!" : "Can not kill in one move!!!");
  }
  return 0;
}

  

原文地址:https://www.cnblogs.com/dwtfukgv/p/6673015.html