File size: 6,643 Bytes
14c9c2b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include <vector>
#include <numeric>
#include <algorithm>
#include <set>
#include <cmath>
#include <random>
#include <chrono>

using namespace std;

static mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
static bool adj[45][45];
static vector<int> adj_list[45];
static int N_glob, M_glob;

struct Candidate {
    vector<int> row;
    int score;
};

inline bool is_adj_or_same(int u, int v) {
    if (u == v) return true;
    return adj[u][v];
}

std::vector<std::vector<int>> create_map(int N, int M,
    std::vector<int> A, std::vector<int> B) {
    
    N_glob = N;
    M_glob = M;
    
    for(int i=1; i<=N; ++i) {
        for(int j=1; j<=N; ++j) adj[i][j] = false;
        adj_list[i].clear();
    }
    
    for(int i=0; i<M; ++i) {
        int u = A[i];
        int v = B[i];
        adj[u][v] = adj[v][u] = true;
        adj_list[u].push_back(v);
        adj_list[v].push_back(u);
    }
    
    int lower_bound_K = ceil(sqrt(M/2.0));
    int node_bound = ceil(sqrt(N));
    int start_W = max({lower_bound_K, node_bound, 1});
    if (start_W < 2 && N > 1) start_W = 2;

    auto start_time = chrono::steady_clock::now();
    
    for (int W = start_W; W <= 240; ++W) {
        for (int attempt = 0; attempt < 5; ++attempt) {
            if (chrono::duration_cast<chrono::milliseconds>(chrono::steady_clock::now() - start_time).count() > 1900) {
                // Time limit approaching, stop trying
                break; 
            }

            vector<vector<int>> grid;
            set<pair<int,int>> covered_edges;
            set<int> covered_nodes;
            
            bool possible = true;
            for (int r = 0; r < 240; ++r) {
                if (covered_edges.size() == M && covered_nodes.size() == N) {
                    int H = grid.size();
                    int K = max(W, H);
                    vector<vector<int>> final_map(K, vector<int>(K));
                    for(int i=0; i<K; ++i) {
                        const vector<int>& src_row = (i < H) ? grid[i] : grid[H-1];
                        for(int j=0; j<K; ++j) {
                            final_map[i][j] = (j < W) ? src_row[j] : src_row[W-1];
                        }
                    }
                    return final_map;
                }
                
                if (r >= 240) { possible = false; break; }
                
                int beam_width = 40;
                vector<Candidate> beam;
                beam.push_back({{}, 0});
                
                for (int c = 0; c < W; ++c) {
                    vector<Candidate> next_beam;
                    int up_node = (r > 0) ? grid[r-1][c] : -1;
                    
                    for (const auto& cand : beam) {
                        int left_node = (c > 0) ? cand.row.back() : -1;
                        
                        vector<int> possibilities;
                        auto check_and_add = [&](int v) {
                            if (up_node != -1 && !is_adj_or_same(v, up_node)) return;
                            possibilities.push_back(v);
                        };
                        
                        if (left_node != -1) {
                            check_and_add(left_node);
                            for(int v : adj_list[left_node]) check_and_add(v);
                        } else if (up_node != -1) {
                            check_and_add(up_node);
                            for(int v : adj_list[up_node]) check_and_add(v);
                        } else {
                             // Random start
                             for(int k=0; k<10; ++k) {
                                 possibilities.push_back((rng() % N) + 1);
                             }
                        }
                        
                        if (possibilities.empty()) continue;
                        
                        sort(possibilities.begin(), possibilities.end());
                        possibilities.erase(unique(possibilities.begin(), possibilities.end()), possibilities.end());
                        shuffle(possibilities.begin(), possibilities.end(), rng);
                        if (possibilities.size() > 8) possibilities.resize(8);
                        
                        for (int v : possibilities) {
                            int score = cand.score;
                            if (covered_nodes.find(v) == covered_nodes.end()) score += 5000;
                             
                            if (left_node != -1 && left_node != v) {
                                int mn = min(left_node, v), mx = max(left_node, v);
                                if (covered_edges.find({mn, mx}) == covered_edges.end()) score += 100;
                            }
                            if (up_node != -1 && up_node != v) {
                                int mn = min(up_node, v), mx = max(up_node, v);
                                if (covered_edges.find({mn, mx}) == covered_edges.end()) score += 100;
                            }
                             
                            Candidate next = cand;
                            next.row.push_back(v);
                            next.score = score;
                            next_beam.push_back(next);
                        }
                    }
                    
                    if (next_beam.empty()) {
                        beam.clear();
                        break;
                    }
                    
                    sort(next_beam.begin(), next_beam.end(), [](const Candidate& a, const Candidate& b){
                        return a.score > b.score;
                    });
                    if (next_beam.size() > beam_width) next_beam.resize(beam_width);
                    beam = next_beam;
                }
                
                if (beam.empty()) {
                    possible = false;
                    break;
                }
                
                const auto& best_row = beam[0].row;
                grid.push_back(best_row);
                
                for (int c = 0; c < W; ++c) {
                    int v = best_row[c];
                    covered_nodes.insert(v);
                    if (c > 0) {
                        int u = best_row[c-1];
                        if (u != v) covered_edges.insert({min(u,v), max(u,v)});
                    }
                    if (r > 0) {
                        int u = grid[r-1][c];
                        if (u != v) covered_edges.insert({min(u,v), max(u,v)});
                    }
                }
            }
        }
    }
    return {};
}