-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfb_3.cpp
104 lines (101 loc) · 2.31 KB
/
fb_3.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
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
// #include<bits/stdc++.h>
// using namespace std ;
// int main()
// {
// #ifndef ONLINE_JUDGE
// freopen("input.txt","r",stdin) ;
// freopen("output.txt" , "w" , stdout);
// #endif
// int x ; cin>>x ;
// cout<< (x+10) <<"\n";
// return 0 ;
// }
#include <bits/stdc++.h>
#define int long long
using namespace std;
int n, m;
char a[5002][5002], b[5002][5002], s[5002][5002];
void dfs(int x, int y)
{
stack<pair<int, int>> A;
A.push({x, y});
while (!A.empty())
{
pair<int, int> P = A.top();
A.pop();
int x = P.first, y = P.second;
if (x < 1 || x > n || y < 1 || y > m || a[x][y] == '#')
continue;
a[x][y] = '#';
s[x - 1][y]--;
if (s[x - 1][y] < 2)
A.push({x - 1, y});
s[x + 1][y]--;
if (s[x + 1][y] < 2)
A.push({x + 1, y});
s[x][y - 1]--;
if (s[x][y - 1] < 2)
A.push({x, y - 1});
s[x][y + 1]--;
if (s[x][y + 1] < 2)
A.push({x, y + 1});
}
}
void solve()
{
cin >> n >> m;
for (int i = 0; i <= n + 1; ++i)
for (int j = 0; j <= n + 1; ++j)
{
a[i][j] = '.';
s[i][j] = 0;
}
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j)
{
cin >> a[i][j];
b[i][j] = a[i][j];
if (a[i][j] != '#')
{
s[i - 1][j]++;
s[i + 1][j]++;
s[i][j - 1]++;
s[i][j + 1]++;
}
}
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j)
if (s[i][j] < 2)
dfs(i, j);
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j)
if (a[i][j] == '#' && b[i][j] == '^')
{
puts("Impossible");
return;
}
puts("Possible");
for (int i = 1; i <= n; ++i)
{
for (int j = 1; j <= m; ++j)
if (a[i][j] != '#')
putchar('^');
else
putchar(b[i][j]);
putchar('\n');
}
}
signed main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int T;
cin >> T;
for (int i = 1; i <= T; ++i)
{
cout << "Case #" << i << ": ";
solve();
}
}