zoukankan      html  css  js  c++  java
  • Ancient Go hdu5546 (DFS)

    http://acm.hdu.edu.cn/showproblem.php?pid=5546

    题意:现在Yu Zhou 和 Su Lu在下棋。Yu Zhou执黑子'x', Su Lu执白子'o'。下棋规则是这样的:若某一方能将另一方的(0<N)棋子全部围起来,则表示他吃了了这枚棋子。现在该Yu Zhou执黑子的走了,问他能不能在一步之内吃掉至少一枚白子。

    分析:若白子周围有两个及其以上'.',那么黑子一定不能够吃掉白子。但需注意,几枚白子在一块的情况

    #include <iostream>
    #include <stdio.h>
    #include <string.h>
    #include <string>
    #include <vector>
    #include <algorithm>
    #include <map>
    #include <queue>
    #include <stack>
    #include <math.h>
    
    using namespace std;
    
    #define INF 0x3f3f3f3f
    const int maxn = 10;
    typedef long long LL;
    char maps[maxn][maxn];
    int v[maxn][maxn];
    int dir[4][2]={{0, 1},{1, 0},{0, -1},{-1, 0}};
    
    int DFS(int x, int y)
    {
    
           v[x][y]=1;
           int ans = 0;
    
           for(int i=0; i<4; i++)
           {
               int nx=dir[i][0]+x;
               int ny=dir[i][1]+y;
    
               if(nx>=0 && nx<9 && ny>=0 && ny<9 && !v[nx][ny])
               {
                   if(maps[nx][ny]=='.')
                   {
                       v[nx][ny] = 1;
                       ans ++;
                   }
                   else if(maps[nx][ny]=='o') ans += DFS(nx, ny);///判断若四周全部被白子包围的情况
               }
           }
    
         return ans;
    }
    
    int main()
    {
        int T, cnt=1, ans, flag;
    
        scanf("%d", &T);
    
        while(T --)
        {
            flag = 0;
    
            for(int i=0; i<9; i++)
                scanf("%s", maps[i]);
    
            for(int i=0; i<9; i++)
            {
                for(int j=0; j<9; j++)
                {
                    memset(v, 0, sizeof(v));
    
                    if(maps[i][j]=='o')///若为白子,则对它进行DFS,判断它的周围有多少个'.',若小于2则它一定被围住
                    {
                        ans=DFS(i, j);
                        if(ans<=1) flag = 1;
                    }
    
                    if(flag) break;
                }
                if(flag) break;
            }
    
            if(flag) printf("Case #%d: Can kill in one move!!!
    ", cnt++);
            else printf("Case #%d: Can not kill in one move!!!
    ", cnt++);
        }
        return 0;
    }
    
    /*
    1
    ......ox.
    .......o.
    ...o.....
    ..o.o....
    .x.o.....
    ooo......
    ooo....o.
    ...x.....
    ........o
    */
    View Code
  • 相关阅读:
    1-hadoop中遇到的各种异常
    13-hadoop-入门程序
    12-mapReduce的简介和yarn搭建
    11-hdfs-NameNode-HA-wtihQJM解决单点故障问题
    10-hdfs-hdfs搭建
    redis-java-api
    深度学习优化方法
    tf.nn.embedding_lookup()
    tf.variable_scope()和tf.name_scope()
    tf.Variable()、tf.get_variable()和tf.placeholder()
  • 原文地址:https://www.cnblogs.com/daydayupacm/p/5777926.html
Copyright © 2011-2022 走看看