zoukankan      html  css  js  c++  java
  • uva 12549

    12549 - Sentry Robots

    Time limit: 1.000 seconds

    We need to guard a set of points of interest using sentry robots that can
    not move or turn. We can position a sentry at any position facing either
    north, south, east or west. Once a sentry is settled, it guards the points of
    interest that are infront of it. If two or more points are in the same row
    or column a single robot can guard them all. Unfortunately, there are also
    some obstacles that the robot cannot see through.
    From a set of points of interest and obstacles lying on a grid, calculate
    the minimum number of robots needed to guard all the points. In order to guard a point of interest, a
    robot must be facing the direction of this point and must not be any obstacles in between.
    Given the following grid, where # represents an obstacle and * a point of interest, the minimum
    number of robots needed is 2 (a possible position and orientation is shown using arrows for each robot).
    Note that this is not the actual input or output, just a gure.
    For the following grid we need 4 robots because of the obstacles.
    Input
    The rst line of the input has an integer C representing the number of test cases that follow. Before
    each test case there is an empty line.
    For each case, the rst line has 2 integers, Y and X, representing the height and width of the grid.
    The next line has an integer that indicates the number of points of interest P. The following P lines
    will have the positions py and px of the points of interest, one point per line. The next line has an
    integer that indicates the number of obstacles W. The following W lines will have the positions wy
    and wx of an obstacle, one per line.
    Output
    For each test case print the minimum number of robots needed to guard all the points of interest, one
    per line.
    CONSTRAINTS:
    1  C  50
    1  Y; X  100
    0  P  Y X
    0  W  Y X
    0  P + W  Y X
    1  px; wx  X
    1  py; wy  Y
    Sample Input
    2
    4 6
    4
    2 2
    2 4
    4 2
    4 4
    3
    2 3
    3 3
    4 3
    4 5
    6
    1 2
    1 3
    2 4
    2 2
    3 3
    4 3
    2
    2 3
    3 2
    Sample Output
    2
    4

    正解好像是二分图匹配,然而我直接贪了一贪,可能是数据水,思路见代码。

    #include <cstdio>
    #include <iostream>
    #include <sstream>
    #include <cmath>
    #include <cstring>
    #include <cstdlib>
    #include <string>
    #include <vector>
    #include <map>
    #include <set>
    #include <queue>
    #include <stack>
    #include <algorithm>
    using namespace std;
    #define ll long long
    #define _cle(m, a) memset(m, a, sizeof(m))
    #define repu(i, a, b) for(int i = a; i < b; i++)
    #define repd(i, a, b) for(int i = b; i >= a; i--)
    #define sfi(n) scanf("%d", &n)
    #define pfi(n) printf("%d
    ", n)
    #define sfi2(n, m) scanf("%d%d", &n, &m)
    #define pfi2(n, m) printf("%d %d
    ", n, m)
    #define pfi3(a, b, c) printf("%d %d %d
    ", a, b, c)
    #define MAXN 105
    const int INF = 0x3f3f3f3f;
    int mp[MAXN][MAXN];
    int tot;
    int main()
    {
        int c, p, w, x, y, px, py;
        sfi(c);
        while(c--)
        {
            sfi2(y, x);
            y++, x++;
            repu(i, 1, y) repu(j, 1, y) mp[i][j] = 1;
            sfi(p);
            repu(i, 0, p)
            {
                sfi2(px, py);
                mp[px][py] = 2;
            }
    
            sfi(w);
            repu(i, 0, w)
            {
                sfi2(px, py);
                mp[px][py] = 3;
            }
            tot = 0;
            int t1, t2;
            repu(i, 1, y) repu(j, 1, x)
            if(mp[i][j] == 2)
            {
                tot++;
                t1 = t2 = 0;
                repu(q, i + 1, y)
                if(mp[q][j] == 2) t1++;
                else if(mp[q][j] == 3) break;
                repu(q, j + 1, x)
                if(mp[i][q] == 2) t2++;
                else if(mp[i][q] == 3) break;
                if(t1 < t2)
                {
                    repu(q, j, x)
                    if(mp[i][q] == 2) mp[i][q] = 1;
                    else if(mp[i][q] == 3) break;
                }
                else
                {
                    repu(q, i, y)
                    if(mp[q][j] == 2) mp[q][j] = 1;
                    else if(mp[q][j] == 3) break;
                }
            }
            pfi(tot);
        }
        return 0;
    }
    View Code
  • 相关阅读:
    前端学习笔记之BOM和DOM
    JAVA学习笔记之图解JAVA参数传递
    Python学习笔记之函数参数传递 传值还是传引用
    Java学习笔记之对象的复制和克隆
    如何科学正确的使用搜索引擎
    JAVA学习笔记之JAVA 对象引用以及赋值
    前端学习笔记之Z-index详解
    Python面试题目之Python的复制和赋值浅析
    Python面试题目之(针对dict或者set数据类型)边遍历 边修改 报错dictionary changed size during iteration
    判断对象是否为数组/函数
  • 原文地址:https://www.cnblogs.com/sunus/p/4839564.html
Copyright © 2011-2022 走看看