Editing
2016 Dec Silver Problem 3 Moocast
(section)
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== C++ Implementation == First, let's create a function to check if cow A can transmit a message to cow B: <pre> bool canTransmit(const vector<int>& cowA, const vector<int>& cowB) { int dx = cowA[0] - cowB[0]; int dy = cowA[1] - cowB[1]; int distanceSquared = dx * dx + dy * dy; return distanceSquared <= cowA[2] * cowA[2]; } </pre> Next, implement the BFS traversal to find the number of reachable cows: <pre> int bfs(const vector<vector<int>>& cows, int start) { int count = 0; vector<bool> visited(cows.size(), false); queue<int> q; q.push(start); visited[start] = true; while (!q.empty()) { int current = q.front(); q.pop(); count++; for (int i = 0; i < cows.size(); ++i) { if (!visited[i] && canTransmit(cows[current], cows[i])) { visited[i] = true; q.push(i); } } } return count; } </pre> Finally, iterate over all cows and find the maximum number of reachable cows: <pre> int main() { int n; cin >> n; vector<vector<int>> cows(n, vector<int>(3)); for (int i = 0; i < n; ++i) { cin >> cows[i][0] >> cows[i][1] >> cows[i][2]; } int maxCows = 0; for (int i = 0; i < n; ++i) { maxCows = max(maxCows, bfs(cows, i)); } cout << maxCows << endl; } </pre>
Summary:
Please note that all contributions to Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
My wiki:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Create account
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
View history
More
Search
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Tools
What links here
Related changes
Special pages
Page information