zoukankan      html  css  js  c++  java
  • 18.06.27 POJ1054 The Troublesome Frog

    描述

    In Korea, the naughtiness of the cheonggaeguri, a small frog, is legendary. This is a well-deserved reputation, because the frogs jump through your rice paddy at night, flattening rice plants. In the morning, after noting which plants have been flattened, you want to identify the path of the frog which did the most damage. A frog always jumps through the paddy in a straight line, with every hop the same length:
    Your rice paddy has plants arranged on the intersection points of a grid as shown in Figure-1, and the troublesome frogs hop completely through your paddy, starting outside the paddy on one side and ending outside the paddy on the other side as shown in Figure-2:
    Many frogs can jump through the paddy, hopping from rice plant to rice plant. Every hop lands on a plant and flattens it, as in Figure-3. Note that some plants may be landed on by more than one frog during the night. Of course, you can not see the lines showing the paths of the frogs or any of their hops outside of your paddy ?for the situation in Figure-3, what you can see is shown in Figure-4:
    From Figure-4, you can reconstruct all the possible paths which the frogs may have followed across your paddy. You are only interested in frogs which have landed on at least 3 of your rice plants in their voyage through the paddy. Such a path is said to be a frog path. In this case, that means that the three paths shown in Figure-3 are frog paths (there are also other possible frog paths). The vertical path down column 1 might have been a frog path with hop length 4 except there are only 2 plants flattened so we are not interested; and the diagonal path including the plants on row 2 col. 3, row 3 col. 4, and row 6 col. 7 has three flat plants but there is no regular hop length which could have spaced the hops in this way while still landing on at least 3 plants, and hence it is not a frog path. Note also that along the line a frog path follows there may be additional flattened plants which do not need to be landed on by that path (see the plant at (2, 6) on the horizontal path across row 2 in Figure-4), and in fact some flattened plants may not be explained by any frog path at all.

    Your task is to write a program to determine the maximum number of landings in any single frog path (where the maximum is taken over all possible frog paths). In Figure-4 the answer is 7, obtained from the frog path across row 6.

    输入

    Your program is to read from standard input. The first line contains two integers R and C, respectively the number of rows and columns in your rice paddy, 1 <= R,C <= 5000. The second line contains the single integer N, the number of flattened rice plants, 3 <= N <= 5000. Each of the remaining N lines contains two integers, the row number (1 <= row number <= R) and the column number (1 <= column number <= C) of a flattened rice plant, separated by one blank. Each flattened plant is only listed once.

    输出

    Your program is to write to standard output. The output contains one line with a single integer, the number of plants flattened along a frog path which did the most damage if there exists at least one frog path, otherwise, 0.

    样例输入

    6 7
    14
    2 1
    6 6
    4 2
    2 5
    2 6
    2 7
    3 4
    6 1
    6 2
    2 3
    6 3
    6 4
    6 5
    6 7
    

    样例输出

    7

    来源

    IOI 2002

     1 #include <cstdio>
     2 #include <string>
     3 #include <memory.h>
     4 #include <algorithm>
     5 #include <stdlib.h>
     6 #include <math.h>
     7 #include <iostream>
     8 #include<queue>
     9 #include <vector>
    10 using namespace std;
    11 
    12 int r, c,num,ans=2;
    13 int map[5005][5005];
    14 
    15 struct node {
    16     int x, y;
    17 };
    18 
    19 bool operator<(node a, node b) {
    20     if (a.x == b.x)
    21         return a.y < b.y;
    22     return a.x < b.x;
    23 }
    24 
    25 node frog[5005];
    26 
    27 int main()
    28 {
    29     scanf("%d%d%d", &r, &c,&num);
    30     for (int i = 1; i <= num; i++) {
    31         int x, y;
    32         scanf("%d%d", &x, &y);
    33         frog[i].x = x, frog[i].y = y;
    34         map[x][y] = 1;
    35     }
    36     sort(frog, frog + num);
    37     for(int i=1;i<=num-2;i++)
    38         for(int j=i+1;j<=num-1;j++)
    39             {
    40                 int deltax = frog[j].x - frog[i].x, deltay = frog[j].y - frog[i].y;
    41                 if ((frog[i].x - deltax >= 1) && (frog[i].y - deltay <= c && frog[i].y - deltay >= 1))
    42                     continue;
    43                 if (deltax*(ans-1)+frog[i].x>r)continue;
    44                 int tmp = deltay * (ans - 1) + frog[i].y;
    45                 if (tmp>c||tmp<1)continue;
    46                 int xnow = frog[j].x, ynow = frog[j].y,len=1;
    47                 bool flag = true;
    48                 while (xnow <= r && xnow >= 1 && ynow <= c && ynow >= 1) {
    49                     if (map[xnow][ynow] == 0) {
    50                         flag = false;
    51                         break;
    52                     }
    53                     xnow += deltax, ynow += deltay;
    54                     len++;
    55                 }
    56                 if(flag)
    57                     ans = max(ans, len);
    58             }
    59     if (ans <= 2)
    60         ans = 0;
    61     printf("%d
    ", ans);
    62     return 0;
    63 }
    View Code

    WA点:

    ans一开始要初始化成2,不然运算中 tmp 值会出现错误

    剪枝:

    1.当前情况继续走下去肯定没有ans大了

    2.当前情况下作判断,发现第一个点并非此路径的第一个点(此路径再上一个点在地图内)

    思路:枚举路径第一、二个点

    注定失败的战争,也要拼尽全力去打赢它; 就算输,也要输得足够漂亮。
  • 相关阅读:
    hdu2049
    hdu2047
    hdu2568
    hdu2570
    推流摄像头推RTMP视频流至EasyDSS视频直播点播平台Chrome浏览器无法播放如何解决?
    RTMP推流协议视频智能分析/人脸识别/直播点播平台EasyDSS接口调用注意事项介绍
    【解决方案】人脸识别/智能分析视频安防服务平台EasyCVR如何打造智慧人社局培训办事机构远程监控系统?
    【解决方案】RTMP推流协议视频智能分析/直播点播/人脸识别平台EasyDSS打造智能多媒体展厅解决方案
    【解决方案】视频智能分析/人脸识别平台EasyDSS实现景区智慧旅游体系,VR+大数据打造风景区实时视频
    RTMP推流协议视频智能分析平台EasyDSS直播点播系统新版本无法完成推流以及录像回看排查过程
  • 原文地址:https://www.cnblogs.com/yalphait/p/9232393.html
Copyright © 2011-2022 走看看