zoukankan      html  css  js  c++  java
  • HDU

     Yu Zhou likes to play Go with Su Lu. From the historical research, we found that there are much difference on the rules between ancient go and modern go. 

     Here is the rules for ancient go they were playing: 

     The game is played on a 8×88×8 cell board, the chess can be put on the intersection of the board lines, so there are 9×99×9 different positions to put the chess. 
     Yu Zhou always takes the black and Su Lu the white. They put the chess onto the game board alternately. 
     The chess of the same color makes connected components(connected by the board lines), for each of the components, if it's not connected with any of the empty cells, this component dies and will be removed from the game board. 
     When one of the player makes his move, check the opponent's components first. After removing the dead opponent's components, check with the player's components and remove the dead components. 
     One day, Yu Zhou was playing ancient go with Su Lu at home. It's Yu Zhou's move now. But they had to go for an emergency military action. Little Qiao looked at the game board and would like to know whether Yu Zhou has a move to kill at least one of Su Lu's chess.

    Input

     The first line of the input gives the number of test cases, T(1T100)T(1≤T≤100). TT test cases follow. Test cases are separated by an empty line. Each test case consist of 9 lines represent the game board. Each line consists of 9 characters. Each character represents a cell on the game board. .′.′ represents an empty cell. x′x′ represents a cell with black chess which owned by Yu Zhou. o′o′ represents a cell with white chess which owned by Su Lu.

    Output

     For each test case, output one line containing Case #x: y, where xx is the test case number (starting from 1) and yy is Can kill in one move!!! if Yu Zhou has a move to kill at least one of Su Lu's components. Can not kill in one move!!! otherwise.

    Sample Input

    2
    
    .......xo
    .........
    .........
    ..x......
    .xox....x
    .o.o...xo
    ..o......
    .....xxxo
    ....xooo.
    
    ......ox.
    .......o.
    ...o.....
    ..o.o....
    ...o.....
    .........
    .......o.
    ...x.....
    ........o

    Sample Output

    Case #1: Can kill in one move!!!
    Case #2: Can not kill in one move!!!

    Hint

    In the first test case, Yu Zhou has 4 different ways to kill Su Lu's component.
    
    In the second test case, there is no way to kill Su Lu's component.

     题意:

    所给9*9大小的棋盘,'.'为空,问'x'再下一棋,围住一堆'o'(围成的一块是没有'.'的)

    思路:

    枚举每个空点,判断能否成功,

    那么怎么判断是否成功呢?

    四个方向走,看做四个块,再写另一个函数,判断块是否封闭

    怎么判断是否封闭呢?

    块里不能有空点就是封闭,每层函数,上||下||左||右 走一格

     1 #include <stdio.h>
     2 #include <string.h>
     3 
     4 int n = 9;
     5 int t[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
     6 char a[15][15];
     7 int vis[15][15];
     8 
     9 int ff(int x, int y)      //递归判断该部分'o'是否完全被围起来了
    10 {
    11     int i, xx, yy;
    12     for(i=0;i<4;i++)
    13     {
    14         xx = x + t[i][0];
    15         yy = y + t[i][1];
    16         if(xx >= 0 && xx < 9 && yy >= 0 && yy < 9 && vis[xx][yy]==0)
    17         {
    18             vis[xx][yy] = 1;
    19             if(a[xx][yy]=='.') return 1;    //有空的位置,则确定没有成功围起来
    20             else if(a[xx][yy]=='o'&&ff(xx, yy)) return 1; //点为'o',需判断周围,再下结论
    21         }
    22     }
    23     return 0;        //完全围起      
    24 }
    25 
    26 int f(int x, int y)
    27 {
    28     int i, xx, yy;
    29     for(i=0;i<4;i++)  //四个方向都有围成功的可能
    30     {
    31         xx = x + t[i][0];
    32         yy = y + t[i][1];
    33         if(xx >= 0 && xx < 9 && yy >= 0 && yy < 9 && a[xx][yy]=='o')
    34         {
    35             memset(vis, 0, sizeof(vis));        //四个方向的块,分开判断,vis每次都需初始化
    36             if(ff(xx, yy)==0) return 1;
    37         }
    38     }
    39     return 0;
    40 }
    41 
    42 int main()
    43 {
    44     int t, i, j, num, ans;
    45     scanf("%d", &t);
    46     getchar();
    47     num = 0;
    48     while(t--)
    49     {
    50         for(i=0; i<n; i++)
    51         {
    52             scanf("%s", a[i]);
    53         }
    54 
    55         ans = 0;
    56         memset(vis, 0, sizeof(vis));
    57         for(i=0; i<n; i++)
    58         {
    59             for(j=0; j<n; j++)
    60             {
    61                 if(a[i][j]=='.')
    62                 {
    63                     a[i][j] = 'x';
    64                     if(f(i, j))
    65                     {
    66                         ans = 1;
    67                         break;
    68                     }
    69                     a[i][j] = '.';
    70                 }
    71             }
    72         }
    73         if(ans) printf("Case #%d: Can kill in one move!!!
    ", ++num);
    74         else printf("Case #%d: Can not kill in one move!!!
    ", ++num);
    75     }
    76     return 0;
    77 }
  • 相关阅读:
    经典排序算法--快速排序
    经典排序算法——希尔排序
    经典排序算法——插入排序
    经典排序算法——选择排序
    经典排序算法——冒泡排序
    java递归求八皇后问题解法
    学习当前流行框架,同时也要自己去造建议的框架
    如何提高服务器并发处理能力
    数据的校验和空指针
    编写程序流程
  • 原文地址:https://www.cnblogs.com/0xiaoyu/p/11330509.html
Copyright © 2011-2022 走看看