SuitNdtie

Deep Blue Sea EXAM06

Mar 28th, 2019
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.12 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<queue>
  3. using namespace std;
  4. char sea[510][510];
  5. bool visited[510][510];
  6. int n,m;
  7. typedef struct{
  8.     int I;
  9.     int J;
  10. }pos;
  11. int max(int a,int b){
  12.     return (a > b ? a : b);
  13. }
  14.  
  15. int dfs(int I,int J){
  16. pos move[4] = {{-1,0},{0,1},{1,0},{0,-1}};
  17.     queue<pos> q;
  18.     q.push({I,J});
  19.     int count = 0;
  20.     while(!q.empty()){
  21.         int nowI = q.front().I;
  22.         int nowJ = q.front().J;
  23.         q.pop();
  24.         if(!visited[nowI][nowJ]){
  25.             visited[nowI][nowJ] = true;
  26.             if(sea[nowI][nowJ] == '*'){
  27.                 count++;
  28.             }
  29.             for(int i=0;i<4;i++){
  30.                 int newI = nowI + move[i].I;
  31.                 int newJ = nowJ + move[i].J;
  32.                 if(newI < 0)newI = n-1;
  33.                 else if(newI >= n)newI = 0;
  34.                 if(newJ < 0)newJ = m-1;
  35.                 else if(newJ >= m)newJ = 0;
  36.                
  37.                 if(sea[newI][newJ] != '#' && !visited[newI][newJ]){
  38.                     q.push({newI,newJ});
  39.                 }
  40.             }
  41.         }
  42.     }
  43.     return count;
  44. }
  45.  
  46. int main()
  47. {
  48.     scanf("%d %d",&n,&m);
  49.     for(int i=0;i<n;i++){
  50.         scanf("%s",sea[i]);
  51.     }
  52.     int maxa = 0;
  53.     for(int i=0;i<n;i++){
  54.         for(int j=0;j<m;j++){
  55.             if(sea[i][j] != '#'){
  56.                 int cal = dfs(i,j);
  57.                 maxa = max(maxa,cal);
  58.             }
  59.         }
  60.     }
  61.     printf("%d",maxa);
  62. }
Advertisement
Add Comment
Please, Sign In to add comment