Skip to content

Instantly share code, notes, and snippets.

@AdrianoPereira
Last active September 18, 2017 12:21
Show Gist options
  • Save AdrianoPereira/01386f5eb17f069f4307086af65aafe3 to your computer and use it in GitHub Desktop.
Save AdrianoPereira/01386f5eb17f069f4307086af65aafe3 to your computer and use it in GitHub Desktop.
Percurso no Labirinto com Leitura de Arquivo
#include <bits/stdc++.h>
using namespace std;
#define MAX 100
int L;
int C;
/*Não foi necessário
int cx[] = {1, -1, 0, 0};
int cy[] = {0, 0, 1, -1};
*/
int matriz[MAX][MAX];
bool flag;
bool visitados[MAX][MAX];
/*Nao foi necessario
bool areaDisponivel(int x, int y){
if(x>=L || x<0 || y>=C || y<0)
return false;
if(matriz[x][y] == 1)
return false;
return true;
}
*/
void percorrer(int x, int y){
if(x>=0 && y>=0 && x<L && y<C && !flag){
visitados[x][y] = true;
cout << "Linha: " << x+1 << " - Coluna: " << y+1 << endl;
if(x==0 && y==0){
flag = true;
cout << "Fim do Labirinto!" << endl;
return;
}else{
if(x+1<L && matriz[x+1][y] == 0 && !visitados[x+1][y]){
percorrer(x+1, y);
}
if(x-1>=0 && matriz[x-1][y] == 0 && !visitados[x-1][y]){
percorrer(x-1, y);
}
if(y+1<C && matriz[x][y+1] == 0 && !visitados[x][y+1]){
percorrer(x, y+1);
}
if(y-1>=0 && matriz[x][y-1] == 0 && !visitados[x][y-1]){
percorrer(x, y-1);
}
}
}
}
int main(){
flag = false;
freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
cout << "Iniciando Percursso\n";
cin>>L>>C;
for(int x=0; x<L; x++){
for(int y=0; y<C; y++){
cin>>matriz[x][y];
}
}
memset(visitados, false, sizeof visitados);
percorrer(L-1, C-1);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment