Advertisement
IlidanBabyRage

112690.cpp

Aug 28th, 2015
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.80 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <cstdio>
  4. #include <vector>
  5. #include <queue>
  6. #include <utility>
  7.  
  8. using namespace std;
  9.  
  10. typedef vector<int> vi;
  11. typedef pair<int, int> pii;
  12. typedef long long int lli;
  13. typedef queue<int> qi;
  14. typedef queue<pii> qii;
  15.  
  16. bool ok(int y, int x, int a[1000][1000], int n, int m);
  17.  
  18. int modifier[8][2] = {{0, -1}, {1, -1}, {1, 0}, {1, 1}, {0, 1}, {-1, 1}, {-1, 0}, {-1, -1}};
  19.  
  20. int main(){
  21.    
  22.     int n, m, a[1000][1000], cnt = 0, tx, ty, x1, y1;
  23.     char tmpc;
  24.     cin >> n >> m;
  25.     for (int i = 0; i < n; i++)
  26.         for (int j = 0; j < m; j++){
  27.             scanf("%c", &tmpc);
  28.             if (tmpc == '#'){
  29.                 a[i][j] = 1;
  30.                 cnt++;
  31.             }else if (tmpc == '.')
  32.                 a[i][j] = 0;
  33.             else
  34.                 j--;
  35.         }
  36.  
  37.     for (int y = 0; y < n; y++){
  38.         for (int x = 0; x < m; x++){
  39.             if (a[y][x] != 1)
  40.                 continue;
  41.             qii q;
  42.             q.push(make_pair(x, y));
  43.             while (q.size()){
  44.                 x1 = q.front().first;
  45.                 y1 = q.front().second;
  46.                 a[y1][x1] = 2;
  47.                 q.pop();
  48.                 int tx, ty;
  49.                 for (int i = 0; i < 8; i++){
  50.                     tx = x1 + modifier[i][0];
  51.                     ty = y1 + modifier[i][1];
  52.                     if (tx < 0 || ty < 0 || tx >= m || ty >= n)
  53.                         continue;
  54.                     if (a[ty][tx] == 1){
  55.                         q.push(make_pair(tx, ty));
  56.                         a[ty][tx] = 2;
  57.                     }else if (a[ty][tx] == 0 && ok(ty, tx, a, n, m)){
  58.                         a[ty][tx] = 2;
  59.                         cnt++;
  60.                         q.push(make_pair(tx, ty));
  61.                     }
  62.                 }
  63.             }
  64.         }
  65.     }
  66.  
  67.     cout << cnt << endl;
  68.  
  69.     return 0;
  70. }
  71.  
  72. bool ok(int y, int x, int a[1000][1000], int n, int m){
  73.     bool tmp;
  74.     int tx, ty;
  75.     for (int i = 0; i < 8; i += 2){
  76.         tmp = true;
  77.         for (int j = 0; j < 3 && tmp; j++){
  78.             tx = x + modifier[(i + j) % 8][0];
  79.             ty = y + modifier[(i + j) % 8][1];
  80.             if (tx < 0 || ty < 0 || tx >= m || ty >= n || a[ty][tx] == 0)
  81.                 tmp = false;
  82.         }
  83.         if (tmp)
  84.             return true;
  85.     }
  86.     return false;
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement