[POJ] [1328] Radar Installation

Problem Description

Loading...

Problem Analysis

Because the radar must be installed on the x-axis
so if y>d, output -1

Draw a circle centered at the point with radius d
intersecting the x-axis at two points
If the radar lies between the two points
it can cover this point

Since the intervals may overlap
we only need to place points in the overlapping part
to cover as many points as possible

Thus the problem can be transformed into
given several intervals
how many points are needed so that each interval contains at least one point

So we arrive at a greedy strategy
sort the intervals by their right endpoints
when comparing, assume a radar is built at the first right endpoint
mark its coordinate as t
then if the left endpoint of the next interval is after this point
update t to the right endpoint of the next interval
and increment cnt++

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include<stdio.h>
char map[120][120];
int cnt[120][120];
int n,m,resx,resy;
bool win=false;
int move(int x,int y,int mcnt) {
//  printf("%d-%d-%c\n",x,y,map[x][y]);
    if(x<0||x==n||y<0||y==m) {
        win=true;
        return mcnt-1;
    }
    if(cnt[x][y]) {
        win=false;
        resx=x;
        resy=y;
        return mcnt-1;
    }
    cnt[x][y]=mcnt;
    if(map[x][y]=='N')
        return move(x-1,y,mcnt+1);
    else if(map[x][y]=='S')
        return move(x+1,y,mcnt+1);
    else if(map[x][y]=='W')
        return move(x,y-1,mcnt+1);
    else if(map[x][y]=='E')
        return move(x,y+1,mcnt+1);
}
int main() {
    int k;
    while(scanf("%d %d %d",&n,&m,&k),n||m||k) {
        getchar();
        for(int i=0; i<n; i++) {
            for(int j=0; j<m; j++) {
                cnt[i][j]=0;
                scanf("%c",&map[i][j]);
            }
            getchar();
        }
        int res=move(0,k-1,1);
        if(win)
            printf("%d step(s) to exit\n",res);
        else
            printf("%d step(s) before a loop of %d step(s)\n",cnt[resx][resy]-1,res-cnt[resx][resy]+1);
    }
    return 0;
}
This post is licensed under CC BY-NC-SA 4.0. If it helped, or if you have anything to say, the comment box below is open.
Page views —
Unique visitors —

Loading...