UVA10998 POJ2857 Flipping ColorsDFS

Posted 海岛Blog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UVA10998 POJ2857 Flipping ColorsDFS相关的知识,希望对你有一定的参考价值。

Flipping Colors
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 749 Accepted: 371

Description

A rectangle with sides parallel to the x-y axes and its left-lower corner at (0, 0) is being painted. The rectangle may be thought of as a flat-screen display with almost infinite resolution; initially the entire rectangle is black. Two numbers are given 0 < h, v < 1 and then

A vertical line is drawn dividing the horizontal sides of the rectangle in proportion h:1−h from the left.
A horizontal line is drawn dividing the vertical sides of the rectangles in proportion v:1−v from the bottom up.
These two lines divide the rectangle into four smaller rectangles.
The upper left and the lower right sub-rectangles remain intact.
The color of the other two rectangles is flipped (from black to white or from white to black) and now each of them is subject to the operation just performed on the bigger rectangle.
This process continues (in principle) forever.
Given a point in the original rectangle but not on the boundary of any rectangle that turns up in the process of painting, determine the color of the point.
在这里插入图片描述

Input

Input contains multiple cases. The first line of each case contains 4 numbers, the length of the rectangle H, the height of the rectangle V and then the numbers h and v. The next line contains one integer number n, the number of points to consider. The following n lines contain two numbers each, the x and the y coordinate of a point.

Output

Organize your output as shown in the sample. For each point from input print the color of the point.

Sample Input

81 32 0.333333333333 0.5
6
16 30
16 25
16 12.0001
16 11.9999
16 7.987654321
16 7.0123456789
10 10 0.123456789 0.987654321
2
0.432 0.9876
9.432 0.9876
0 0 0 0

Sample Output

Case 1:
black
black
white
black
white
white
Case 2:
white
black

Source

Waterloo local 2006.02.25

问题链接UVA10998 POJ2857 Flipping Colors
问题简述:(略)
问题分析:DFS问题,不解释。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* UVA10998 POJ2857 Flipping Colors */

#include <iostream>
#include <cstdio>

using namespace std;

double dfs(double h, double v, double h2, double v2, double x, double y, int c)
{
    double hh = h * h2;
    double vv = v * v2;
    if (x <= hh && y >= vv)	return c; // upper left
    if (x >= hh && y <= vv)	return c; // lower right
    if (x <= hh) 	// lower left
        return dfs(h * h2, v * v2, h2, v2, x, y, 1 - c);
    else			// upper right
        return dfs(h * (1 - h2), v * (1 - v2), h2, v2, x - hh, y - vv, 1 - c);
}

int main()
{
    double h, v, h2, v2, x, y;
    int n, caseno = 0;
    while (scanf("%lf%lf%lf%lf", &h, &v, &h2, &v2) == 4 && h) {
        scanf("%d", &n);

        printf("Case %d:\\n", ++caseno);
        for (int i = 0; i < n; i++) {
            scanf("%lf%lf", &x, &y);
            int ans = dfs(h, v, h2, v2, x, y, 1);
            puts(ans ? "black" : "white");
        }
    }

    return 0;
}

以上是关于UVA10998 POJ2857 Flipping ColorsDFS的主要内容,如果未能解决你的问题,请参考以下文章

UVA 322 ships (POJ 1138)

2018 Spring Single Training B (uva 572,HihoCoder 1632,POJ 2387,POJ 2236,UVA 10054,HDU 2141)

2019年7月做题记录

POJ 1015 / UVA 323 Jury Compromise(01背包,打印路径)

UVA11115 POJ3199 Uncle Jack大数

UVA10360 POJ1916 Rat Attack枚举