Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- char tab[507][507];
- int vis[507][507];
- int N, cont;
- vector<pair<int, int> > bad;
- int _x[] = {1,-1, 0, 0};
- int _y[] = {0,0, 1, -1};
- bool check(int x, int y, int i){
- if(x < N && x >= 0 && y < N && y >= 0 && !vis[x][y]){
- if(tab[x][y] == '>' && _y[i] == -1) return true;
- if(tab[x][y] == '<' && _y[i] == +1) return true;
- if(tab[x][y] == 'A' && _x[i] == +1) return true;
- if(tab[x][y] == 'V' && _x[i] == -1) return true;
- return false;
- }return false;
- }
- bool bfs(pair<int, int> no){
- queue<pair<int, int> > q;
- vis[no.first][no.second] = true;
- q.push(no);
- cont++;
- while(!q.empty()){
- pair<int, int> front = q.front();
- q.pop();
- for(int i = 0; i < 4; i++){
- int x = front.first + _x[i];
- int y = front.second + _y[i];
- //printf("(%d %d)%c %d\n", x, y, tab[x][y], check(x, y, i));
- if(check(x,y, i)){
- vis[x][y] = true;
- cont++;
- q.push(make_pair(x, y));
- }
- }
- }
- }
- int main(){
- scanf("%d", &N);
- cont = 0;
- for(int i = 0; i < N; i++){
- for(int j = 0; j < N; j++){
- cin >> tab[i][j];
- if(i == 0 && tab[i][j] == 'A') bad.push_back(make_pair(i,j));
- if(i == N-1 && tab[i][j] == 'V') bad.push_back(make_pair(i,j));
- if(j == 0 && tab[i][j] == '<') bad.push_back(make_pair(i,j));
- if(j == N-1 && tab[i][j] == '>') bad.push_back(make_pair(i,j));
- }
- }
- for(int i = 0; i < bad.size(); i++){
- //printf("%d %d\n", bad[i].first, bad[i].second);
- bfs(bad[i]);
- }
- printf("%d\n", (N*N)-cont);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement