Ir ao conteúdo
  • Cadastre-se

Recursividade


Eaken

Posts recomendados

Boa tarde pessoal, o código já funciona corretamente, só quero saber se vocês sabem como remover o # do labirinto quando passar pelo caminho errado.

Explicação batata na foto

Capturar.PNG.17cd7c8d29b659c171a00e579caf6127.PNG

#include <iostream>
#include <windows.h>
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#define LINHA 4
#define COLUNA 9
using namespace std;

struct Labirinto{
    bool comeco;//=false;
    bool parede;//=true;
    bool fim;
    bool blank;
    string andou = " ";
};

void imprimirLabirinto(Labirinto matriz[][COLUNA])
{
    int i,j;
    for(i=0;i<LINHA;i++)
    {
        for(j=0;j<COLUNA;j++)
        {
            if(matriz[i][j].comeco == true){
                cout << "|";
                cout << "O";
                cout << "| ";
            }else if(matriz[i][j].parede == true){
                cout << "|";
                cout << (char)254u;
                cout << "| ";
            }else if(matriz[i][j].fim == true){
                cout << "|";
                cout << "X";
                cout << "| ";
            }else if (matriz[i][j].blank == true){
                cout << "|";
                cout << matriz[i][j].andou;
                cout << "| ";
            }


        } cout << "\n\n";
    }
}

bool andar(Labirinto matriz[][COLUNA],int i,int j){
    if(i<0 || i>= LINHA || j<0 || j>COLUNA){ // FORA DO ESCOPO DA MATRIZ
        return false;

    }else if (matriz[i][j].parede == true){  // SE FOR PAREDE N PODE ANDAR
        return false;

    }else if(matriz[i][j].fim == true) {  //SE FIM=TRUE CHEGOU AO FINAL
        system("cls");
        imprimirLabirinto(matriz);
        cout << "\n Chegou ao fim\n\n";
        system("pause");
        exit(0);
    }else if(matriz[i][j].andou != " "){
        return false;
    }
    if(matriz[i][j].blank == true){
        matriz[i][j].andou = "#";
        system("pause");
        system("cls");
        imprimirLabirinto(matriz);
    }if(andar(matriz,i+1,j)){
        return true;
    }else if(andar(matriz,i,j+1)){
        return true;
    }else if(andar(matriz,i-1,j)){
        return true;
    }else if (andar(matriz,i,j-1)){
        return true;
    }

}




void preencherLabirinto(Labirinto matriz[][COLUNA]){
int i,j,k,l,aleatorio;

for(k=0;k<LINHA;k++){
    for(l=0;l<COLUNA;l++){
        matriz[k][l].blank = true; // TODAS AS MATRIZES BLANK = TRUE RESTO = FALSE
        matriz[k][l].fim = false;
        matriz[k][l].comeco = false;
        matriz[k][l].parede = false;
    }
}

matriz[0][0].comeco = true;
matriz[0][0].blank=true;    // 0,0 COMECO = TRUE PRA IMPRIMIR
matriz[0][0].parede=false;  // 0,0 BLANK = TRUE PRA ANDAR
matriz[0][0].fim=false;     // RESTO FALSE

matriz[0][8].comeco = false;
matriz[0][8].parede = false; // 0,8 FIM = TRUE PRA IMPRIMIR
matriz[0][8].blank = true;   // 0,8 BLANK = TRUE PRA ANDAR
matriz[0][8].fim = true;     // RESTO FALSE

    for(i=0;i<LINHA;i++){
        for(j=0;j<COLUNA;j++){
            aleatorio = rand()%100;
            if(i==0 && j==0){
                // MATRIZ 0,0 JA FOI INICIALIZADA
            } else if(i==0 && j==8){
                // MATRIZ 0,8 JA FOI INICIALIZADA
            }
            else if(aleatorio<10){
                matriz[i][j].comeco = false;
                matriz[i][j].blank=false;
                matriz[i][j].parede = true; //20% chance de ser parede
                matriz[i][j].fim = false;
            }else{
                matriz[i][j].comeco = false;
                matriz[i][j].parede = false;
                matriz[i][j].fim = false;
                matriz[i][j].blank = true;
            }
        }
    }
}


int main()
{
    srand (time(NULL));
    Labirinto labi[LINHA][COLUNA];
    preencherLabirinto(labi);
    imprimirLabirinto(labi);
    andar(labi,0,0);


    return 0;
}

 

Link para o comentário
Compartilhar em outros sites

Visitante
Este tópico está impedido de receber novas respostas.

Sobre o Clube do Hardware

No ar desde 1996, o Clube do Hardware é uma das maiores, mais antigas e mais respeitadas comunidades sobre tecnologia do Brasil. Leia mais

Direitos autorais

Não permitimos a cópia ou reprodução do conteúdo do nosso site, fórum, newsletters e redes sociais, mesmo citando-se a fonte. Leia mais

×
×
  • Criar novo...