CSES - Leirikisa 9.12.2021 - Results
Submission details
Task:Ruudukko
Sender:tonero
Submission time:2021-12-09 15:37:08 +0200
Language:C++ (C++17)
Status:READY
Result:17
Feedback
groupverdictscore
#1ACCEPTED17
#20
#30
Test results
testverdicttimegroup
#1ACCEPTED0.01 s1details
#2ACCEPTED0.01 s1details
#3ACCEPTED0.01 s1details
#4ACCEPTED0.01 s1details
#5ACCEPTED0.01 s1details
#60.08 s2details
#70.09 s2details
#80.09 s2details
#90.09 s2details
#100.09 s2details
#110.09 s3details
#120.08 s3details
#130.09 s3details
#140.09 s3details
#150.09 s3details

Code

#include <bits/stdc++.h>

using namespace std;

char arr[500][500] = {{__INT8_MAX__}};

string w[500][500] = {""};

int n;

bool valid(int i){
    return i >= 0 && i < n;
}

bool sF = false;
string shortest = "";
vector<char> shortestVec;

void step(int x, int y, string len, vector<char> jono){
    len += arr[x][y];
    if(!w[x][y].empty() && len.length() >= w[x][y].length() && (len.length() > w[x][y].length() || len >= w[x][y])) return;
    w[x][y] = len;

    jono.emplace_back(arr[x][y]);
    if(x == n-1 && y == n-1){
        shortest = len;
        shortestVec = jono;
        return;
    }

    if(valid(x-1)) step(x-1, y, len, jono);
    if(valid(x+1)) step(x+1, y, len, jono);

    if(valid(y+1)) step(x, y+1, len, jono);
    if(valid(y-1)) step(x, y-1, len, jono);
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    cin >> n;
    char b;
    for (int y = 0; y < n; ++y){
        for (int x = 0; x < n; ++x){
            cin >> b;
            arr[x][y] = b;
        }
    }

    list<tuple<short,short,string, vector<char>>> nxt = {};

    nxt.push_back({0,0,"",{}});

    int x;
    int y;
    string len;
    vector<char> jono;

    while(!nxt.empty()){
        auto elem = nxt.back();
        nxt.pop_back();
        x = get<0>(elem);
        y = get<1>(elem);
        len = get<2>(elem);
        jono = get<3>(elem);

        len += arr[x][y];
        if(!w[x][y].empty() && len.length() >= w[x][y].length() && (len.length() > w[x][y].length() || len >= w[x][y])) continue;
        w[x][y] = len;

        jono.emplace_back(arr[x][y]);
        if(x == n-1 && y == n-1){
            shortest = len;
            shortestVec = jono;
            continue;
        }

        if(valid(x-1)) nxt.emplace_back(x-1, y, len, jono);
        if(valid(x+1)) nxt.emplace_back(x+1, y, len, jono);

        if(valid(y+1)) nxt.emplace_back(x, y+1, len, jono);
        if(valid(y-1)) nxt.emplace_back(x, y-1, len, jono);
    }

    step(0,0, "", {});
    for (const auto &item : shortestVec){
        cout << item;
    }

    flush(std::cout);
}

Test details

Test 1

Group: 1

Verdict: ACCEPTED

input
5
AAAAA
AAAAA
AAAAA
AAAAA
...

correct output
AAAAAAAAB

user output
AAAAAAAAB

Test 2

Group: 1

Verdict: ACCEPTED

input
5
ABABA
BABAB
ABABA
BABAB
...

correct output
ABABABABA

user output
ABABABABA

Test 3

Group: 1

Verdict: ACCEPTED

input
5
WRYIU
TWLKH
UJMJC
GRDJW
...

correct output
WRWJMDJWK

user output
WRWJMDJWK

Test 4

Group: 1

Verdict: ACCEPTED

input
5
RUEAE
ZYHHW
KDBPD
DXREW
...

correct output
RUEAEWDWX

user output
RUEAEWDWX

Test 5

Group: 1

Verdict: ACCEPTED

input
5
SRGYR
MYDOB
GNOVM
SZOZK
...

correct output
SMGNOOLTU

user output
SMGNOOLTU

Test 6

Group: 2

Verdict:

input
100
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA...

correct output
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA...

user output
(empty)

Test 7

Group: 2

Verdict:

input
100
ABABABABABABABABABABABABABABAB...

correct output
ABABABABABABABABABABABABABABAB...

user output
(empty)

Test 8

Group: 2

Verdict:

input
100
FWOVNYKNMMQCNHJGUYPNEDXGVVGONC...

correct output
FWDBDECKBHKIACOVUCJGDJOHAYIBHO...

user output
(empty)

Test 9

Group: 2

Verdict:

input
100
ETGCJABWKMAAEOQXWFFYMDJBMNKMQK...

correct output
EAARGLBRLHCDHHBPABHDAJBEEBHQBE...

user output
(empty)

Test 10

Group: 2

Verdict:

input
100
GNWMLJNHSBAADUFCSGIZMWHZTVDHNR...

correct output
GEGOFRDKBNLLEUOPOEQCEFMTKANLNC...

user output
(empty)

Test 11

Group: 3

Verdict:

input
500
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA...

correct output
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA...

user output
(empty)

Test 12

Group: 3

Verdict:

input
500
ABABABABABABABABABABABABABABAB...

correct output
ABABABABABABABABABABABABABABAB...

user output
(empty)

Test 13

Group: 3

Verdict:

input
500
HGADXTSFXYIEMDWMFIVQGHTACFUPYI...

correct output
HGADEJOGAKPJCRAHTABRSDLAVGBFAG...

user output
(empty)

Test 14

Group: 3

Verdict:

input
500
SBLNMAZESQVGWAPZYHQJMQTNGMEZWS...

correct output
SBLCAMDHILGIDRCIDUNMMAHFYCENOS...

user output
(empty)

Test 15

Group: 3

Verdict:

input
500
AOXYXRYFWPYWQDPWXQITLHQQUAYZAJ...

correct output
AOJLDOAPBGEKSGCNKBUMKAJCCWCOOD...

user output
(empty)