Editing
2019 Dec Gold Problem 3 Moortal Cowmbat
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!
== Official Problem Statement == [http://www.usaco.org/index.php?page=viewproblem2&cpid=971 Moortal Cowmbat] Problem Statement: Moortal Cowmbat is a game played on a rectangular grid of size N Γ M. Each cell of the grid is either empty or contains a single cow. The goal of the game is to move the cows from their initial positions to their final positions. Solution: The solution is to use a Breadth-First Search (BFS) algorithm to find the shortest path from the initial position of each cow to its final position. The algorithm works as follows: 1. Start at the initial position of the cow. 2. Check all adjacent cells to see if they are empty. 3. If an empty cell is found, add it to a queue. 4. Repeat steps 2 and 3 until the final position of the cow is reached. The following C++ code implements the BFS algorithm: <pre> #include <queue> #include <vector> struct Point { int x, y; }; // Returns the shortest path from start to end std::vector<Point> bfs(Point start, Point end, int n, int m) { std::queue<Point> q; q.push(start); std::vector<std::vector<bool>> visited(n, std::vector<bool>(m, false)); visited[start.x][start.y] = true; std::vector<Point> path; while (!q.empty()) { Point curr = q.front(); q.pop(); path.push_back(curr); if (curr == end) { break; } // Check all adjacent cells for (int i = -1; i <= 1; i++) { for (int j = -1; j <= 1; j++) { int x = curr.x + i; int y = curr.y + j; if (x >= 0 && x < n && y >= 0 && y < m && !visited[x][y]) { Point next = {x, y}; q.push(next); visited[x][y] = true; } } } } return path; } </pre> [[Category:Yearly_2019_2020]] [[Category:Gold]] [[Category:Dynamic Programming]] [[Category:Floyd-Warshall Algorithm]] [[Category:Graph]]
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