Skip to content

Instantly share code, notes, and snippets.

@rogerioagjr
Created June 10, 2017 19:57
Show Gist options
  • Select an option

  • Save rogerioagjr/441e43070b5a208f2c5eda05e52f033f to your computer and use it in GitHub Desktop.

Select an option

Save rogerioagjr/441e43070b5a208f2c5eda05e52f033f to your computer and use it in GitHub Desktop.
Toca do Saci
#include <bits/stdc++.h>
using namespace std;
#define MAXN 1010
#define F first
#define S second
typedef pair<int,int> ii;
int n, m, tab[MAXN][MAXN], dist[MAXN][MAXN];
ii ini, fim;
int main(){
memset(dist,-1,sizeof dist);
cin >> n >> m;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin >> tab[i][j];
if(tab[i][j]==3) ini=ii(i,j);
if(tab[i][j]==2) fim=ii(i,j);
}
}
queue<ii> fila;
fila.push(ini);
dist[ini.F][ini.S]=1;
while(!fila.empty()){
ii v=fila.front();
fila.pop();
for(int i=-1;i<=1;i++){
for(int j=-1;j<=1;j++){
if((i==0 and j==0) or (i!=0 and j!=0)) continue;
if(tab[v.F+i][v.S+j]!=0 and dist[v.F+i][v.S+j]<0){
dist[v.F+i][v.S+j]=dist[v.F][v.S]+1;
fila.push(ii(v.F+i,v.S+j));
}
}
}
}
cout << dist[fim.F][fim.S] << "\n";
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment