hihoCoder#1119 小Hi小Ho的惊天大作战:扫雷·二

原题地址

 

没有复杂算法,就是麻烦,写起来细节比较多,比较考验细心,一次AC好开心。

 

代码:

  1 #include <iostream>
  2 #include <vector>
  3 #include <cstring>
  4 #include <cstdlib>
  5 
  6 using namespace std;
  7 
  8 #define SIZE 400
  9 
 10 int N, M;
 11 int map[SIZE][SIZE];
 12 int res[SIZE][SIZE];
 13 
 14 vector<pair<int, int> > unknown_neighbors(int i, int j) {
 15   vector<pair<int, int> > nbs;
 16 
 17   for (int ii = -1; ii < 2; ii++)
 18     for (int jj = -1; jj < 2; jj++) {
 19       int r = ii + i;
 20       int c = jj + j;
 21       if (r >= 0 && r < N && c >= 0 && c < M && (r != i || c != j) && map[r][c] < 0)
 22         nbs.push_back(pair<int, int>(r, c));
 23     }
 24 
 25   return nbs;
 26 }
 27 
 28 vector<pair<int, int> > known_counterparts(int i, int j) {
 29   vector<pair<int, int> > cps;
 30 
 31   for (int ii = -2; ii < 3; ii++) {
 32     for (int jj = -2; jj < 3; jj++) {
 33       int r = ii + i;
 34       int c = jj + j;
 35       if (r >= 0 && r < N && c >= 0 && c < M && (r != i || c != j) && map[r][c] >= 0)
 36         cps.push_back(pair<int, int>(r, c));
 37     }
 38   }
 39 
 40   return cps;
 41 }
 42 
 43 vector<pair<int, int> > differ(vector<pair<int, int> > &a, vector<pair<int, int> > &b) {
 44   vector<pair<int, int> > diff;
 45   int count = 0;
 46 
 47   for (auto pa : a) {
 48     bool found = false;
 49     for (auto pb : b) {
 50       if (pb == pa) {
 51         found = true;
 52         count++;
 53         break;
 54       }
 55     }
 56     if (!found)
 57       diff.push_back(pa);
 58   }
 59 
 60   if (count < b.size())
 61     diff.clear();
 62 
 63   return diff;
 64 }
 65 
 66 void merge_pos(int i, int j, int v) {
 67   if (res[i][j] == -1 || res[i][j] == v)
 68     res[i][j] = v;
 69   else
 70     res[i][j] = -2;
 71 }
 72 
 73 void solve() {
 74   for (int i = 0; i < N; i++)
 75     for (int j = 0; j < M; j++) {
 76       if (map[i][j] == 0) {
 77         vector<pair<int, int> > nbs = unknown_neighbors(i, j);
 78         for (auto p : nbs)
 79           merge_pos(p.first, p.second, 0);
 80       }
 81       else if (map[i][j] > 0) {
 82         vector<pair<int, int> > nbs = unknown_neighbors(i, j);
 83         if (nbs.size() == map[i][j]) {
 84           for (auto p : nbs)
 85             merge_pos(p.first, p.second, 1);
 86           continue;
 87         }
 88 
 89         vector<pair<int, int> > cps = known_counterparts(i, j);
 90         for (auto p : cps) {
 91           vector<pair<int, int> > cp_nbs = unknown_neighbors(p.first, p.second);
 92           if (nbs.size() <= cp_nbs.size() || map[i][j] <= map[p.first][p.second])
 93             continue;
 94           vector<pair<int, int> > diff = differ(nbs, cp_nbs);
 95           if ((int) (nbs.size() - cp_nbs.size()) == map[i][j] - map[p.first][p.second] && !diff.empty())
 96             for (auto dp : diff)
 97               merge_pos(dp.first, dp.second, 1);
 98         }
 99       }
100     }
101 }
102 
103 int main() {
104   int n;
105 
106   cin >> n;
107   while (n--) {
108     cin >> N >> M;
109     for (int i = 0; i < N; i++)
110       for (int j = 0; j < M; j++)
111         cin >> map[i][j];
112     memset(res, -1, SIZE * SIZE * sizeof(int));
113 
114     solve();
115 
116     int mine = 0;
117     int not_mine = 0;
118 
119     for (int i = 0; i < N; i++)
120       for (int j = 0; j < M; j++) {
121         mine += (res[i][j] == 1 ? 1 : 0);
122         not_mine += (res[i][j] == 0 ? 1 : 0);
123       }
124 
125     cout << mine << " " << not_mine << endl;
126   }
127 
128   return 0;
129 }

 

posted @ 2015-03-22 14:57  李舜阳  阅读(640)  评论(0编辑  收藏  举报