[BFS/DFS]HOJ2581Go

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

傳送門:Go

Go

My Tags   (Edit)
  Source : Stanford Programming Contest 2007
  Time limit : 1 sec   Memory limit : 64 M

Submitted : 201, Accepted : 117

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.

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.

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(1 ≤ n ≤ 19), b and w(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(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 01 12 0 11 15 12 41 1 1 2 1 3 2 1 2 3 3 1 3 3 4 1 4 3 5 1 5 2 5 31 4 2 4 3 4 3 50

Sample Output

DrawWhite wins by 3Black wins by 1

解題報告:

此題可以用DFS/BFS做。題意下圍棋時黑子和白子哪個占的多,且黑白子不會重複在一個點。此題化簡一下,把黑子占的地方賦值為1,白子占的地方賦值為2.空白的地方首先為0.

做法是:找到一個為0的點,進行BFS/DFS,把訪問過的點賦值為3.當碰見點為1的就說明這塊是黑子的,為2就說明為白子的。最後比較大小即可。


DFS:

#include<iostream>#include<stack>#include<cstdio>#include<cstring>using namespace std;int maps[25][25];struct node{    int x,y;}st;int dx[4]={0,1,0,-1};int dy[4]={1,0,-1,0};int flagb,flagw,numb,numw,num;void dfs(node st,int n){    stack<node> q;    int i;    node a,b;    while(!q.empty()){        q.pop();    }    flagb=0;flagw=0;num=1;    q.push(st);    maps[st.x][st.y]=3;    while(!q.empty()){        a=q.top();        q.pop();        for(i=0;i<4;i++){            b.x=a.x+dx[i];            b.y=a.y+dy[i];            if(b.x>0&&b.x<=n&&b.y>0&&b.y<=n){                if(maps[b.x][b.y]==0){                    maps[b.x][b.y]=3;                    q.push(b);                    num++;                }                else if(maps[b.x][b.y]==1)                    flagb=1;                else if(maps[b.x][b.y]==2)                    flagw=1;            }        }    }}int main(){    int n,b,w,i,j,x,y;    while(scanf("%d%d%d",&n,&b,&w)==3){        flagb=0;flagw=0;numb=0;numw=0;num=0;        memset(maps,0,sizeof(maps));        //黑子為1        for(i=1;i<=b;i++){            scanf("%d%d",&x,&y);            maps[x][y]=1;        }        //白子為2        for(i=1;i<=w;i++){            scanf("%d%d",&x,&y);            maps[x][y]=2;        }        for(i=1;i<=n;i++){            for(j=1;j<=n;j++){                if(maps[i][j]==0){                    st.x=i;                    st.y=j;                    dfs(st,n);                    if(flagb==0&&flagw==1)                        numw+=num;                    if(flagb==1&&flagw==0)                        numb+=num;                }            }        }        if(numw==numb)            printf("Draw\n");        else if(numw>numb)            printf("White wins by %d\n",numw-numb);        else            printf("Black wins by %d\n",numb-numw);    }    return 0;}


BFS:

#include<iostream>#include<queue>#include<cstdio>#include<cstring>using namespace std;int maps[25][25];struct node{    int x,y;}st;int dx[4]={0,1,0,-1};int dy[4]={1,0,-1,0};int flagb,flagw,numb,numw,num;void bfs(node st,int n){    queue<node> q;    int i;    node a,b;    while(!q.empty()){        q.pop();    }    flagb=0;flagw=0;num=1;    q.push(st);    maps[st.x][st.y]=3;    while(!q.empty()){        a=q.front();        q.pop();        for(i=0;i<4;i++){            b.x=a.x+dx[i];            b.y=a.y+dy[i];            if(b.x>0&&b.x<=n&&b.y>0&&b.y<=n){                if(maps[b.x][b.y]==0){                    maps[b.x][b.y]=3;                    q.push(b);                    num++;                }                else if(maps[b.x][b.y]==1)                    flagb=1;                else if(maps[b.x][b.y]==2)                    flagw=1;            }        }    }}int main(){    int n,b,w,i,j,x,y;    while(scanf("%d%d%d",&n,&b,&w)==3){        flagb=0;flagw=0;numb=0;numw=0;num=0;        memset(maps,0,sizeof(maps));        //黑子為1        for(i=1;i<=b;i++){            scanf("%d%d",&x,&y);            maps[x][y]=1;        }        //白子為2        for(i=1;i<=w;i++){            scanf("%d%d",&x,&y);            maps[x][y]=2;        }        for(i=1;i<=n;i++){            for(j=1;j<=n;j++){                if(maps[i][j]==0){                    st.x=i;                    st.y=j;                    bfs(st,n);                    if(flagb==0&&flagw==1)                        numw+=num;                    if(flagb==1&&flagw==0)                        numb+=num;                }            }        }        if(numw==numb)            printf("Draw\n");        else if(numw>numb)            printf("White wins by %d\n",numw-numb);        else            printf("Black wins by %d\n",numb-numw);    }    return 0;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.