Advertisement
JosepRivaille

P90766: Tresors en un mapa (3)

Apr 30th, 2016
815
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.49 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <queue>
  4. #include <stack>
  5. using namespace std;
  6.  
  7. typedef vector<char> Row;
  8. typedef vector<Row> Graph;
  9.  
  10.  
  11. int bfs(const Graph &G, int xx, int yy)
  12. {
  13.   Graph visited(G.size(), Row(G[0].size(), 'F'));
  14.   queue< pair<int, int> >  Q;
  15.   Q.push(make_pair(xx, yy));
  16.   visited[xx][yy] = 'T';
  17.   int count = 0;
  18.   while (!Q.empty()) {
  19.     pair<int, int> actual = Q.front();
  20.     int x = actual.first;
  21.     int y = actual.second;
  22.     //Above
  23.     if (y > 0 && visited[x][y-1] == 'F' && G[x][y-1] != 'X') {
  24.       if (G[x][y-1] == 't') ++count;
  25.       Q.push(make_pair(x, y-1));
  26.       visited[x][y-1] = 'T';
  27.     }
  28.     //Below
  29.     if (y < G[0].size() - 1 && visited[x][y+1] == 'F' && G[x][y+1] != 'X') {
  30.       if (G[x][y+1] == 't') ++count;
  31.       Q.push(make_pair(x, y+1));
  32.       visited[x][y+1] = 'T';
  33.     }
  34.     //Left
  35.     if (x > 0 && visited[x-1][y] == 'F' && G[x-1][y] != 'X') {
  36.       if (G[x-1][y] == 't') ++count;
  37.       Q.push(make_pair(x-1, y));
  38.       visited[x-1][y] = 'T';
  39.     }
  40.     //Right
  41.     if (x < G.size() - 1 && visited[x+1][y] == 'F' && G[x+1][y] != 'X') {
  42.       if (G[x+1][y] == 't') ++count;
  43.       Q.push(make_pair(x+1, y));
  44.       visited[x+1][y] = 'T';
  45.   }
  46.     Q.pop();
  47.   }
  48.   return count;
  49. }
  50.  
  51. int main()
  52. {
  53.   int n, m;
  54.   cin >> n >> m;
  55.   Graph G(n, Row(m));
  56.   for (int i = 0; i < n; ++i)
  57.     for (int j = 0; j < m; ++j)
  58.       cin >> G[i][j];
  59.   int x, y;
  60.   cin >> x >> y;
  61.   cout << bfs(G, x-1, y-1) << endl;
  62. }
  63.  
  64. //JosepRivaille
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement