DMOJ · dwite07c4p4

Shortest path around

This C++ solution uses shortest path for DMOJ dwite07c4p4 Shortest path around. Read the reasoning, inspect the code, or try your own test case below.

dwite07c4p4Graphs & treesShortest pathC++59 lines
Solution188of 248
Open official problem ↗ Download C++ file ↓ Search the library → Open full Code Lab ↗ Report an issue ↗

Approach

Shortest path

Shortest path around matches five 10-by-10 maps, X endpoints, walls, diagonal moves, delimiters, and distance output.

Graphs & trees

Problem and code

Useful links.

Written by benbenyaojifen. Try the problem first, then compare your approach with the code.

Open official problem ↗View exact source file ↗
Implementation

shortest_path_around.cpp

C++

     
    #include <bits/stdc++.h>
     
    using namespace std;
     
    const vector<vector<int>> DIR = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}, {-1, -1}, {1, 1}, {1,  -1}, {-1, 1}};
    int main(){
        ios::sync_with_stdio(0);
        cin.tie(0);
        int t = 5;
        while(t-- > 0){
        vector<vector<char>> grid(10, vector<char>(10));
        int r, c;
        for(int i = 0; i < 10; i++){
            for(int j = 0; j < 10; j++){
                    char g;
                    cin >> g;
                    grid[i][j] = g;
                    if(g == 'X'){
                        r = i; c = j;
                    }
                }
            }
            queue<pair<int, int>> q;
            vector<vector<bool>> visited(10, vector<bool>(10));
            vector<vector<int>> dist(10, vector<int>(10));
            for(int i = 0; i < 10; i++){
                for(int j = 0; j < 10; j++){
                    dist[i][j] = 0;
                }
            }
            visited[r][c] = true;
            q.push({r, c});
            bool end = false;
            while(!q.empty()){
                auto[cr, cc] = q.front(); q.pop();
                for(auto d : DIR){
                    int nr, nc;
                    nr = cr + d[0]; 
                    nc = cc + d[1];
                    if(nr >= 0 && nr < 10 && nc >= 0 && nc < 10 && !visited[nr][nc] && grid[nr][nc] != '#'){
                        visited[nr][nc] = true;
                        q.push({nr, nc});
                        dist[nr][nc] = dist[cr][cc] + 1;
                        if(grid[nr][nc] == 'X'){
                            cout << dist[nr][nc] << '\n';
                            end = true;
                        }
                        if(end) break;
                    }
                }
                if(end) break;
            }
            cin.ignore();
            string dash;
            getline(cin, dash);
        }
        return 0;
    }
        

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗Keep studying →

Test this problem

Run your code here.

Paste your code, run a test case, compare the output, or trace selected values.

Full trace, comparison & stress testing ↗
StatusReady
Output
No run yet.
Diagnostics
No diagnostics yet.

Each run is isolated and has strict limits. Passing one test does not guarantee the judge will accept the solution.