码迷,mamicode.com
首页 > 其他好文 > 详细

uva 12549

时间:2015-09-25 22:59:58      阅读:266      评论:0      收藏:0      [点我收藏+]

标签:

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", n)
#define sfi2(n, m) scanf("%d%d", &n, &m)
#define pfi2(n, m) printf("%d %d\n", n, m)
#define pfi3(a, b, c) printf("%d %d %d\n", 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

 

uva 12549

标签:

原文地址:http://www.cnblogs.com/sunus/p/4839564.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!