CSES - Aalto Competitive Programming 2024 - wk9 - Mon - Results
Submission details
Task:Chess board tour
Sender:Nallue
Submission time:2024-11-04 17:49:35 +0200
Language:C++ (C++11)
Status:READY
Result:
Test results
testverdicttime
#1ACCEPTED0.00 sdetails
#2ACCEPTED0.00 sdetails
#3ACCEPTED0.00 sdetails
#4ACCEPTED0.00 sdetails
#5ACCEPTED0.00 sdetails
#6ACCEPTED0.00 sdetails
#7ACCEPTED0.00 sdetails
#8ACCEPTED0.00 sdetails
#9ACCEPTED0.00 sdetails
#10ACCEPTED0.00 sdetails
#11ACCEPTED0.01 sdetails
#12ACCEPTED0.00 sdetails
#13ACCEPTED0.01 sdetails
#14ACCEPTED0.00 sdetails
#15ACCEPTED0.00 sdetails
#16ACCEPTED0.00 sdetails
#170.12 sdetails
#180.09 sdetails
#19ACCEPTED0.00 sdetails
#20ACCEPTED0.00 sdetails
#210.12 sdetails
#22ACCEPTED0.00 sdetails
#230.12 sdetails
#24ACCEPTED0.00 sdetails
#25ACCEPTED0.00 sdetails
#26ACCEPTED0.00 sdetails
#27--details
#28--details
#29--details
#30--details
#31--details
#32ACCEPTED0.93 sdetails
#330.00 sdetails
#34--details
#350.04 sdetails
#360.01 sdetails
#37--details
#38--details
#39--details
#400.37 sdetails
#410.77 sdetails
#42--details
#430.16 sdetails
#44ACCEPTED0.14 sdetails
#450.17 sdetails
#460.19 sdetails
#47--details
#48--details
#49--details
#50--details
#51--details
#52--details
#53--details
#54--details
#55--details
#56--details

Code

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

bool isWithinBounds(int nx, int ny, int n, int m) {
    return nx >= 0 && ny >= 0 && nx < n && ny < m;
}

bool hamCycleUtil(int x, int y, int n, int m, int pos, int path[], int visited) {

    int dx[] = {1, 0, -1, 0};
    int dy[] = {0, 1, 0, -1};

    if (pos == n * m) {
        for (int k = 0; k < 4; k++) {
            int nx = x + dx[k], ny = y + dy[k];
            if (nx == 0 && ny == 0) return true;
        }
        return false;
    }

    for (int k = 0; k < 4; k++) {
        int nx = x + dx[k];
        int ny = y + dy[k];
        int nextPos = nx * m + ny;

        if (isWithinBounds(nx, ny, n, m) && !(visited & (1 << nextPos))) {
            visited |= (1 << nextPos);
            path[pos] = nextPos;

            if (hamCycleUtil(nx, ny, n, m, pos + 1, path, visited)) return true;

            visited &= ~(1 << nextPos);
        }
    }
    return false;
}

void printSolution(int path[], int V, int n, int m) {
    int from_x = 0, from_y = 0;
    for (int i = 1; i < V; i++) {
        int y = path[i] / m;
        int x = path[i] % m;
        if (y > from_y) cout << "D";
        else if (y < from_y) cout << "U";
        else if (x > from_x) cout << "R";
        else if (x < from_x) cout << "L";
        from_x = x;
        from_y = y;
    }
    if(from_y<0)cout<<"D";
    else if(from_y>0)cout<<"U";
    else if(from_x<0)cout<<"R";
    else if(from_x>0)cout<<"L";
    cout << endl;
}

bool hamCycle(int n, int m) {
    int V = n * m;
    int path[V];
    int visited = 1;

    for (int i = 0; i < V; i++) path[i] = -1;
    path[0] = 0;

    if (!hamCycleUtil(0, 0, n, m, 1, path, visited)) {
        cout << -1 << endl;
        return false;
    }

    printSolution(path, V, n, m);
    return true;
}

int main() {
    int n, m;
    cin >> n >> m;

    hamCycle(n, m);
    return 0;
}

Test details

Test 1

Verdict: ACCEPTED

input
2 2

correct output
DRUL

user output
DRUL

Test 2

Verdict: ACCEPTED

input
2 2

correct output
DRUL

user output
DRUL

Test 3

Verdict: ACCEPTED

input
2 3

correct output
RRDLLU

user output
DRRULL

Test 4

Verdict: ACCEPTED

input
2 2

correct output
DRUL

user output
DRUL

Test 5

Verdict: ACCEPTED

input
4 4

correct output
DDDRUURDDRUUULLL

user output
DDDRRRUUULDDLUUL

Test 6

Verdict: ACCEPTED

input
3 3

correct output
-1

user output
-1

Test 7

Verdict: ACCEPTED

input
4 4

correct output
DDDRUURDDRUUULLL

user output
DDDRRRUUULDDLUUL

Test 8

Verdict: ACCEPTED

input
3 5

correct output
-1

user output
-1

Test 9

Verdict: ACCEPTED

input
3 2

correct output
DDRUUL

user output
DDRUUL

Test 10

Verdict: ACCEPTED

input
4 2

correct output
DDDRUUUL

user output
DDDRUUUL

Test 11

Verdict: ACCEPTED

input
5 5

correct output
-1

user output
-1

Test 12

Verdict: ACCEPTED

input
2 2

correct output
DRUL

user output
DRUL

Test 13

Verdict: ACCEPTED

input
5 5

correct output
-1

user output
-1

Test 14

Verdict: ACCEPTED

input
2 2

correct output
DRUL

user output
DRUL

Test 15

Verdict: ACCEPTED

input
5 2

