24h購物| | PChome| 登入
2013-10-11 08:28:37| 人氣3,045| 回應0 | 上一篇 | 下一篇

[UVA][離散化&dp] 1092 - Tracking Bio-bots

推薦 0 收藏 0 轉貼0 訂閱站台

The researchers at International Bio-bot Makers (IBM) have invented a new kind of Bio-bot, a robot with behavior mimicking biological organisms. The development of the new robot is at a primitive stage; they now resemble simple four-wheeled rovers. And like most modern robots, Bio-bots are not very mobile. Their weak motors and limited turning capability put considerable limitations on their movement, even in simple, relatively obstacle-free environments.

Currently the Bio-bots operate in a room which can be described as an m x n grid. A Bio-bot occupies a full square on this grid. The exit is in the northeast corner, and the room slopes down towards it, which means the Bio-bots are only capable of moving north or east at any time. Some squares in the room are also occupied by walls, which completely block the robot. Figure 1, which corresponds to the sample input, shows an example of such a room.

epsfbox{p4787.eps}

Clearly, a Bio-bot located on square A is capable of leaving the room, while one at square B is trapped inside it, no matter what it does. Locations like B are called ``stuck squares." (Walls do not count as stuck squares.) Given the description of a room, your job is to count the total number of stuck squares in the room.

Input 

Input consists of multiple test cases, each describing one room. Each test case begins with a line containing three integers m, n, and w (1$ le$m, n$ le$106, 0$ le$w$ le$1000). These indicate that the room contains m rows, n columns, and w horizontal walls.

Each of the next w lines contains four integers x1, y1, x2, y2, the coordinates of the squares delimiting one wall. All walls are aligned from west to east, so 0$ le$x1$ le$x2 < n and 0$ le$y1 = y2 < m. Walls do not overlap each other. The southwest corner of the room has coordinates (0,0) and the northeast corner has coordinates (n - 1, m - 1).

The last test case is followed by a line containing three zeros.

Output 

For each test case, display one line of output containing the test case number followed by the number of stuck squares in the given room. Follow the format shown in the sample output.

Sample Input 

8 8 3 
1 6 3 6 
2 4 2 4 
4 2 7 2 
0 0 0

Sample Output 

Case 1: 8



題目描述:

只能往右往上走,求圖中有多少點無法抵達右上角。

題目解法:


首先,先將 x, y 軸離散化,用 dp 推回去找到可以抵達的點。

對於不能抵達的點,將離散化的結果拿出來計算該點有多少格子。

#include <stdio.h>
#include <string.h>
#include <map>
#include <algorithm>
using namespace std;
char g[2010][2010], dp[2010][2010];
int lx[4010], rx[4010], ly[4010], ry[4010];
long long x[4010], y[4010];
int main() {
    //freopen("in.txt","r+t",stdin);
    //freopen("out.txt","w+t",stdout);
    int cases = 0;
    int m, n, w;
    int i, j, k, p, q, r;
    while(scanf("%d %d %d", &m, &n, &w) == 3 && n+m) {
        map<int, int> Rx, Ry;
        Rx[0] = 1, Ry[0] = 1;
        Rx[n] = 1, Ry[m] = 1;
        for(i = 0; i < w; i++) {
            scanf("%d %d %d %d", &lx[i], &ly[i], &rx[i], &ry[i]);
            rx[i]++, ry[i]++;
            Rx[lx[i]] = 1, Ry[ly[i]] = 1;
            Rx[rx[i]] = 1, Ry[ry[i]] = 1;
        }
        int sizex = 0, sizey = 0;
        for(map<int, int>::iterator it = Rx.begin();
            it != Rx.end(); it++) {
            it->second = sizex;
            x[sizex++] = it->first;
        }
        for(map<int, int>::iterator it = Ry.begin();
            it != Ry.end(); it++) {
            it->second = sizey;
            y[sizey++] = it->first;
        }
        memset(g, 0, sizeof(g));
        memset(dp, 0, sizeof(dp));
        for(i = 0; i < w; i++) {
            int llx = Rx[lx[i]], rrx = Rx[rx[i]];
            int rry = Ry[ry[i]];
            for(p = llx+1; p <= rrx; p++)
                g[p][rry] = 1;
        }
        dp[sizex-1][sizey-1] = 1;
        long long ret = 0;
        for(i = sizex-1; i > 0; i--) {
            for(j = sizey-1; j > 0; j--) {
                if(g[i][j])
                    continue;
                if(!dp[i][j])
                    ret += (x[i]-x[i-1])*(y[j]-y[j-1]);
                dp[i-1][j] |= dp[i][j];
                dp[i][j-1] |= dp[i][j];
            }
        }
        printf("Case %d: %lld\n", ++cases, ret);
    }
    return 0;
}

台長: Morris
人氣(3,045) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 教育學習(進修、留學、學術研究、教育概況) | 個人分類: UVA |
此分類下一篇:[UVA] 1039 - Simplified GSM Network
此分類上一篇:[UVA][積分&二分] 1280 - Curvy Little Bottles

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文