Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <queue>
- using namespace std;
- typedef vector < vector <char> > Graf;
- typedef vector < vector <int> > Graf1;
- typedef pair<int,int> iipair;
- void prova( int i, int j, Graf& mapa, Graf1& dist, queue<iipair>& q, int d) {
- if (i>=0 and i < mapa.size() and j >= 0 and j < mapa[0].size()) {
- if (mapa[i][j] == '.' or mapa[i][j] == 't') {
- q.push(iipair(i,j));
- dist[i][j] = d+1;
- }
- }
- }
- int main() {
- int n, m, contador;
- contador = 0;
- cin >> n >> m;
- Graf g (n,vector<char> (m));
- Graf1 dist (n,vector<int> (m, -1));
- for (int i = 0; i < n; ++i) {
- for (int j = 0; j < m; ++j) cin >> g[i][j];
- }
- int x, y;
- cin >> x >> y;
- --x; --y;
- // bool trobat = false;
- queue <iipair> q;
- q.push(iipair(x,y));
- int d = -1;
- dist[x][y] = 0;
- while (/*!trobat and*/ q.size()) {
- iipair p = q.front();
- q.pop();
- if (g[p.first][p.second] == 't') {
- //trobat = true;
- g[p.first][p.second] = '.';
- d = dist[p.first][p.second];
- contador++;
- // cout << "minimum distance: " << d << endl;
- }if (g[p.first][p.second] == '.'){
- g[p.first][p.second] = 'v'; //visitat
- prova(p.first-1,p.second,g,dist,q,dist[p.first][p.second]);
- prova(p.first+1,p.second,g,dist,q,dist[p.first][p.second]);
- prova(p.first,p.second+1,g,dist,q,dist[p.first][p.second]);
- prova(p.first,p.second-1,g,dist,q,dist[p.first][p.second]);
- }
- }
- //if (not trobat) cout << "no treasure can be reached" << endl;
- cout << contador << endl;
- }
Add Comment
Please, Sign In to add comment