Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <queue>
- using namespace std;
- int x, y;
- char mat[101][101];
- void BFS(int a, int b)
- {
- queue<int> redx, redy;
- redx.push(a);
- redy.push(b);
- while(!redx.empty());
- {
- int m = redx.front();
- int n = redy.front();
- redx.pop();
- redy.pop();
- mat[m][n]='*';
- if(m+1<x and mat[m+1][n] == '@') { redx.push(m+1); redy.push(n); }
- if(m+1<x and n+1<y and mat[m+1][n+1] == '@') { redx.push(m+1); redy.push(n+1); }
- if(n+1<y and mat[m][n+1] == '@') { redx.push(m); redy.push(n+1); }
- if(m-1>=0 and n+1<y and mat[m-1][n+1] == '@') { redx.push(m-1); redy.push(n+1); }
- if(m-1>=0 and mat[m-1][n] == '@') { redx.push(m-1); redy.push(n); }
- if(m-1>=0 and n-1>=0 and mat[m-1][n-1] == '@') { redx.push(m-1); redy.push(n-1); }
- if(n-1>=0 and mat[m][n-1] == '@') { redx.push(m); redy.push(n-1); }
- if(m+1<x and n-1>=0 and mat[m+1][n-1] == '@') { redx.push(m+1); redy.push(n-1); }
- }
- }
- int main()
- {
- int i, j, nafteniP=0;
- cin >> x >> y;
- for(i=0; i<x; i++)
- for(j=0; j<y; j++)
- cin >> mat[i][j];
- for(i=0; i<x; i++)
- {
- for(j=0; j<y; j++)
- {
- if(mat[i][j] == '@'){ nafteniP++; BFS(i, j);}
- }
- }
- cout << nafteniP;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement