forked from daiwb/Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathB.cpp
More file actions
100 lines (92 loc) · 1.73 KB
/
Copy pathB.cpp
File metadata and controls
100 lines (92 loc) · 1.73 KB
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
#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <iterator>
#include <algorithm>
using namespace std;
#define REP(i,n) for(int i=0;i<(n);++i)
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define MAXH 100
#define MAXW 100
int T, H, W;
int mat[MAXH + 5][MAXW + 5];
int mm[MAXH + 5][MAXW + 5];
int dir[4][2] = {{-1, 0}, {0, -1}, {0, 1}, {1, 0}};
char dd[27];
void run() {
cin >> H >> W;
REP(i,H) {
REP(j,W) {
cin >> mat[i][j];
}
}
REP(i,27) dd[i] = '*';
memset(mm, -1, sizeof(mm));
int num = 1;
REP(i,H) {
REP(j,W) {
if (mm[i][j] != -1) continue;
vector<pair<int, int> > q;
q.push_back(make_pair(i, j));
int ci = i, cj = j;
while (true) {
int idx = -1, low;
REP(d,4) {
int ti = ci + dir[d][0], tj = cj + dir[d][1];
if (ti < 0 || ti >= H || tj < 0 || tj >= W) continue;
if (mat[ti][tj] >= mat[ci][cj]) continue;
if (idx == -1) {
idx = d;
low = mat[ti][tj];
} else if (mat[ti][tj] < low) {
idx = d;
low = mat[ti][tj];
}
}
if (idx == -1) {
REP(qi,q.size()) {
mm[q[qi].first][q[qi].second] = num;
}
++num;
break;
}
int ni = ci + dir[idx][0], nj = cj + dir[idx][1];
if (mm[ni][nj] == -1) {
q.push_back(make_pair(ni, nj));
ci = ni, cj = nj;
} else {
REP(qi,q.size()) {
mm[q[qi].first][q[qi].second] = mm[ni][nj];
}
break;
}
}
}
}
char ch = 'a';
REP(i,H) {
REP(j,W) {
int n = mm[i][j];
if (dd[n] == '*') {
dd[n] = ch;
++ch;
}
}
}
REP(i,H) {
REP(j,W) {
if (j) cout << " ";
cout << dd[mm[i][j]];
}
cout << endl;
}
}
int main() {
cin >> T;
REP(t,T) {
cout << "Case #" << t + 1 << ":" << endl;
run();
}
return 0;
}