CSES - Aalto Competitive Programming 2024 - wk9 - Mon - Results
Submission details
Task:Chess board tour
Sender:Nallue
Submission time:2024-11-04 17:38:34 +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.04 sdetails
#12ACCEPTED0.00 sdetails
#13ACCEPTED0.04 sdetails
#14ACCEPTED0.00 sdetails
#15ACCEPTED0.00 sdetails
#16ACCEPTED0.01 sdetails
#17ACCEPTED0.26 sdetails
#18ACCEPTED0.33 sdetails
#19ACCEPTED0.00 sdetails
#20ACCEPTED0.00 sdetails
#21--details
#22ACCEPTED0.00 sdetails
#23--details
#24ACCEPTED0.00 sdetails
#25ACCEPTED0.00 sdetails
#26ACCEPTED0.00 sdetails
#270.11 sdetails
#28--details
#29--details
#30--details
#31--details
#32--details
#330.63 sdetails
#34--details
#35--details
#36ACCEPTED0.00 sdetails
#37--details
#38--details
#39--details
#40--details
#41--details
#42--details
#43--details
#44--details
#45--details
#46--details
#470.40 sdetails
#480.40 sdetails
#490.39 sdetails
#50--details
#510.40 sdetails
#52--details
#530.40 sdetails
#54--details
#55--details
#560.16 sdetails

Compiler report

input/code.cpp: In function 'bool isSafe(int, const std::vector<std::vector<bool> >&, int*, Bitmask, int)':
input/code.cpp:10:9: warning: unused variable 'V' [-Wunused-variable]
   10 |     int V = graph.size();
      |         ^
input/code.cpp: In function 'int countUnvisitedNeighbors(int, const std::vector<std::vector<bool> >&, Bitmask)':
input/code.cpp:16:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<bool> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   16 |     for (int i = 0; i < graph.size(); i++) {
      |                     ~~^~~~~~~~~~~~~~
input/code.cpp: In function 'bool hamCycleUtil(std::vector<std::vector<bool> >&, int*, Bitmask, int)':
input/code.cpp:36:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   36 |     for (auto [_, v] : neighbors) {
      |               ^

Code

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

typedef bitset<100> Bitmask;

bool isSafe(int v, const vector<vector<bool>>& graph, int path[], Bitmask visited, int pos) {
    int V = graph.size();
    return graph[path[pos - 1]][v] && !visited[v];
}

int countUnvisitedNeighbors(int v, const vector<vector<bool>>& graph, Bitmask visited) {
    int count = 0;
    for (int i = 0; i < graph.size(); i++) {
        if (graph[v][i] && !visited[i]) count++;
    }
    return count;
}

bool hamCycleUtil(vector<vector<bool>>& graph, int path[], Bitmask visited, int pos) {
    int V = graph.size();
    if (pos == V) {
        return graph[path[pos - 1]][path[0]];
    }

    vector<pair<int, int>> neighbors;
    for (int v = 1; v < V; v++) {
        if (isSafe(v, graph, path, visited, pos)) {
            neighbors.push_back({countUnvisitedNeighbors(v, graph, visited), v});
        }
    }
    sort(neighbors.begin(), neighbors.end());

    for (auto [_, v] : neighbors) {
        path[pos] = v;
        visited[v] = true;

        if (hamCycleUtil(graph, path, visited, pos + 1)) return true;

        path[pos] = -1;
        visited[v] = false;
    }

    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(vector<vector<bool>>& graph, int n, int m) {
    int V = graph.size();
    int path[V];
    Bitmask visited;

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

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

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

int main() {
    int n, m;
    cin >> n >> m;
    vector<vector<bool>> adj(n * m, vector<bool>(n * m, false));
    for (int i = 0; i < n; i++) {
        for (int l = 0; l < m; l++) {
            int pos = i * m + l;
            if (i - 1 >= 0) adj[pos][(i - 1) * m + l] = true;
            if (i + 1 < n) adj[pos][(i + 1) * m + l] = true; 
            if (l - 1 >= 0) adj[pos][i * m + (l - 1)] = true;
            if (l + 1 < m) adj[pos][i * m + (l + 1)] = true; 
        }
    }

    hamCycle(adj, n, m);
    return 0;
}

Test details

Test 1

Verdict: ACCEPTED

input
2 2

correct output
DRUL

user output
RDLU

Test 2

Verdict: ACCEPTED

input
2 2

correct output
DRUL

user output
RDLU

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
RDLU

Test 5

Verdict: ACCEPTED

input
4 4

correct output
DDDRUURDDRUUULLL

user output
RRRDLLDRRDLLLUUU

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
RRRDLLDRRDLLLUUU

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
RDDLUU

Test 10

Verdict: ACCEPTED

input
4 2

correct output
DDDRUUUL

user output
RDDDLUUU

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
RDLU

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
RDLU

Test 15

Verdict: ACCEPTED

input
5 2

correct output
DDDDRUUUUL

user output
RDDDDLUUUU

Test 16

Verdict: ACCEPTED

input
2 3

correct output
RRDLLU

user output
DRRULL

Test 17

Verdict: ACCEPTED

input
6 7

correct output
RRRRRRDLLLLLDRRRRRDLLLLLDRRRRR...

user output
RRRRRRDLLLLLDRRRRRDLLLLLDRRRRR...

Test 18

Verdict: ACCEPTED

input
5 10

correct output
DDDDRUUURDDDRUUURDDDRUUURDDDRU...

user output
RRRRRRRRRDLLLLLLLLDRRRRRRRRDDL...

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
RDDDDDLUUUUU

Test 21

Verdict:

input
10 10

correct output
DDDDDDDDDRUUUUUUUURDDDDDDDDRUU...

user output
(empty)

Test 22

Verdict: ACCEPTED

input
3 2

correct output
DDRUUL

user output
RDDLUU

Test 23

Verdict:

input
10 10

correct output
DDDDDDDDDRUUUUUUUURDDDDDDDDRUU...

user output
(empty)

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
RDDDDDDDDLUUUUUUUU

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)

Error:
*** stack smashing detected ***: terminated

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:

input
23 7

correct output
-1

user output
(empty)

Test 33

Verdict:

input
90 95

correct output
RRRRRRRRRRRRRRRRRRRRRRRRRRRRRR...

user output
(empty)

Error:
*** stack smashing detected ***: terminated

Test 34

Verdict:

input
9 24

correct output
DDDDDDDDRUUUUUUURDDDDDDDRUUUUU...

user output
(empty)

Test 35

Verdict:

input
88 3

correct output
RRDLDRDLDRDLDRDLDRDLDRDLDRDLDR...

user output
(empty)

Test 36

Verdict: ACCEPTED

input
3 38

correct output
DDRURDRURDRURDRURDRURDRURDRURD...

user output
RRRRRRRRRRRRRRRRRRRRRRRRRRRRRR...
Truncated

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
(empty)

Test 41

Verdict:

input
194 181

correct output
RRRRRRRRRRRRRRRRRRRRRRRRRRRRRR...

user output
(empty)

Test 42

Verdict:

input
46 12

correct output
DDDDDDDDDDDDDDDDDDDDDDDDDDDDDD...

user output
(empty)

Test 43

Verdict:

input
179 190

correct output
DDDDDDDDDDDDDDDDDDDDDDDDDDDDDD...

user output
(empty)

Test 44

Verdict:

input
17 47

correct output
-1

user output
(empty)

Test 45

Verdict:

input
175 4

correct output
DDDDDDDDDDDDDDDDDDDDDDDDDDDDDD...

user output
(empty)

Test 46

Verdict:

input
4 74

correct output
DDDRUURDDRUURDDRUURDDRUURDDRUU...

user output
(empty)

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)

Error:
*** stack smashing detected ***: terminated