The Troublesome Frog
Description
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.
Input
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.
Output
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.
Sample Input
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
Sample Output
7
我的代码
这一题是直接枚举+剪枝,只不过题设的条件太多了,要一一考虑进去才行。
首先要将所有被压平的植物的坐标进行排序,这样枚举时可以先减一半的复杂度。然后在枚举时,将两个点视作为这条路径的前两个点,由于一条路径必须完整地在稻田中,所以要检查第一个点再往前推进一个是否是在地图外,且依照前两个点向后推进是否每一个在地图内的点都被踩踏过。
剪枝方法是判断这条路径如果能超过目前答案的个数,应当在地图中起码还剩余了这么多个点。
#include<stdio.h>
#include<vector>
#include<string.h>
#include<algorithm>
using namespace std;
struct node{
int x,y;
bool operator<(const node a)const{
if (x==a.x) {
return y<a.y;
}
else {
return x<a.x;
}
}
node(){
}
node(int x,int y):x(x),y(y){
}
};
int r,c,n,ans;
int map[5001][5001];
node plants[5001];
int checkBorder(int x,int y){
if (x>=1&&x<=r&&y>=1&&y<=c){
return 1;
}else{
return 0;
}
}
void searchFrog(){
int stepX,stepY,nowX,nowY,temp;
for (int i=0;i<n;i++){
for (int j=i+1;j<n;j++){
stepX=plants[j].x-plants[i].x;
stepY=plants[j].y-plants[i].y;
if (checkBorder(plants[i].x-stepX,plants[i].y-stepY)==1){
continue;
}
if (checkBorder(plants[i].x+ans*stepX,plants[i].y+ans*stepY)==0){
continue;
}
temp=2;
nowX=plants[j].x;
nowY=plants[j].y;
while (checkBorder(nowX+stepX,nowY+stepY)==1){
if (map[nowX+stepX][nowY+stepY]==1){
nowX+=stepX;
nowY+=stepY;
temp++;
}else{
temp=0;
break;
}
}
if (temp>=3){
if (temp>ans){
ans=temp;
}
}
}
}
}
int main()
{
while (scanf("%d%d",&r,&c)==2){
memset(map,0,sizeof(map));
ans=0;
int x,y;
scanf("%d",&n);
for (int i=0;i<n;i++){
scanf("%d%d",&x,&y);
map[x][y]=1;
plants[i]=node(x,y);
}
sort(plants,plants+n);
searchFrog();
printf("%d\n",ans);
}
return 0;
}
反思
浮躁时写题目太难受了。题目条件根本还没有看全就已经开始写了。