2014 Jan Silver Problem 2 Cross Country Skiing: Difference between revisions

From Wiki
Jump to navigation Jump to search
(Created page with "== Problem == == Solution == == Code == Category:Yearly_2013_2014 Category:Silver")
 
No edit summary
Line 1: Line 1:
== Problem ==
== Problem ==
The problem describes a cross-country skiing course represented by an M x N grid of elevations, some of which are designated as waypoints. The difficulty rating of the course is the minimum value of D such that all waypoints are mutually reachable by repeatedly skiing from one cell to an adjacent cell with an absolute elevation difference of at most D. The difficulty rating is calculated based on the elevations and the waypoint designations provided in the input. The task is to calculate and output the difficulty rating of the course.


== Solution ==
== Solution ==
To approach this question, you can use a binary search algorithm to find the minimum difficulty rating D that allows a cow to reach any waypoint from any other waypoint by repeatedly skiing from a cell to an adjacent cell with an absolute elevation difference at most D.
First, you need to read the input values and store the elevations and the waypoint designations in an appropriate data structure. You can use a two-dimensional vector to store the elevations and another two-dimensional vector to store the waypoint designations.
Next, you can perform a binary search over the range of possible values for D. You can start with a range of [0, max_elevation], where max_elevation is the maximum elevation in the grid. At each step of the binary search, you check if all the waypoints are mutually reachable using the current value of D. You can do this by performing a depth-first search (DFS) or breadth-first search (BFS) on the waypoint cells and checking if all the waypoints are visited. If all the waypoints are visited, you can update the upper bound of the range of possible values for D. Otherwise, you update the lower bound.
Repeat the binary search until the upper and lower bounds converge to a single value. This value is the minimum difficulty rating D that allows a cow to reach any waypoint from any other waypoint by repeatedly skiing from a cell to an adjacent cell with an absolute elevation difference at most D. Finally, output the value of D.
Note that in the DFS or BFS, you need to check if a cell is adjacent to another cell with an elevation difference at most D. You can use a two-dimensional array to store the visited status of each cell during the search.
Overall, the time complexity of this approach is O(MN log W), where W is the range of possible values for D (i.e., max_elevation - 0).


== Code ==
== Code ==
<pre>
#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> elevation;
vector<vector<int>> destination;
int totalPoints, current;
int startX = -1, startY = -1;
int numRows, numCols;
int maxHeight = 0, minHeight = 1e9;
void bfs(int x, int y, int& maxDiff, vector<vector<bool>>& visited) {
    visited[x][y] = true;
    current += destination[x][y];
    int dx[] = {-1, 0, 1, 0};
    int dy[] = {0, 1, 0, -1};
    for (int i = 0; i < 4; i++) {
        int newX = x + dx[i];
        int newY = y + dy[i];
        if (newX >= 0 && newX < numRows && newY >= 0 && newY < numCols &&
            !visited[newX][newY] && abs(elevation[newX][newY] - elevation[x][y]) <= maxDiff) {
            bfs(newX, newY, maxDiff, visited);
        }
    }
}
bool isReachable(int maxDiff) {
    current = 0;
    vector<vector<bool>> visited(numRows, vector<bool>(numCols, false));
    bfs(startX, startY, maxDiff, visited);
    return current >= totalPoints;
}
int main() {
    freopen("ccski.in", "r", stdin);
    freopen("ccski.out", "w", stdout);
    cin >> numRows >> numCols;
    totalPoints = 0;
    elevation.resize(numRows, vector<int>(numCols, 0));
    destination.resize(numRows, vector<int>(numCols, 0));
    for (int i = 0; i < numRows; i++) {
        for (int j = 0; j < numCols; j++) {
            cin >> elevation[i][j];
            maxHeight = max(maxHeight, elevation[i][j]);
            minHeight = min(minHeight, elevation[i][j]);
        }
    }
    for (int i = 0; i < numRows; i++) {
        for (int j = 0; j < numCols; j++) {
            cin >> destination[i][j];
            totalPoints += destination[i][j];
            if (startX < 0 && destination[i][j] == 1) {
                startX = i;
                startY = j;
            }
        }
    }
    int high = maxHeight - minHeight + 1;
    int low = 0;
    while (low < high) {
        int mid = (low + high) / 2;
        if (isReachable(mid)) {
            high = mid;
        } else {
            low = mid + 1;
        }
    }
    cout << high << endl;
}
</pre>


