[Bfs/dfs] Hoj2581go

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Portal: Go

Go

My Tags (Edit)
 
source  : stanford Programming Contest
Time Limit : 1 sec Memory limit : M

submitted : 201, Accepted : 117

In The game of Go, players alternate placing black and white stones in lattice points of an n * n grid, each attempting to surround a s much territory (i.e., regions of unfilled lattice points) as possible. At the end of the game, the score for each player are the total area of the territory surrounded by he or her stones. Given the locations of black and white stones on a Go board at the end of a match, your task was to compute the score of EA CH Player in order to determine the winner.

Formally, both grid lattice points with coordinates (R, c) and (R ', C ') is 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 cont Ain Stones belonging to this 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 is marked with B, and unfilled lattice points belonging to White ' s Territory is marked with W. Neutral unfilled lattice points is unmarked. In the game above, White wins by 21-3 = 18.

Input

The input test file would contain multiple cases, each consisting of three lines. Each test case is 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, and B is the number of black pieces placed, and W are the number of white pieces placed. The second line of all test case contains B pairs of integers r1 C1 ... RB CB (where 1≤ri, ci≤n) indicating the PO Sitions of the B black stones. The third line of all test case contains w pairs of integers R ' 1 C ' 1 ... r ' W C ' W (1≤r ' I, C ' I≤n) indicating the Positio NS of the W white stones. No. Stones'll be located at the same lattice point. Input is terminated by a, containing only the number 0; Do not process this line.

Output

For each test case, the 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

Problem Solving Report:

This problem can be done with DFS/BFS. Test instructions go when the sunspot and white son which accounted for more, and black and white children will not repeat at a point. This simplification, the location of the sunspot accounted for 1, the white child accounted for the local assignment of 2. The blank place is first 0.

The practice is to find a point of 0, make Bfs/dfs, and assign a value of 3 to the point visited. When the meeting point is 1, it means that the block is a sunspot, and 2 is the white one. The final comparison size can be.


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));            Sunspots are 1 for (i=1;i<=b;i++) {scanf ("%d%d", &x,&y);        Maps[x][y]=1;            }//Shirako is 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", &AMP;N,&AMP;B,&AMP;W) ==3) {flagb=0;flagw=0;numb=0;numw=0;num=0;        memset (maps,0,sizeof (maps));            Sunspots are 1 for (i=1;i<=b;i++) {scanf ("%d%d", &x,&y);        Maps[x][y]=1;            }//Shirako is 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;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.