Advertisement
shamiul93

Guilty Prince - LightOJ

Feb 8th, 2017
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.56 KB | None | 0 0
  1. #include<iostream>
  2. #include<algorithm>
  3. #include<queue>
  4. #include<map>
  5. #include<vector>
  6. #include<cstdio>
  7. #include<cstdlib>
  8. #include<cstring>
  9.  
  10. using namespace std;
  11.  
  12. #define ll long long
  13.  
  14. #define PII pair<int,int>
  15.  
  16. ll BFS()
  17. {
  18.     ll ans = 1;
  19.     int colm, row;
  20.     cin >> colm >> row;
  21.     char Grid[21][21] = {};
  22.     int visited[21][21] ;
  23.     memset(visited , 0 , sizeof(visited));
  24.     queue<PII> q;
  25.     PII pos;
  26.  
  27.     for (int i = 0; i < row; i++)
  28.     {
  29.         for (int j = 0; j < colm; j++)
  30.         {
  31.             cin >> Grid[i][j];
  32.             if (Grid[i][j] == '@')
  33.             {
  34.                 pos = make_pair(i, j);
  35.                 visited[i][j] = 1;
  36.                 q.push(pos);
  37.             }
  38.         }
  39.     }
  40.  
  41.     int dx[] = { -1, 0, 0, 1 };
  42.     int dy[] = { 0, 1, -1, 0 };
  43.  
  44.     while (!q.empty())
  45.     {
  46.         PII p;
  47.         p = q.front();
  48.         q.pop();
  49.         for (int i = 0; i < 4; i++)
  50.         {
  51.             int tx, ty;
  52.             tx = p.first + dx[i];
  53.             ty = p.second + dy[i];
  54.  
  55.             if (tx >= 0 && tx < row && ty >= 0 && ty < colm && Grid[tx][ty] != '#')
  56.             {
  57.                 if (visited[tx][ty] == 0)
  58.                 {
  59.                     ans++;
  60.                     visited[tx][ty] = 1;
  61.                     q.push(make_pair(tx, ty));
  62.                 }
  63.             }
  64.         }
  65.     }
  66.  
  67.     return ans;
  68.  
  69. }
  70.  
  71. int main()
  72. {
  73.     int Test, t = 0;
  74.     ll ans;
  75.     cin >> Test;
  76.     while (Test--)
  77.     {
  78.         t++;
  79.         ans = BFS();
  80.         printf("Case %d: %lld\n", t, ans);
  81.     }
  82.  
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement