ZeroJudge H658: Fishing

Xiao Ming is a fisherman who goes out to sea in his boat every day to catch fish. Fishing is his passion, and to catch as many fish as possible efficiently, he uses the radar system on his boat to detect nearby schools of fish, thereby determining the distance between each school and his boat, allowing him to start fishing from the nearest location. Xiao Ming hopes to quickly find the nearest school of fish using a program, so he's asking for your help.

Sample Inputs/Outputs

Sample Input(s)Sample Output(s)
The first line of input consists of two integers X and Y (0 ≤ X, Y ≤ 500) representing the fisherman's coordinates. The second line contains an integer N (1 ≤ N ≤ 100) representing the number of fish schools. Following that, there are N lines, each containing two integers Ai and Bi (0 ≤ Ai, Bi ≤ 500, 1 ≤ i ≤ N) indicating the coordinates of the center of each fish school.Output two integers representing the coordinates of the center of the nearest fish school to the fisherman, ensuring that there is only one nearest fish school.
5 6
1
10 10
10 10
8 2
3
7 9
10 3
5 1
10 3
20 15
7
25 34
100 0
12 51
3 9
37 22
10 71
2 36
3 9

Thought Process

Because calculating distances between coordinates may involve rounding errors due to rounding up or down, which could lead to incorrect determination of the minimum value, you can avoid calculating the square root when computing distances. Use a for loop to calculate the distance between each coordinate and the boat, then select the minimum value.

Sample Code-ZeroJudge H658: Fishing

#include <iostream>
#include <math.h>
using namespace std;

int main() {
    cin.sync_with_stdio(0);
    cin.tie(0);
    int x, y;
    cin >> x >> y;
    int N;
    cin >> N;
    int ans = 9999999;
    pair<int, int>loc;
    for (int i = 0; i<N; i++)
    {
        int a, b;
        cin >> a >> b;
        int dis = pow(a-x, 2) + pow(b-y, 2);
        if (dis < ans)
        {
            ans = dis;
            loc.first = a;
            loc.second = b;
        }
    }
    cout << loc.first << " " << loc.second << "\n";
}

//ZeroJudge H658
//Dr. SeanXD

Comments