1 /* 2 题意:问最少替换'*'为'.',使得'.'连通的都是矩形 3 BFS:搜索想法很奇妙,先把'.'的入队,然后对于每个'.'八个方向寻找 4 在2*2的方格里,若只有一个是'*',那么它一定要被替换掉 5 */ 6 #include <cstdio> 7 #include <iostream> 8 #include <algorithm> 9 #include <cstring> 10 #include <queue> 11 using namespace std; 12 13 const int MAXN = 2e3 + 10; 14 const int INF = 0x3f3f3f3f; 15 int n, m; 16 int dx[4][3] = {{1,0,1},{0,-1,-1},{-1,-1,0},{1,1,0}}; 17 int dy[4][3] = {{0,1,1},{1,0,1},{0,-1,-1},{0,-1,-1}}; 18 char s[MAXN][MAXN]; 19 20 bool ok(int x, int y) 21 { 22 if (x < 0 || x >= n) return false; 23 if (y < 0 || y >= m) return false; 24 25 return true; 26 } 27 28 void BFS(void) 29 { 30 queue<pair<int, int> > Q; 31 for (int i=0; i<n; ++i) 32 { 33 for (int j=0; j<m; ++j) 34 { 35 if (s[i][j] == '.') 36 { 37 Q.push (make_pair (i, j)); 38 } 39 } 40 } 41 42 while (!Q.empty ()) 43 { 44 int x = Q.front ().first; int y = Q.front ().second; 45 Q.pop (); 46 for (int i=0; i<4; ++i) 47 { 48 int cnt = 0; int px, py; bool flag = true; 49 for (int j=0; j<3; ++j) 50 { 51 int tx = x + dx[i][j]; int ty = y + dy[i][j]; 52 if (ok (tx, ty)) 53 { 54 if (s[tx][ty] == '*') 55 { 56 cnt++; px = tx; py = ty; 57 } 58 } 59 else flag = false; 60 } 61 if (flag && cnt == 1) 62 { 63 s[px][py] = '.'; Q.push (make_pair (px, py)); 64 } 65 } 66 } 67 } 68 69 int main(void) //Codeforces Round #297 (Div. 2) D. Arthur and Walls 70 { 71 while (scanf ("%d%d", &n, &m) == 2) 72 { 73 for (int i=0; i<n; ++i) scanf ("%s", s[i]); 74 BFS (); 75 for (int i=0; i<n; ++i) printf ("%s\n", s[i]); 76 } 77 78 return 0; 79 } 80 81 82 /* 83 5 5 84 .*.*. 85 ***** 86 .*.*. 87 ***** 88 .*.*. 89 6 7 90 ***.*.* 91 ..*.*.* 92 *.*.*.* 93 *.*.*.* 94 ..*...* 95 ******* 96 */