[Explanations] grid game

Title Description

        Alice and Bob play an old game: a first draw an n × n matrix (below n = 3)

        Then they take turns between two adjacent dots painted on the red side and blue side:

        

        Until surrounded by a closed circle (area need not be 1) so far, "seals" the man who is the winner. Because the board is too big (n ≤ 200), their game is too long! They even do not know who in the game to win the game. So you write a program to help them calculate whether they ended the game?

 

Input Format

        Behavior data input of the first two integers n and m. m represents a total of m lines drawn. After the m rows, each first two numbers (x, y), the coordinates of the start point represents a line drawing, separated by a space followed by a character, if the character is "D", it is a downward edge even if it is "R" is not even a right side. There will be no repeat of the input data and to ensure that the correct side.

 

Output Format

        Output line: end of the first few times. If no step after the end of m, then the output line "draw".

 

SAMPLE INPUT

3 5

1 1 D

1 1 R

1 2 D

2 1 R

2 2 D

 

Sample Output

4

 

answer

        It looks like a lot of trouble, but in fact can still be directly set disjoint-set.

        As can be seen, form a closed ring if the point and the point $ $ $ i $ J after connection, then the point and the point $ $ $ i $ J necessarily $ K $ can communicate points, we only need disjoint-set determination point $ i $ point and $ j $ are in the same collection where you can know at this point whether there is a $ k $.

#include <iostream>

#define MAX_N (200 + 5)

using namespace std;

struct Node
{
    int x, y;
    inline friend bool operator == (Node x, Node y) {return x.x == y.x && x.y == y.y;}
    inline friend bool operator != (Node x, Node y) {return !(x == y);}
};

int n, m;
Node r[MAX_N][MAX_N];

Node Root(Node x)
{
    Node R = x, tmp;
    while(R != r[R.x][R.y]) R = r[R.x][R.y];
    while(x != r[x.x][x.y]) tmp = r[x.x][x.y], r[x.x][x.y] = R, x = tmp;
    return R;
}

int main()
{
    cin >> n >> m;
    char ch;
    Node u, v;
    for(register int i = 1; i <= n; ++i)
    {
        for(register int j = 1; j <= n; ++j)
        {
            u = (Node){i, j};
            r[i][j] = u;
        }
    }
    for(register int i = 1; i <= m; ++i)
    {
        cin >> u.x >> u.y >> ch;
        v = u;
        if(ch == 'D') ++v.x;
        else ++v.y;
        if(Root(u) == Root(v)) return cout << i, 0;
        r[Root(u).x][Root(u).y] = Root(v);
    }    
    cout << "draw";
    return 0;
}
Reference program

 

Guess you like

Origin www.cnblogs.com/kcn999/p/10990273.html