Advertisement
IlidanBabyRage

112701.cpp

Aug 2nd, 2015
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.20 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[4][2] = {{0, -1}, {1, 0}, {0, 1}, {-1, 0}};
  19. int modifier2[8][2] = {{0, -1}, {1, -1}, {1, 0}, {1, 1}, {0, 1}, {-1, 1}, {-1, 0}, {-1, -1}};
  20.  
  21. int main(){
  22.    
  23.     int n, m, a[1000][1000], cnt = 0, tx, ty, x1, y1;
  24.     char tmpc;
  25.     cin >> n >> m;
  26.     for (int i = 0; i < n; i++)
  27.         for (int j = 0; j < m; j++){
  28.             scanf("%c", &tmpc);
  29.             if (tmpc == '#'){
  30.                 a[i][j] = 1;
  31.                 cnt++;
  32.             }else if (tmpc == '.')
  33.                 a[i][j] = 0;
  34.             else
  35.                 j--;
  36.         }
  37.  
  38.     for (int y = 0; y < n; y++){
  39.         for (int x = 0; x < m; x++){
  40.             if (a[y][x] != 1)
  41.                 continue;
  42.             qii q;
  43.             q.push(make_pair(x, y));
  44.             while (q.size()){
  45.                 x1 = q.front().first;
  46.                 y1 = q.front().second;
  47.                 // cout << x1 << " " << y1 << ":" << endl;
  48.                 a[y1][x1] = 2;
  49.                 q.pop();
  50.                 int tx, ty;
  51.                 for (int i = 0; i < 4; i++){
  52.                     tx = x1 + modifier[i][0];
  53.                     ty = y1 + modifier[i][1];
  54.                     // cout << tx << " " << ty << ", ";
  55.                     if (tx < 0 || ty < 0 || tx >= m || ty >= n)
  56.                         continue;
  57.                     if (a[ty][tx] == 1){
  58.                         q.push(make_pair(tx, ty));
  59.                         a[ty][tx] = 2;
  60.                     }else if (a[ty][tx] == 0 && ok(ty, tx, a, n, m)){
  61.                         a[ty][tx] = 2;
  62.                         cnt++;
  63.                         // cout << tx << " " << ty << "!" << endl;
  64.                         q.push(make_pair(tx, ty));
  65.                     }
  66.                 }
  67.                 // cout << endl;
  68.             }
  69.             // cout << endl;
  70.         }
  71.     }
  72.  
  73.     cout << cnt << endl;
  74.  
  75.     return 0;
  76. }
  77.  
  78. bool ok(int y, int x, int a[1000][1000], int n, int m){
  79.     // cout << "ok(" << x << "," << y <<  ") : " << endl;
  80.     bool tmp;
  81.     int tx, ty;
  82.     for (int i = 0; i < 8; i += 2){
  83.         tmp = true;
  84.         for (int j = 0; j < 3 && tmp; j++){
  85.             tx = x + modifier2[(i + j) % 8][0];
  86.             ty = y + modifier2[(i + j) % 8][1];
  87.             // cout << tx << " " << ty << endl;
  88.             if (tx < 0 || ty < 0 || tx >= m || ty >= n || a[ty][tx] == 0)
  89.                 tmp = false;
  90.         }
  91.         if (tmp)
  92.             // cout << "true\n";
  93.             return true;
  94.             // cout << "fail\n";
  95.     }
  96.     // cout << "false\n";
  97.     return false;
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement