#include<iostream>
#include<stdio.h>
using namespace std;
struct Plant{
int x;
int y;
};
int row, line;
Plant plant[5000];
int plant_num;
int cmp(const void* plant_one, const void* plant_two) //将踩踏的稻苗按坐标由小到大排序
{
if (((Plant*)plant_one)->x == ((Plant*)plant_two)->x)
return (((Plant*)plant_one)->y) - (((Plant*)plant_two)->y);
else
return (((Plant*)plant_one)->x) - (((Plant*)plant_two)->x);
}
int bisearch(Plant secPlant, int x, int y) //二分法查找,判断下一个按规定的秧苗是否被踩踏
{
Plant plant_temp;
int step = 2;
plant_temp.x = secPlant.x + x;
plant_temp.y = secPlant.y + y;
while (plant_temp.x <= row && plant_temp.x >= 1 && plant_temp.y <= line && plant_temp.y >= 1)
{
if (!bsearch(&plant_temp, plant, plant_num, sizeof(Plant), cmp))
{
step = 0;
break;
}
step++;
plant_temp.x = plant_temp.x + x;
plant_temp.y = plant_temp.y + y;
}
return step;
}
int main()
{
scanf("%d %d", &row, &line);
scanf("%d", &plant_num);
for (int i = 0; i < plant_num; i++)
scanf("%d %d", &plant[i].x, &plant[i].y);
qsort(plant, plant_num, sizeof(Plant), cmp);
int dis_x, dis_y;
int before_x, before_y;
int step;
int max = 2;
for (int i = 0; i < plant_num - 1; i++)
for (int j = i + 1; j < plant_num; j++)
{
dis_x = plant[j].x - plant[i].x;
dis_y = plant[j].y - plant[i].y;
before_x = plant[i].x - dis_x; //plant[i]的前一个应该被踩踏稻田的横坐标
before_y = plant[i].y - dis_y;//plant[i]的前一个应该被踩踏稻田的纵坐标
if (before_x >= 1 && before_x <= row && before_y >= 1 && before_y <= line) //防止重复计算
continue;
if ((plant[i].x + max * dis_x) > row) //如果两次后行数超过稻田就跳出内层循环(因为行是由小到大排序的)
break;
if ((plant[i].y + max * dis_y) > line || (plant[i].y + max * dis_y) < 1)//如果两次后列数超过稻田就进行下一次循环
continue;
step = bisearch(plant[j], dis_x, dis_y);
if (step > max)
max = step;
}
if (2 == max)
max = 0;
printf("%d\n", max);
return 0;
}
谁能帮我转换这两张图的代码 转成python
是要简单对应还是功能相同?
追问功能相同