Advertisement
Guest User

TocadoSaci

a guest
Sep 21st, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.54 KB | None | 0 0
  1. #include<iostream>
  2. #include<vector>
  3. #include<queue>
  4.  
  5. #define vis -1
  6.  
  7. using namespace std;
  8.  
  9. int toca[1005][1005];
  10. vector< pair<int, int> > mov;
  11. int n, m;
  12.  
  13. void bfs(pair<int, int> ponto){
  14.    
  15.     int res = 0;
  16.     queue<pair<int, int>> fila;
  17.     pair<int, int> atual, filho;    
  18.  
  19.     fila.push(ponto);
  20.  
  21.     while(!fila.empty()){
  22.  
  23.         atual = fila.front();
  24.         fila.pop();
  25.  
  26.         res++;
  27.  
  28.         if(toca[atual.first][atual.second] == 2) break;
  29.  
  30.         for(int i = 0; i < mov.size(); i++){
  31.             filho = make_pair(atual.first + mov[i].first, atual.second + mov[i].second);
  32.  
  33.             if(filho.first >= 0 && filho.first < n && filho.second >= 0 && filho.second < m){
  34.                 if(toca[filho.first][filho.second] == 1 || toca[filho.first][filho.second] == 2){
  35.                    
  36.                     fila.push(filho);
  37.  
  38.                     if(toca[filho.first][filho.second] == 1)
  39.                         toca[filho.first][filho.second] = vis;
  40.  
  41.                 }
  42.             }
  43.  
  44.         }            
  45.  
  46.     }
  47.    
  48.     printf("%d\n", res);
  49. }
  50.  
  51.  
  52. int main(){
  53.  
  54.     int aux;
  55.     pair<int, int> ponto;  
  56.  
  57.     mov.push_back(make_pair(-1, 0));
  58.     mov.push_back(make_pair(0, 1));
  59.     mov.push_back(make_pair(1, 0));
  60.     mov.push_back(make_pair(0, -1));  
  61.    
  62.     scanf("%d %d", &n, &m);    
  63.  
  64.     for(int i = 0; i < n; i++){
  65.         for(int j = 0; j < m; j++){
  66.            
  67.             scanf("%d", &aux);
  68.             toca[n][m] = aux;            
  69.  
  70.             if(aux == 3)
  71.                 ponto = make_pair(i, j);
  72.  
  73.         }
  74.     }    
  75.  
  76.     bfs(ponto);
  77.  
  78.     return 0;
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement