Advertisement
Manioc

SETAS14

Feb 3rd, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.52 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. char tab[507][507];
  6. int vis[507][507];
  7.  
  8. int N, cont;
  9. vector<pair<int, int> > bad;
  10.  
  11. int _x[] = {1,-1, 0, 0};
  12. int _y[] = {0,0, 1, -1};
  13.  
  14. bool check(int x, int y, int i){
  15.     if(x < N && x >= 0 && y < N && y >= 0 && !vis[x][y]){
  16.         if(tab[x][y] == '>' && _y[i] == -1) return true;
  17.         if(tab[x][y] == '<' && _y[i] == +1) return true;
  18.         if(tab[x][y] == 'A' && _x[i] == +1) return true;
  19.         if(tab[x][y] == 'V' && _x[i] == -1) return true;
  20.         return false;
  21.     }return false;
  22. }
  23. bool bfs(pair<int, int> no){
  24.     queue<pair<int, int> > q;
  25.     vis[no.first][no.second] = true;
  26.     q.push(no);
  27.     cont++;
  28.    
  29.     while(!q.empty()){
  30.         pair<int, int> front = q.front();
  31.         q.pop();
  32.        
  33.         for(int i = 0; i < 4; i++){
  34.             int x = front.first + _x[i];
  35.             int y = front.second + _y[i];
  36.             //printf("(%d %d)%c %d\n", x, y, tab[x][y], check(x, y, i));
  37.             if(check(x,y, i)){
  38.                 vis[x][y] = true;
  39.                 cont++;
  40.                 q.push(make_pair(x, y));
  41.                
  42.             }
  43.         }
  44.     }
  45. }
  46. int main(){
  47.     scanf("%d", &N);
  48.     cont = 0;
  49.     for(int i = 0; i < N; i++){
  50.         for(int j = 0; j < N; j++){
  51.             cin >> tab[i][j];
  52.             if(i == 0 && tab[i][j] == 'A') bad.push_back(make_pair(i,j));
  53.             if(i == N-1 && tab[i][j] == 'V') bad.push_back(make_pair(i,j));
  54.             if(j == 0 && tab[i][j] == '<') bad.push_back(make_pair(i,j));
  55.             if(j == N-1 && tab[i][j] == '>') bad.push_back(make_pair(i,j));
  56.         }
  57.     }
  58.     for(int i = 0; i < bad.size(); i++){
  59.         //printf("%d %d\n", bad[i].first, bad[i].second);
  60.         bfs(bad[i]);
  61.     }
  62.     printf("%d\n", (N*N)-cont);
  63.     return 0;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement