CSES - Aalto Competitive Programming 2024 - wk2 - Mon - Results
Submission details
Task:Abandoned warehouse
Sender:auni
Submission time:2024-09-09 17:49:04 +0300
Language:C++ (C++17)
Status:READY
Result:
Test results
testverdicttime
#1ACCEPTED0.00 sdetails
#2ACCEPTED0.00 sdetails
#3ACCEPTED0.00 sdetails
#4ACCEPTED0.00 sdetails
#5ACCEPTED0.00 sdetails
#6ACCEPTED0.07 sdetails
#70.22 sdetails
#80.24 sdetails
#90.27 sdetails
#100.28 sdetails
#110.00 sdetails
#120.00 sdetails
#130.16 sdetails
#14ACCEPTED0.00 sdetails
#15ACCEPTED0.00 sdetails
#160.27 sdetails

Code

#include "bits/stdc++.h"

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

#define F first
#define S second
#define PB push_back
#define MP make_pair

#define REP(i,a,b) for (int i = a; i < b; i++)


int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    int n, m;
    cin >> n >> m;
    // vector<int> nodes(n);
    vector<int> map(n*m);
    vector<vector<int>> edges(n*m);
    int start= 0;
    int goal = 0;

    REP(i, 0, n) {
        REP(j, 0, m) {
            char a;
            cin >> a;

            if(a == 'A') {
                start = i*m+j;
                map[i*m + j] = '.';
                if(i > 0 && map[(i-1)*m + j] == '.') {
                    edges[(i-1)*m + j].PB((i)*m + j);
                    edges[(i)*m + j].PB((i-1)*m + j);
                } 
                if(j > 0 && map[(i)*m + j - 1] == '.') {
                    edges[(i)*m + j-1].PB((i)*m + j);
                    edges[(i)*m + j].PB((i)*m + j-1);
                }
            } else if(a == 'B') {
                goal = i*m+j;
                map[i*m + j] = '.';
                if(i > 0 && map[(i-1)*m + j] == '.') {
                    edges[(i-1)*m + j].PB((i)*m + j);
                    edges[(i)*m + j].PB((i-1)*m + j);
                } 
                if(j > 0 && map[(i)*m + j - 1] == '.') {
                    edges[(i)*m + j-1].PB((i)*m + j);
                    edges[(i)*m + j].PB((i)*m + j-1);
                }
            } else if(a == '.') {
                map[i*m + j] = '.';
                if(i > 0 && map[(i-1)*m + j] == '.') {
                    edges[(i-1)*m + j].PB((i)*m + j);
                    edges[(i)*m + j].PB((i-1)*m + j);
                } 
                if(j > 0 && map[(i)*m + j - 1] == '.') {
                    edges[(i)*m + j-1].PB((i)*m + j);
                    edges[(i)*m + j].PB((i)*m + j-1);
                }
            } else {
                map[i*m + j] = '#';
            }
        }
    }

    vector<bool> visited(n*m);
    stack<char> path;
    queue<int> q;
    vector<int> distance(n*m,-1);
    q.push(start);
    visited[start] = true;
    distance[start] = 0;
    
    vector<int> parents(n*m, -1);

    while(!q.empty()) {
        int l = q.front(); q.pop();

        for(auto e: edges[l]) {
            if(visited[e]) continue;
            parents[e] = l;
            distance[e] = distance[l] + 1;
            q.push(e);
           
            visited[e] = true;
        }
    }
    int node = goal;
    path.push(node);

    if(distance[goal] == -1) {
        std::cout << "NO"<< "\n";
    } else {
        std::cout << "YES" << "\n" << distance[goal] << "\n";
        while(parents[node] != -1) {
            path.push(parents[node]);
            node = parents[node];
        }
        int a, b = path.top(); path.pop();

        while(!path.empty()) {
            a = b;
            b = path.top(); path.pop();
            int d = a-b;
            //std::cout << d << " HERE" << "\n";
            if(d == -1) {
                std::cout << "R";
            } else if(d == 1) {
                std::cout << "L";
            } else if(d > 0) {
                std::cout << "U";
            } else {
                std::cout << "D";
            }
        }
        std::cout << "\n";
    }

    return 0;
}


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:

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

correct output
YES
626
LLLDDRDDDDLDLDDLLLLLDDDDLLDLDL...

user output
YES
626
LLLUURUUDULULUULLLLLUUUULLULDL...
Truncated

Test 8

Verdict:

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

correct output
YES
364
LULULLULLLULLLLLUULLLLUUULLLLL...

user output
YES
364
UDDDDDLDDDDDUDDDDDLLLDDDDLLDUD...
Truncated

Test 9

Verdict:

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

correct output
YES
1003
LLLLLLLLLLLLLLLLLLLLLLLLLDLLLL...

user output
YES
1003
LLLLLLLLLLLLLLLLLLLLLLLLLULLLL...
Truncated

Test 10

Verdict:

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

correct output
YES
947
LLLLLLLLLLLLLLLLLLLLLLLLLLLLLL...

user output
YES
947
UDDDDDDDDDUDDDDDDDDDDUDDDDDDDD...
Truncated

Test 11

Verdict:

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

correct output
YES
2000
DDDDDDDDDDDDDDDDDDDDDDDDDDDDDD...

user output
YES
2000
DDDDDDDDDDDDDDDDDDDDDDDDDDDDDD...
Truncated

Test 12

Verdict:

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

correct output
YES
2000
RRRRRRRRRRRRRRRRRRRRRRRRRRRRRR...

user output
YES
2000
RRRRRRRRRRRRRRRRRRRRRRRRRRRRRR...
Truncated

Test 13

Verdict:

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

correct output
YES
499998
DDDDDDDDDDDDDDDDDDDDDDDDDDDDDD...

user output
YES
499998
UUUUUDUUUUUUUUUDUUUUUUUUUDUUUU...
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:

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

correct output
YES
1998
RRRRRRRRRRRRRRRRRRRRRRRRRRRRRR...

user output
YES
1998
RRRRRRRRRRRRRRRRRRRRRRRRRRRRRR...
Truncated