Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<vector>
- #include<queue>
- #define vis -1
- using namespace std;
- int toca[1005][1005];
- vector< pair<int, int> > mov;
- int n, m;
- void bfs(pair<int, int> ponto){
- int res = 0;
- queue<pair<int, int>> fila;
- pair<int, int> atual, filho;
- fila.push(ponto);
- while(!fila.empty()){
- atual = fila.front();
- fila.pop();
- res++;
- if(toca[atual.first][atual.second] == 2) break;
- for(int i = 0; i < mov.size(); i++){
- filho = make_pair(atual.first + mov[i].first, atual.second + mov[i].second);
- if(filho.first >= 0 && filho.first < n && filho.second >= 0 && filho.second < m){
- if(toca[filho.first][filho.second] == 1 || toca[filho.first][filho.second] == 2){
- fila.push(filho);
- if(toca[filho.first][filho.second] == 1)
- toca[filho.first][filho.second] = vis;
- }
- }
- }
- }
- printf("%d\n", res);
- }
- int main(){
- int aux;
- pair<int, int> ponto;
- mov.push_back(make_pair(-1, 0));
- mov.push_back(make_pair(0, 1));
- mov.push_back(make_pair(1, 0));
- mov.push_back(make_pair(0, -1));
- scanf("%d %d", &n, &m);
- for(int i = 0; i < n; i++){
- for(int j = 0; j < m; j++){
- scanf("%d", &aux);
- toca[n][m] = aux;
- if(aux == 3)
- ponto = make_pair(i, j);
- }
- }
- bfs(ponto);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement