51nod1448(yy)

题目链接: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1448

题意: 中文题诶~ 不过要仔细看题, 原来颜色是被覆盖而非变成原来相反的颜色~

思路: 首先找到一个 k * k 的纯色矩阵 s1, 不需要管这个纯色矩阵是不是白色, 因为如果不是白色的话用白色印章将其覆盖一次即可. 然后再取 s1 中任意一点为左上角顶点, 引出一个 k * k 的矩阵 s2, 若 s2 中除了与 s1 公共部分外是纯色的, 那么显然可以将 s2 也覆盖成白色的. 接着从 s2 中任取一点为左上角顶点, 再引出一个 k * k 的矩阵 s3, 若其除了与前面的 s1, s2 公共部分外为纯色, 显然也是可以将 s3 覆盖成白色的...后面的依次类推即可.

最后再检查一遍是否还存在没被标记的 B, 若存在则输出 Impossible, 反之输出 Possible.

代码:

 1 #include <iostream>
 2 #include <string.h>
 3 using namespace std;
 4 
 5 const int MAXN = 25;
 6 int vis[MAXN][MAXN];
 7 string s[MAXN];
 8 
 9 int main(void){
10     int t;
11     cin >> t;
12     while(t--){
13         int n, k;
14         memset(vis, 0, sizeof(vis));
15         cin >> n >> k;
16         for(int i = 0; i < n; i++){
17             cin >> s[i];
18         }
19         bool flag = true;
20         while(flag){
21             flag = false;
22             for(int i = 0; i + k < n + 1; i++){
23                 for(int j = 0; j + k < n + 1; j++){
24                     int cnt1 = 0, cnt2 = 0, cnt = 0;
25                     for(int l = 0; l < k; l++){
26                         for(int h = 0; h < k; h++){
27                             int x = i + l;
28                             int y = j + h;
29                             if(vis[x][y]) continue;
30                             cnt = 1;
31                             if(s[x][y] == 'B') cnt1++;
32                             else cnt2++;
33                         }
34                     }
35                     if(cnt && (cnt1 == 0 || cnt2 == 0)){
36                         flag = true;
37                         for(int l = 0; l < k; l++){
38                             for(int h = 0; h < k; h++){
39                                 int x = i + l;
40                                 int y = j + h;
41                                 vis[x][y] = 1;
42                             }
43                         }
44                     }
45                 }
46             }
47         }
48         flag = true;
49         for(int i = 0; i < n; i++){
50             for(int j = 0; j < n; j++){
51                 if(!vis[i][j] && s[i][j] == 'B') flag = false;
52             }
53         }
54         if(flag) cout << "Possible" << endl;
55         else cout << "Impossible" << endl;
56     }
57     return 0;
58 }
View Code
原文地址:https://www.cnblogs.com/geloutingyu/p/7646600.html