forked from ngthanhtrung23/CompetitiveProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathB.cpp
More file actions
168 lines (146 loc) · 4.48 KB
/
B.cpp
File metadata and controls
168 lines (146 loc) · 4.48 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
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
#pragma comment(linker, "/STACK:16777216")
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <string>
#include <deque>
#include <complex>
#include <sstream>
#include <iomanip>
#define FOR(i,a,b) for(int i=(a),_b=(b); i<=_b; i++)
#define FORD(i,a,b) for(int i=(a),_b=(b); i>=_b; i--)
#define REP(i,a) for(int i=0,_a=(a); i<_a; i++)
#define ll long long
#define F first
#define S second
#define PB push_back
#define MP make_pair
using namespace std;
const double PI = acos(-1.0);
int m, n, cnt[1011][1011];
char a[1011][1011];
bool mark[300];
const int di[4] = {-1,1,0,0};
const int dj[4] = {0,0,-1,1};
int now;
pair<int,int> x[100];
bool inside(int i, int j) {
if (i < 0 || j < 0) return false;
if (i > m || j > n) return false;
return true;
}
char getColor() {
FOR(cur,'0','9') mark[cur] = false;
FOR(p,1,now) {
REP(dir,4) {
int i = x[p].F + di[dir];
int j = x[p].S + dj[dir];
if (a[i][j] >= '0' && a[i][j] <= '9') mark[a[i][j]] = true;
}
}
char c = '0';
while (mark[c]) c++;
return c;
}
bool check(char c) {
c = getColor();
FOR(p,1,now) if (a[x[p].F][x[p].S] != '.') return false;
bool ok = true;
FOR(p,1,now) a[x[p].F][x[p].S] = c;
FOR(p,1,now) {
int i = x[p].F, j = x[p].S;
REP(dir,4) {
int ii = i + di[dir], jj = j + dj[dir];
if (a[ii][jj] == '.') {
cnt[ii][jj]--;
if (!cnt[ii][jj]) ok = false;
}
}
}
if (!ok) { // Roll back
FOR(p,1,now) {
int i = x[p].F, j = x[p].S;
REP(dir,4) {
int ii = i + di[dir], jj = j + dj[dir];
if (a[ii][jj] == '.') {
cnt[ii][jj]++;
if (!cnt[ii][jj]) ok = false;
}
}
}
FOR(p,1,now) a[x[p].F][x[p].S] = '.';
}
return ok;
}
int main() {
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
cin >> m >> n;
FOR(i,1,m) FOR(j,1,n) cin >> a[i][j];
FOR(i,1,m) FOR(j,1,n) if (a[i][j] == '.') {
cnt[i][j] = 0;
REP(dir,4) {
int ii = i + di[dir], jj = j + dj[dir];
if (inside(ii,jj) && a[ii][jj] == '.') cnt[i][j]++;
}
if (!cnt[i][j]) {
puts("-1");
return 0;
}
}
else cnt[i][j] = -1;
FOR(i,1,m) FOR(j,1,n) if (a[i][j] == '.') {
// FOR(u,1,m) {
// FOR(v,1,n) cout << a[u][v]; cout << endl;
// }
char c;
// xx
now = 2; x[1] = MP(i,j); x[2] = MP(i,j+1);
if (check(c)) continue;
now = 2; x[1] = MP(i,j); x[2] = MP(i+1,j);
if (check(c)) continue;
// xxx
now = 3; x[1] = MP(i,j); x[2] = MP(i,j+1); x[3] = MP(i,j+2);
if (check(c)) continue;
now = 3; x[1] = MP(i,j); x[2] = MP(i+1,j); x[3] = MP(i+2,j);
if (check(c)) continue;
// 3
now = 3; x[1] = MP(i,j); x[2] = MP(i,j+1); x[3] = MP(i+1,j);
if (check(c)) continue;
now = 3; x[1] = MP(i,j); x[2] = MP(i,j+1); x[3] = MP(i+1,j+1);
if (check(c)) continue;
now = 3; x[1] = MP(i,j); x[2] = MP(i+1,j); x[3] = MP(i+1,j+1);
if (check(c)) continue;
now = 3; x[1] = MP(i,j); x[2] = MP(i+1,j-1); x[3] = MP(i+1,j);
if (check(c)) continue;
// 4
now = 4;
x[1] = MP(i,j); x[2] = MP(i,j+1); x[3] = MP(i,j+2); x[4] = MP(i+1,j+1);
if (check(c)) continue;
x[1] = MP(i,j); x[2] = MP(i+1,j-1); x[3] = MP(i+1,j); x[4] = MP(i+2,j);
if (check(c)) continue;
x[1] = MP(i,j); x[2] = MP(i+1,j); x[3] = MP(i+1,j+1); x[4] = MP(i+2,j);
if (check(c)) continue;
x[1] = MP(i,j); x[2] = MP(i+1,j-1); x[3] = MP(i+1,j); x[4] = MP(i+1,j+1);
if (check(c)) continue;
// 5
now = 5;
x[1] = MP(i,j); x[2] = MP(i+1,j-1); x[3] = MP(i+1,j); x[4] = MP(i+1,j+1); x[5] = MP(i+2,j);
if (check(c)) continue;
puts("-1");
return 0;
}
FOR(i,1,m) {
FOR(j,1,n) cout << a[i][j];
puts("");
}
return 0;
}