4月 23, 2009

【解題】The Swallowing Ground

@
ACM Volume CIX 10963 - The Swallowing Ground


The Problem

Now that the wicked is punished, there is still work to do. For example, the ground needs to seal itself up. (Otherwise, good people will accidentally fall into Hell, which is not good.)

The ground swallowed Don Giovanni by collapsing: Part of the ground collapsed--fell off to Hell--and this created a gap that swallowed Don Giovanni. The only way the ground can be fixed is by sliding landmasses to close up the gap. Here is an example. The left diagram shows the ground with a ravine. Assume that the ground extends infinitely to the north and to the south, and there are seams on the east and west ends; so the northern block can slide southward, and the southern block can slide northward. The right diagram shows the gap closed.



Here is another example. The ravine in the left diagram cannot be closed perfectly by sliding the ground northward and southward. The right diagram shows the result of the best effort: there is still a hole.



Write a program to determine if the gap can be closed. The ground is a grid with rows (a row runs east-west) and columns (a column runs north-south). Holes are cells on the grid, and the gap consists of holes.


Input

The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs.

The first line contains the number of columns W of the ground to be considered. Then W lines follow, specifying the boundary of the gap by columns: a line represents a column, and the order of the lines follows the east-to-west order. Each line contains y1, the north-most row of the gap in this column, and y2, the south-most row of the gap in this column. Row numbers are integers between -100 and 100 inclusive.

The input specifies a valid gap: the holes separate the ground into exactly a northern block and a southern block. There are no holes embedded inside a block. There are no islands.

Note: The sample input below corresponds to the first diagram.


Output

For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.

Output the line

yes

if the gap can be closed by sliding blocks north and south; and

no

otherwise.


Sample Input

1

5
2 1
0 -1
1 0
1 0
2 1


Sample Output

yes


解題思考

  其實這一題分析起來,所謂的「裂口是否能接合」,即要你判斷是否每一行的 y1 - y2 皆相等。

  只要利用這個想法,用迴圈進行判定,就是題目要求的答案了。


參考解答(C++)

#include <iostream>

using namespace std;

int main(void)
{
    int n;

    cin >> n;
    for (int i = 0; i < n; i++)
    {
        if (i) { cout << endl; }

        int w;
        cin >> w;

        int y[2];
        cin >> y[0] >> y[1];

        int hole = y[0] - y[1];
        bool ok = true;
        for (int j = 1; j < w; j++)
        {
            // 判斷洞口距離是否都相等
            cin >> y[0] >> y[1];
            if (j && ok)
            {
                ok = ((y[0] - y[1]) == hole);
            }
        }

        cout << (ok ? "yes" : "no") << endl;
    }

#ifndef ONLINE_JUDGE
    system("pause");
#endif
}

0 回覆:

張貼留言