velimir

OvciDFSStack

Mar 30th, 2013
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.15 KB | None | 0 0
  1. #include <iostream>
  2. #include <stack>
  3. using namespace std;
  4. int ovci=0, volci=0, x, y;
  5. char mat[251][251];
  6. void DFS(int m, int n)
  7. {
  8.     stack<int> stx, sty;
  9.     stx.push(m);
  10.     sty.push(n);
  11.     while(!stx.empty())
  12.     {
  13.         int a = stx.top();
  14.         int b = sty.top();
  15.         stx.pop();
  16.         sty.pop();
  17.         if(mat[a][b]=='o')ovci++;
  18.         if(mat[a][b]=='v')volci++;
  19.         mat[a][b] = '#';
  20.         if(b+1<y and mat[a][b+1]!='#') { stx.push(a); sty.push(b+1); }
  21.         if(a+1<x and mat[a+1][b]!='#') { stx.push(a+1); sty.push(b); }
  22.         if(b-1>=0 and mat[a][b-1]!='#') { stx.push(a); sty.push(b-1); }
  23.         if(a-1>=0 and mat[a-1][b]!='#') { stx.push(a-1); sty.push(b); }
  24.     }
  25. }
  26.  
  27. int main()
  28. {
  29.     int i, j, finalOvci=0, finalVolci=0;
  30.     cin >> x >> y;
  31.     for(i=0; i<x; i++)
  32.         for(j=0; j<y; j++)
  33.             cin >> mat[i][j];
  34.     for(i=0; i<x; i++)
  35.         for(j=0; j<y; j++)
  36.         {
  37.             if(mat[i][j]!='#')DFS(i, j);
  38.             if(ovci>volci)finalOvci+=ovci;
  39.             else finalVolci+=volci;
  40.             ovci = 0; volci = 0;
  41.         }
  42.     cout << finalOvci << " " << finalVolci;
  43.     return 0;
  44. }
Advertisement
Add Comment
Please, Sign In to add comment