CSES - Aalto Competitive Programming 2024 - wk2 - Mon - Results
Submission details
Task:Abandoned warehouse
Sender:bubu2006
Submission time:2024-09-09 16:33:23 +0300
Language:C++ (C++20)
Status:READY
Result:ACCEPTED
Test results
testverdicttime
#1ACCEPTED0.00 sdetails
#2ACCEPTED0.00 sdetails
#3ACCEPTED0.00 sdetails
#4ACCEPTED0.00 sdetails
#5ACCEPTED0.00 sdetails
#6ACCEPTED0.01 sdetails
#7ACCEPTED0.03 sdetails
#8ACCEPTED0.03 sdetails
#9ACCEPTED0.05 sdetails
#10ACCEPTED0.05 sdetails
#11ACCEPTED0.00 sdetails
#12ACCEPTED0.00 sdetails
#13ACCEPTED0.05 sdetails
#14ACCEPTED0.00 sdetails
#15ACCEPTED0.00 sdetails
#16ACCEPTED0.06 sdetails

Code

// Problem: Labyrinth
// Contest: CSES - CSES Problem Set
// URL: https://cses.fi/problemset/task/1193
// Memory Limit: 512 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)
 
/*
 * Author: bubu2006
**/
 
#include <bits/stdc++.h>
using namespace std;
 
int main() {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    
    int n, m;
    cin >> n >> m;
    
    vector<string> a(n);
    for(int i = 0; i < n; i++)
        cin >> a[i];
    
    pair<int, int> st, en;
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < m; j++) {
            if(a[i][j] == 'A') st = {i, j};
            else if(a[i][j] == 'B') en = {i, j};
        }
    }
    
    vector<vector<int>> dis(n, vector<int>(m, 1e7));
    vector<vector<pair<int, int>>> par(n, vector<pair<int, int>>(m));
    vector<int> dy = {0, 0, -1, 1};
    vector<int> dx = {1, -1, 0, 0};
    
    queue<pair<int, int>> q;
    q.push({st.first, st.second});
    dis[st.first][st.second] = 0;
    
    while(!q.empty()) {
        pair<int, int> cur = q.front();
        int y = cur.first;
        int x = cur.second;
        
        // cerr << y << ' ' << x << '\n';
        q.pop();
        
        if(cur == en) {
            break;
        }
        
        for(int k = 0; k < 4; k++) {
            int ny = y + dy[k];
            int nx = x + dx[k];
            
            if(ny < 0 || ny >= n || nx < 0 || nx >= m) continue;
            if(a[ny][nx] == '#') continue;
            if(dis[ny][nx] != 1e7) continue;
            
            q.push({ny, nx});
            par[ny][nx] = {y, x};
            dis[ny][nx] = dis[y][x] + 1;
        }
    }
    
    if(dis[en.first][en.second] == 1e7) {
        cout << "NO\n";
        return 0;
    }
    
    cout << "YES\n";
    cout << dis[en.first][en.second] << '\n';
    // cout << par[en.first][en.second].first << ' ' << par[en.first][en.second].second << '\n';
    
    string ans = "";
    int y = en.first;
    int x = en.second;
    
    while(true) {
        // cerr << y << ' ' << x << '\n';
        if(a[y][x] == 'A') break;
        
        pair<int, int> p = par[y][x];
        int py = p.first;
        int px = p.second;
        
        int difx = x - px;
        int dify = y - py;
        pair<int, int> dir = {dify, difx};
        
        if(dir == make_pair(0, 1)) {
            ans += 'R';
        } else if(dir == make_pair(0, -1)) {
            ans += 'L';
        } else if(dir == make_pair(1, 0)) {
            ans += 'D';
        } else {
            ans += 'U';
        }
        
        y = py;
        x = px;
    }
    
    reverse(ans.begin(), ans.end());
    cout << ans << '\n';
}

Test details

Test 1

Verdict: ACCEPTED

input
10 10
##.A######
#.##.##.##
#####..###
.#########
...

correct output
NO

user output
NO

Test 2

Verdict: ACCEPTED

input
10 10
B#..##.#..
#....A##..
#.....#..#
.#......#.
...

correct output
NO

user output
NO

Test 3

Verdict: ACCEPTED

input
10 10
...#..A.#.
....B...##
...#......
..........
...

correct output
YES
3
LLD

user output
YES
3
LLD

Test 4

Verdict: ACCEPTED

input
10 10
.#........
..........
..........
........#.
...

correct output
YES
1
R

user output
YES
1
R

Test 5

Verdict: ACCEPTED

input
10 10
..........
..........
..........
..........
...

correct output
YES
3
RDD

user output
YES
3
RDD

Test 6

Verdict: ACCEPTED

input
1000 1000
##.###..######.#########.###.#...

correct output
NO

user output
NO

Test 7

Verdict: ACCEPTED

input
1000 1000
####.#.###....#.......##.##.#....

correct output
YES
626
LLLDDRDDDDLDLDDLLLLLDDDDLLDLDL...

user output
YES
626
RDDLDLLDDDLDLDDLLLLLDDDDLLDLDL...
Truncated

Test 8

Verdict: ACCEPTED

input
1000 1000
....#.##......#....#......#......

correct output
YES
364
LULULLULLLULLLLLUULLLLUUULLLLL...

user output
YES
364
LULULLULLLULLLLLUULLLLUUULLLLL...
Truncated

Test 9

Verdict: ACCEPTED

input
1000 1000
.................#......#........

correct output
YES
1003
LLLLLLLLLLLLLLLLLLLLLLLLLDLLLL...

user output
YES
1003
LLLLLLLLLLLLLLLLLLLLLLLLLDLLLL...
Truncated

Test 10

Verdict: ACCEPTED

input
1000 1000
.................................

correct output
YES
947
LLLLLLLLLLLLLLLLLLLLLLLLLLLLLL...

user output
YES
947
LLLLLLLLLLLLLLLLLLLLLLLLLLLLLL...
Truncated

Test 11

Verdict: ACCEPTED

input
1000 3
A#B
.#.
.#.
.#.
...

correct output
YES
2000
DDDDDDDDDDDDDDDDDDDDDDDDDDDDDD...

user output
YES
2000
DDDDDDDDDDDDDDDDDDDDDDDDDDDDDD...
Truncated

Test 12

Verdict: ACCEPTED

input
3 1000
A................................

correct output
YES
2000
RRRRRRRRRRRRRRRRRRRRRRRRRRRRRR...

user output
YES
2000
RRRRRRRRRRRRRRRRRRRRRRRRRRRRRR...
Truncated

Test 13

Verdict: ACCEPTED

input
999 999
A#...#...#...#...#...#...#...#...

correct output
YES
499998
DDDDDDDDDDDDDDDDDDDDDDDDDDDDDD...

user output
YES
499998
DDDDDDDDDDDDDDDDDDDDDDDDDDDDDD...
Truncated

Test 14

Verdict: ACCEPTED

input
1 3
A.B

correct output
YES
2
RR

user output
YES
2
RR

Test 15

Verdict: ACCEPTED

input
2 2
##
AB

correct output
YES
1
R

user output
YES
1
R

Test 16

Verdict: ACCEPTED

input
1000 1000
A................................

correct output
YES
1998
RRRRRRRRRRRRRRRRRRRRRRRRRRRRRR...

user output
YES
1998
RRRRRRRRRRRRRRRRRRRRRRRRRRRRRR...
Truncated