correct output
DDDDRUUUUL

user output
DDDDRUUUUL

Test 16

Verdict: ACCEPTED

input
2 3

correct output
RRDLLU

user output
DRRULL

Test 17

Verdict:

input
6 7

correct output
RRRRRRDLLLLLDRRRRRDLLLLLDRRRRR...

user output
-1

Test 18

Verdict:

input
5 10

correct output
DDDDRUUURDDDRUUURDDDRUUURDDDRU...

user output
-1

Test 19

Verdict: ACCEPTED

input
5 3

correct output
-1

user output
-1

Test 20

Verdict: ACCEPTED

input
6 2

correct output
DDDDDRUUUUUL

user output
DDDDDRUUUUUL

Test 21

Verdict:

input
10 10

correct output
DDDDDDDDDRUUUUUUUURDDDDDDDDRUU...

user output
-1

Test 22

Verdict: ACCEPTED

input
3 2

correct output
DDRUUL

user output
DDRUUL

Test 23

Verdict:

input
10 10

correct output
DDDDDDDDDRUUUUUUUURDDDDDDDDRUU...

user output
-1

Test 24

Verdict: ACCEPTED

input
2 4

correct output
DRRRULLL

user output
DRRRULLL

Test 25

Verdict: ACCEPTED

input
9 2

correct output
DDDDDDDDRUUUUUUUUL

user output
DDDDDDDDRUUUUUUUUL

Test 26

Verdict: ACCEPTED

input
2 5

correct output
RRRRDLLLLU

user output
DRRRRULLLL

Test 27

Verdict:

input
56 60

correct output
DDDDDDDDDDDDDDDDDDDDDDDDDDDDDD...

user output
(empty)

Test 28

Verdict:

input
43 100

correct output
DDDDDDDDDDDDDDDDDDDDDDDDDDDDDD...

user output
(empty)

Test 29

Verdict:

input
45 20

correct output
DDDDDDDDDDDDDDDDDDDDDDDDDDDDDD...

user output
(empty)

Test 30

Verdict:

input
56 9

correct output
RRRRRRRRDLLLLLLLDRRRRRRRDLLLLL...

user output
(empty)

Test 31

Verdict:

input
97 91

correct output
-1

user output
(empty)

Test 32

Verdict: ACCEPTED

input
23 7

correct output
-1

user output
-1

Test 33

Verdict:

input
90 95

correct output
RRRRRRRRRRRRRRRRRRRRRRRRRRRRRR...

user output
-1

Test 34

Verdict:

input
9 24

correct output
DDDDDDDDRUUUUUUURDDDDDDDRUUUUU...

user output
(empty)

Test 35

Verdict:

input
88 3

correct output
RRDLDRDLDRDLDRDLDRDLDRDLDRDLDR...

user output
-1

Test 36

Verdict:

input
3 38

correct output
DDRURDRURDRURDRURDRURDRURDRURD...

user output
-1

Test 37

Verdict:

input
111 119

correct output
-1

user output
(empty)

Test 38

Verdict:

input
84 200

correct output
DDDDDDDDDDDDDDDDDDDDDDDDDDDDDD...

user output
(empty)

Test 39

Verdict:

input
88 38

correct output
DDDDDDDDDDDDDDDDDDDDDDDDDDDDDD...

user output
(empty)

Test 40

Verdict:

input
111 16

correct output
DDDDDDDDDDDDDDDDDDDDDDDDDDDDDD...

user output
-1

Test 41

Verdict:

input
194 181

correct output
RRRRRRRRRRRRRRRRRRRRRRRRRRRRRR...

user output
-1

Test 42

Verdict:

input
46 12

correct output
DDDDDDDDDDDDDDDDDDDDDDDDDDDDDD...

user output
(empty)

Test 43

Verdict:

input
179 190

correct output
DDDDDDDDDDDDDDDDDDDDDDDDDDDDDD...

user output
-1

Test 44

Verdict: ACCEPTED

input
17 47

correct output
-1

user output
-1

Test 45

Verdict:

input
175 4

correct output
DDDDDDDDDDDDDDDDDDDDDDDDDDDDDD...

user output
-1

Test 46

Verdict:

input
4 74

correct output
DDDRUURDDRUURDDRUURDDRUURDDRUU...

user output
-1

Test 47

Verdict:

input
550 594

correct output
DDDDDDDDDDDDDDDDDDDDDDDDDDDDDD...

user output
(empty)

Test 48

Verdict:

input
418 998

correct output
DDDDDDDDDDDDDDDDDDDDDDDDDDDDDD...

user output
(empty)

Test 49

Verdict:

input
437 186

correct output
DDDDDDDDDDDDDDDDDDDDDDDDDDDDDD...

user output
(empty)

Test 50

Verdict:

input
552 72

correct output
DDDDDDDDDDDDDDDDDDDDDDDDDDDDDD...

user output
(empty)

Test 51

Verdict:

input
968 901

correct output
RRRRRRRRRRRRRRRRRRRRRRRRRRRRRR...

user output
(empty)

Test 52

Verdict:

input
223 57

correct output
-1

user output
(empty)

Test 53

Verdict:

input
893 948

correct output
DDDDDDDDDDDDDDDDDDDDDDDDDDDDDD...

user output
(empty)

Test 54

Verdict:

input
78 229

correct output
RRRRRRRRRRRRRRRRRRRRRRRRRRRRRR...

user output
(empty)

Test 55

Verdict:

input
874 13

correct output
RRRRRRRRRRRRDLLLLLLLLLLLDRRRRR...

user output
(empty)

Test 56

Verdict:

input
12 366

correct output
DDDDDDDDDDDRUUUUUUUUUURDDDDDDD...

user output
(empty)