[[Category:Yearly_2013_2014]]
[[Category:Yearly_2013_2014]]
[[Category:Silver]]
[[Category:Silver]]

Revision as of 05:45, 5 May 2023

Problem

The problem describes a cross-country skiing course represented by an M x N grid of elevations, some of which are designated as waypoints. The difficulty rating of the course is the minimum value of D such that all waypoints are mutually reachable by repeatedly skiing from one cell to an adjacent cell with an absolute elevation difference of at most D. The difficulty rating is calculated based on the elevations and the waypoint designations provided in the input. The task is to calculate and output the difficulty rating of the course.

Solution

To approach this question, you can use a binary search algorithm to find the minimum difficulty rating D that allows a cow to reach any waypoint from any other waypoint by repeatedly skiing from a cell to an adjacent cell with an absolute elevation difference at most D.

First, you need to read the input values and store the elevations and the waypoint designations in an appropriate data structure. You can use a two-dimensional vector to store the elevations and another two-dimensional vector to store the waypoint designations.

Next, you can perform a binary search over the range of possible values for D. You can start with a range of [0, max_elevation], where max_elevation is the maximum elevation in the grid. At each step of the binary search, you check if all the waypoints are mutually reachable using the current value of D. You can do this by performing a depth-first search (DFS) or breadth-first search (BFS) on the waypoint cells and checking if all the waypoints are visited. If all the waypoints are visited, you can update the upper bound of the range of possible values for D. Otherwise, you update the lower bound.

Repeat the binary search until the upper and lower bounds converge to a single value. This value is the minimum difficulty rating D that allows a cow to reach any waypoint from any other waypoint by repeatedly skiing from a cell to an adjacent cell with an absolute elevation difference at most D. Finally, output the value of D.

Note that in the DFS or BFS, you need to check if a cell is adjacent to another cell with an elevation difference at most D. You can use a two-dimensional array to store the visited status of each cell during the search.

Overall, the time complexity of this approach is O(MN log W), where W is the range of possible values for D (i.e., max_elevation - 0).

Code

#include <bits/stdc++.h>
using namespace std;

vector<vector<int>> elevation;
vector<vector<int>> destination;
int totalPoints, current;
int startX = -1, startY = -1;
int numRows, numCols;
int maxHeight = 0, minHeight = 1e9;

void bfs(int x, int y, int& maxDiff, vector<vector<bool>>& visited) {
    visited[x][y] = true;
    current += destination[x][y];

    int dx[] = {-1, 0, 1, 0};
    int dy[] = {0, 1, 0, -1};

    for (int i = 0; i < 4; i++) {
        int newX = x + dx[i];
        int newY = y + dy[i];

        if (newX >= 0 && newX < numRows && newY >= 0 && newY < numCols &&
            !visited[newX][newY] && abs(elevation[newX][newY] - elevation[x][y]) <= maxDiff) {
            bfs(newX, newY, maxDiff, visited);
        }
    }
}

bool isReachable(int maxDiff) {
    current = 0;
    vector<vector<bool>> visited(numRows, vector<bool>(numCols, false));
    bfs(startX, startY, maxDiff, visited);
    return current >= totalPoints;
}

int main() {
    freopen("ccski.in", "r", stdin);
    freopen("ccski.out", "w", stdout);

    cin >> numRows >> numCols;
    totalPoints = 0;

    elevation.resize(numRows, vector<int>(numCols, 0));
    destination.resize(numRows, vector<int>(numCols, 0));

    for (int i = 0; i < numRows; i++) {
        for (int j = 0; j < numCols; j++) {
            cin >> elevation[i][j];
            maxHeight = max(maxHeight, elevation[i][j]);
            minHeight = min(minHeight, elevation[i][j]);
        }
    }

    for (int i = 0; i < numRows; i++) {
        for (int j = 0; j < numCols; j++) {
            cin >> destination[i][j];
            totalPoints += destination[i][j];
            if (startX < 0 && destination[i][j] == 1) {
                startX = i;
                startY = j;
            }
        }
    }

    int high = maxHeight - minHeight + 1;
    int low = 0;

    while (low < high) {
        int mid = (low + high) / 2;
        if (isReachable(mid)) {
            high = mid;
        } else {
            low = mid + 1;
        }
    }

    cout << high << endl;
}