这道题用曼哈顿距离来做,曼哈顿距离百度百科的解释如下:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i, j, k;
int target_pos[4][2] = {{0, 0}, {2020, 11}, {11, 14}, {2000, 2000}};
int count = 0;
for (i = -2500; i < 5000; i++)
{
for (j = -2500; j < 5000; j++)
{
for (k = 0; k < 4; k++)
{
if (abs(i - target_pos[k][0]) + abs(j - target_pos[k][1]) <= 2020)//如果与4个坐标中任何一个坐标的曼哈顿距离小于等于2020,则count++
{
count ++;
break;//一定要,否则会出现同一个点多次计算
}
}
}
}
printf("%d\n", count);
return 0;
}