演算法題 討厭的青蛙

2021-09-11 18:30:59 字數 1110 閱讀 8932

//青蛙問題,求解一條路徑

//這條路徑中被乙隻青蛙壓倒的稻子最多

//青蛙只會在乙個方向上前進,並且步長是一定的

#include #include #include using namespace std;

int r,c,n;

struct plant; //記錄被踩壞的水稻座標

//為了debug方便自己先初始化

plant plants[5001]=,,,,,

,,,,,,,,}; //最多記錄5000個座標

plant plant;

//過載小於號

bool operator < (const plant &p1, const plant &p2)

//搜尋路徑,返回被踩壞的水稻數

int searchpath(plant secplant, int dx, int dy)

plant.x += dx;

plant.y += dy;

steps++;

}return (steps);

}void input()

int main()

//接著我們要判斷是否在目前得到的最大的步數下會越界

//如果會越界說明這條路徑沒前途

//而且我們的步長dx在第i個點會越來越大,所以直接換下乙個i來判斷

if(plants[i].x + (max - 1)*dx > r)

py = plants[i].y + (max - 1)*dy;

if(py > c || py < 1)

/*printf("the first point (%d, %d)\n", plants[i].x, plants[i].y);

printf("the second point (%d, %d)\n", plants[j].x, plants[j].y);*/

steps = searchpath(plants[j], dx, dy); //計算走的步數

if(steps > max) max = steps;}}

if(max == 2) max = 0;

printf("%d\n", max);

}

討厭的青蛙

題目很長,直接放鏈結討厭的青蛙 要求出最長的路徑,就要比較所有的路徑長度。對於每一條路徑,因為步長相等,所以只要確定開始兩個被踩的點就可以求出整條路徑了。假設前兩個點為 x1,y1 x2,y2 則步長dx x2 x1,dy y2 y1,需要判斷下面三個條件是否都滿足。之後的每個點 xi,yi x i...

討厭的青蛙

問題描述 在南韓,有一種小的青蛙。每到晚上,這種青蛙會跳越稻田,從而踩踏稻子。農民在早 上看到被踩踏的稻子,希望找到造成最大損害的那只青蛙經過的路徑。每只青蛙總是沿著一 條直線跳越稻田,而且每次跳躍的距離都相同,如圖8 4 所示。稻田裡的稻子組成乙個柵 格,每棵稻子位於乙個格點上,如圖8 5 所示。...

列舉 討厭的青蛙(演算法基礎 第2周)

問題講解 分析 講解的很好了,再說就是畫蛇添足。原始碼 include include include using namespace std int r,c,n struct plant plant plants 5001 plant plant int searchpath plant secp...