CSES - Aalto Competitive Programming 2024 - wk2 - Mon - Results
Submission details
Task:Abandoned warehouse
Sender:ZDHKLV
Submission time:2024-09-09 16:26:58 +0300
Language:C++11
Status:READY
Result:ACCEPTED
Test results
testverdicttime
#1ACCEPTED0.00 sdetails
#2ACCEPTED0.00 sdetails
#3ACCEPTED0.00 sdetails
#4ACCEPTED0.00 sdetails
#5ACCEPTED0.00 sdetails
#6ACCEPTED0.07 sdetails
#7ACCEPTED0.08 sdetails
#8ACCEPTED0.08 sdetails
#9ACCEPTED0.10 sdetails
#10ACCEPTED0.10 sdetails
#11ACCEPTED0.00 sdetails
#12ACCEPTED0.00 sdetails
#13ACCEPTED0.10 sdetails
#14ACCEPTED0.00 sdetails
#15ACCEPTED0.00 sdetails
#16ACCEPTED0.10 sdetails

Code

#include <bits/stdc++.h>

using namespace std;

int n, m;
char **labyrinth;
bool **seen;
pair<int, int> **parent;

string bfs(int x, int y) {

    queue<pair<int, int>> q;

    q.push({x, y});

    while (!q.empty()) {

        auto front = q.front();
        q.pop();

        int fx = front.first;
        int fy = front.second;

        if (seen[fx][fy])
            continue;
        
        seen[fx][fy] = true;

        if (labyrinth[fx][fy] == 'B') {
            string path = "";
            
            pair<int, int> current = {fx, fy};
            while (labyrinth[current.first][current.second] != 'A') {
                pair<int, int> next = parent[current.first][current.second];
                if (next.first - current.first == 1) path += "U";
                else if (next.first - current.first == -1) path += "D";
                else if (next.second - current.second == 1) path += "L";
                else if (next.second - current.second == -1) path += "R";
                current = next;
            }

            return path;
        }

        if (fx > 0 && labyrinth[fx-1][fy] != '#' && !seen[fx-1][fy]) {
            parent[fx-1][fy] = {fx, fy};
            q.push({fx-1, fy});
        }
    
        if (fy > 0 && labyrinth[fx][fy-1] != '#' && !seen[fx][fy-1]) {
            parent[fx][fy-1] = {fx, fy};
            q.push({fx, fy-1});
        }
    
        if (fx+1 < n && labyrinth[fx+1][fy] != '#' && !seen[fx+1][fy]) {
            parent[fx+1][fy] = {fx, fy};
            q.push({fx+1, fy});
        }

        if (fy+1 < m && labyrinth[fx][fy+1] != '#' && !seen[fx][fy+1]) {
            parent[fx][fy+1] = {fx, fy};
            q.push({fx, fy+1});
        }

    }

    return "";

}

int main() {

    cin >> n >> m;

    labyrinth = new char*[n];
    for (int i = 0; i < n; i++)
        labyrinth[i] = new char[m];

    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
            cin >> labyrinth[i][j];
    
    seen = new bool*[n];
    for (int i = 0; i < n; i++) {
        seen[i] = new bool[m];
        for (int j = 0; j < m; j++)
            seen[i][j] = false;
    }

    parent = new pair<int, int>*[n];
    for (int i = 0; i < n; i++) {
        parent[i] = new pair<int, int>[m];
        for (int j = 0; j < m; j++)
            parent[i][j] = {-1, -1};
    }

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            if (labyrinth[i][j] == 'A') {
                string res = bfs(i, j);
                reverse(res.begin(), res.end());
                if (res.size() > 0) {
                    cout << "YES" << endl;
                    cout << res.size() << endl;
                    cout << res << endl;
                } else {
                    cout << "NO" << endl;
                }

                return 0;

            }
        }
    }

    return 0;

}

/*
5 8
########
#.A#...#
#.##.#B#
#......#
########
*/

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
DLL

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
RDDDDDLDDDDLDDDDLLLLLLDDLLLDDL...

Test 8

Verdict: ACCEPTED

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

correct output
YES
364
LULULLULLLULLLLLUULLLLUUULLLLL...

user output
YES
364
LULULLULLLULLLLLUULLLLUUULLLLL...

Test 9

Verdict: ACCEPTED

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

correct output
YES
1003
LLLLLLLLLLLLLLLLLLLLLLLLLDLLLL...

user output
YES
1003
DDDDDDDDDDDDDDDDDLDDDDDDDDDDDD...

Test 10

Verdict: ACCEPTED

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

correct output
YES
947
LLLLLLLLLLLLLLLLLLLLLLLLLLLLLL...

user output
YES
947
LLLLLLLLLLLLLLLLLLLLLLLLLLLLLL...

Test 11

Verdict: ACCEPTED

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

correct output
YES
2000
DDDDDDDDDDDDDDDDDDDDDDDDDDDDDD...

user output
YES
2000
DDDDDDDDDDDDDDDDDDDDDDDDDDDDDD...

Test 12

Verdict: ACCEPTED

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

correct output
YES
2000
RRRRRRRRRRRRRRRRRRRRRRRRRRRRRR...

user output
YES
2000
RRRRRRRRRRRRRRRRRRRRRRRRRRRRRR...

Test 13

Verdict: ACCEPTED

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

correct output
YES
499998
DDDDDDDDDDDDDDDDDDDDDDDDDDDDDD...

user output
YES
499998
DDDDDDDDDDDDDDDDDDDDDDDDDDDDDD...

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...