zoukankan      html  css  js  c++  java
  • hdu 4158 GO (广搜BFS)

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

    Problem Description
    In the game of Go, two players alternate placing black and white stones on lattice points of an n × n grid, each attempting to surround as much territory (i.e., regions of unfilled lattice points) as possible. At the end of the game, the score for each player is the total area of the territory surrounded by his or her stones. Given the locations of black and white stones on a Go board at the end of a match, your task is to compute the score of each player in order to determine the winner.1
    Formally, two grid lattice points with coordinates (r, c) and (r′, c′) are adjacent if |r - r′| + |c - c′| = 1. A connected region of unfilled lattice points belongs to one player’s territory if all adjacent filled lattice points contain stones belonging to that player (see Figure 1). Finally, a player’s score consists of the number of unfilled lattice points in his or her territory.

    Note that the scoring of Go boards described here does not correspond exactly to the real game of Go: we make the simplifying assumptions that all “disputes” have been settled so that any territories surrounded by stones of both colors are considered neutral, and that all groups on the board are considered “alive.”

    Figure 1: Diagram of a 9 × 9 Go board. Unfilled lattice points belonging to black’s territory are marked with B, and unfilled lattice points belonging to white’s territory are marked with W. Neutral unfilled lattice points are unmarked. In the game above, white wins by 21-3 = 18.
     
    Input
    The input test file will contain multiple cases, each consisting of three lines. Each test case begins with a line containing three integers, n (where 1 ≤ n ≤ 19), b, and w (where b ≥ 0,w ≥ 0 and 1 ≤ b + w ≤ n2). Here, n denotes the size of the board, b is the number of black pieces placed, and w is the number of white pieces placed. The second line of each test case contains b pairs of integers r1 c1 . . . rb cb (where 1 ≤ ri , ci ≤ n) indicating the positions of the b black stones. The third line of each test case contains w pairs of integers r`1 c`1 . . . r`w c`w (where 1 ≤ r`i , c`i ≤ n) indicating the positions of the w white stones. No two stones will be located at the same lattice point. Input is terminated by a single line containing only the number 0; do not process this line.
     
    Output
    For each test case, print either “White wins by ”, “Black wins by ”, or “Draw”.
     
    Sample Input
    1 1 0 1 1 2 0 1 1 1 5 12 4 1 1 1 2 1 3 2 1 2 3 3 1 3 3 4 1 4 3 5 1 5 2 5 3 1 4 2 4 3 4 3 5 0
     
    Sample Output
    Draw White wins by 3 Black wins by 1
     
    简单广搜:注意好标记就ok
     
    代码:
     1 #include<iostream>
     2 #include<queue>
     3 using namespace std;
     4 int map[20][20];
     5 int nb,nw;
     6 struct Nod
     7 {
     8     int x,y;
     9 }d,p;
    10 int cx[]={1,-1,0,0};
    11 int cy[]={0,0,-1,1};
    12 int n;
    13 void bfs(int x,int y)
    14 {
    15     int num=0;
    16     queue<Nod> q;
    17     d.x=x;
    18     d.y=y;
    19     q.push(d);
    20     map[x][y]=3;
    21     int flag1=0,flag2=0;
    22     while(!q.empty())
    23     {
    24         p=q.front();
    25         q.pop();
    26         num++;
    27         int i;
    28         for(i=0;i<4;i++)
    29         {
    30             d.x=p.x+cx[i];
    31             d.y=p.y+cy[i];
    32             if(d.x>=1&&d.x<=n&&d.y>=1&&d.y<=n)
    33             {
    34                 if(map[d.x][d.y]==0)
    35                 {
    36                     q.push(d);
    37                     map[d.x][d.y]=3;
    38                 }
    39                 else if(map[d.x][d.y]==1)
    40                     flag1=1;
    41                 else if(map[d.x][d.y]==2)
    42                     flag2=1;
    43             }
    44         }
    45     }
    46     if(flag1+flag2!=2)
    47     {
    48         if(flag1)
    49             nb+=num;
    50         else if(flag2)
    51             nw+=num;
    52     }
    53 }
    54 int main()
    55 {
    56     int b,w;
    57     while(~scanf("%d",&n),n)
    58     {
    59         scanf("%d%d",&b,&w);
    60         memset(map,0,sizeof(map));
    61         int i,x,y,j;
    62         for(i=0;i<b;i++)
    63         {
    64             scanf("%d%d",&x,&y);
    65             map[x][y]=1;
    66         }
    67         for(i=0;i<w;i++)
    68         {
    69             scanf("%d%d",&x,&y);
    70             map[x][y]=2;
    71         }
    72         nb=nw=0;
    73         for(i=1;i<=n;i++)
    74             for(j=1;j<=n;j++)
    75                 if(!map[i][j])
    76                     bfs(i,j);
    77         if(nb==nw)
    78             puts("Draw");
    79         else if(nb<nw)
    80             printf("White wins by %d\n",nw-nb);
    81         else
    82             printf("Black wins by %d\n",nb-nw);
    83     }
    84     return 0;
    85 }
  • 相关阅读:
    IFNULL和isnull用法
    Python 进制转换 二进制 八进制 十进制 十六进制
    xhr是什么文件类型?
    from __future__ import unicode_literals
    sort is deprecated, use sort_values(inplace=True) for INPLACE sorting
    Autodesk View and Data API二次开发学习指南
    设置Mac 中保存对话框默认为扩展窗口
    [大数据学习研究] 错误排查,Hadoop集群部分DataNode不能启动
    IDEA 环境下更改Maven的仓库镜像提高下载速度
    [大数据学习研究] 4. Zookeeper-分布式服务的协同管理神器
  • 原文地址:https://www.cnblogs.com/crazyapple/p/2663666.html
Copyright © 2011-2022 走看看