-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathGNY07D.cpp
61 lines (52 loc) · 1.23 KB
/
GNY07D.cpp
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
/*
ad-hoc > implementation
difficulty: easy
date: 21/Feb/2020
by: @brpapa
*/
#include <iostream>
#include <vector>
using namespace std;
#define VISITED -1
int grid[22][22];
int R, C;
vector<int> binDecoded;
int d;
int dr[4] = {0, 1, 0,-1};
int dc[4] = {1, 0,-1, 0};
void walk(int r, int c) {
binDecoded.push_back(grid[r][c]);
grid[r][c] = VISITED;
bool has = false;
for (int i = 0; !has && i < 4; i++) {
int nr = r+dr[d], nc = c+dc[d];
if (nr < 0 || nr == R || nc < 0 || nc == C || grid[nr][nc] == VISITED)
d = (d+1)%4;
else has = true;
}
if (has) walk(r+dr[d], c+dc[d]);
}
int main() {
int T; cin >> T;
for (int t = 1; t <= T; t++) {
cin >> R >> C;
string bin; cin >> bin;
for (int b = 0; b < R*C; b++)
grid[b/C][b%C] = bin[b] == '1'? 1:0;
d = 0;
binDecoded.clear();
walk(0, 0);
vector<char> ans;
for (int i = 0; i < R*C - (R*C)%5; i += 5) {
int dec = 0;
for (int j = 4; j >= 0; j--)
if (binDecoded[i+4-j])
dec |= (1 << j);
ans.push_back(dec == 0? ' ' : 'A'+dec-1);
}
cout << t << " ";
for (char c : ans) cout << c;
cout << endl;
}
return 0;
}