-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathmain.cpp
More file actions
86 lines (67 loc) · 1.88 KB
/
Copy pathmain.cpp
File metadata and controls
86 lines (67 loc) · 1.88 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
#include <iostream>
#include <utility>
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>
#include <map>
#include <queue>
using namespace std;
vector<string> lab;
vector<vector<int*> > l;
int n, m;
int* build(int i, int j) {
int* v = new int;
*v = 1;
queue< pair<int, int> > q;
q.push(make_pair(i, j));
l[i][j] = v;
while(!q.empty()) {
pair<int, int> p = q.front();
q.pop();
int i = p.first, j = p.second;
l[i][j] = v;
if(i > 0 && lab[i-1][j] == '.' && l[i-1][j] == NULL) { q.push(make_pair(i-1, j)); l[i-1][j] = v; (*v)++; }
if(i < n-1 && lab[i+1][j] == '.' && l[i+1][j] == NULL) { q.push(make_pair(i+1, j)); l[i+1][j] = v; (*v)++; }
if(j > 0 && lab[i][j-1] == '.' && l[i][j-1] == NULL) { q.push(make_pair(i, j-1)); l[i][j-1] = v; (*v)++; }
if(j < m-1 && lab[i][j+1] == '.' && l[i][j+1] == NULL) { q.push(make_pair(i, j+1)); l[i][j+1] = v; (*v)++; }
}
return v;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cin >> n >> m;
lab = vector<string>(n);
for(int i = 0; i < n; i++) {
cin >> lab[i];
}
l = vector<vector<int*> >(n, vector<int*>(m));
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
if(l[i][j]) continue;
if(lab[i][j] != '.') continue;
build(i, j);
}
}
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
if(l[i][j] != NULL)
cout << ".";
else {
int *T = (i > 0 ) ? l[i-1][j] : NULL;
int *L = (j > 0 ) ? l[i][j-1] : NULL;
int *B = (i < n-1) ? l[i+1][j] : NULL;
int *R = (j < m-1) ? l[i][j+1] : NULL;
int val = 1;
if(T != NULL) val += *T;
if(L != NULL && L != T) val += *L;
if(B != NULL && B != T && B != L) val += *B;
if(R != NULL && R != T && R != L && R != B) val += *R;
cout << (val%10);
}
}
cout << endl;
}
return 0;
}