Editing
2020 Jan Platinum Problem 3 Falling Portals
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=998 Falling Portals] Problem Statement: Given a rectangular grid of size N by M, each cell in the grid contains either a portal or a wall. A portal is a cell that can be used to teleport from one cell to another. A wall is an impassable cell. The goal is to find the minimum number of moves required to reach the bottom right corner of the grid from the top left corner. Solution: The solution to this problem is to use a Breadth-First Search (BFS) algorithm. We start by creating a queue of cells to visit and add the top left cell to it. Then, we loop through the queue and for each cell, we check if it is a portal. If it is, we add the cell it teleports to to the queue. If it is not a portal, we check if it is the bottom right cell. If it is, we return the number of moves required to reach it. Otherwise, we add all of its adjacent cells to the queue. We repeat this process until we find the bottom right cell or the queue is empty. Code Example (C++): #include <iostream> #include <queue> #include <vector> using namespace std; const int MAXN = 100; const int MAXM = 100; int N, M; int grid[MAXN][MAXM]; int dist[MAXN][MAXM]; struct Cell { int x, y; }; int bfs() { queue<Cell> q; Cell start = {0, 0}; q.push(start); dist[0][0] = 0; while (!q.empty()) { Cell curr = q.front(); q.pop(); if (curr.x == N - 1 && curr.y == M - 1) { return dist[curr.x][curr.y]; } if (grid[curr.x][curr.y] == 1) { Cell next = {curr.x + 1, curr.y}; if (next.x < N && dist[next.x][next.y] == -1) { dist[next.x][next.y] = dist[curr.x][curr.y] + 1; q.push(next); } next = {curr.x, curr.y + 1}; if (next.y < M && dist[next.x][next.y] == -1) { dist[next.x][next.y] = dist[curr.x][curr.y] + 1; q.push(next); } } else { Cell next = {grid[curr.x][curr.y], curr.y}; if (next.x < N && dist[next.x][next.y] == -1) { dist[next.x][next.y] = dist[curr.x][curr.y] + 1; q.push(next); } next = {curr.x, grid[curr.x][curr.y]}; if (next.y < M && dist[next.x][next.y] == -1) { dist[next.x][next.y] = dist[curr.x][curr.y] + 1; q.push(next); } } } return -1; } int main() { cin >> N >> M; for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) { cin >> grid[i][j]; dist[i][j] = -1; } } cout << bfs() << endl; return 0; } [[Category:Yearly_2019_2020]] [[Category:Platinum]] [[Category:Graph]] [[Category:Dijkstra's Algorithm]] [[Category:Shortest Path]] [[Category:Geometry]]
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