SHARE
TWEET

Untitled

a guest Oct 10th, 2019 10 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5.  
  6. #define N 5           //tabla merete
  7. #define START_ROW 0   //kiindulo sor
  8. #define START_COL 0   //kiindulo oszlop
  9. #define DEST_ROW N-1  //cel sor
  10. #define DEST_COL N-1  //cel oszlop
  11.  
  12.  
  13. void initMaze(int maze[N][N]){
  14.     int i, j;
  15.     srand(time(0));
  16.     for(i=0; i<N; i++)
  17.         for(j=0; j<N; j++)
  18.             maze[i][j] = rand()%2;
  19.     maze[START_ROW][START_COL] = maze[DEST_ROW][DEST_COL] = 1; //kiindulas es cel legyen elerheto
  20. }
  21.  
  22.  
  23. void printMaze(int maze[N][N]){
  24.     int i, j;
  25.     printf("|");
  26.     for(j=0; j<N*4-1; j++) printf("-");
  27.     printf("|\n");
  28.     for(i=0; i<N; i++){
  29.         for(j=0; j<N; j++){
  30.             printf("| %c ", maze[i][j]==1 ? '1' : ' ');
  31.         }
  32.         printf("|\n|");
  33.         for(j=0; j<N*4-1; j++) printf("-");
  34.         printf("|\n");
  35.     }
  36.    
  37. }
  38.  
  39.  
  40. int solveMaze(int maze[N][N], int solution[N][N], int row, int col){
  41.     if(row == DEST_ROW && col == DEST_COL){ //elertuk a celt, keszen vagyunk
  42.         solution[row][col] = 1;
  43.         return 1;
  44.     }
  45.     if(row >= 0 && row < N && col >= 0 && col < N && maze[row][col] == 1 && solution[row][col] != 1){
  46.         solution[row][col] = 1;
  47.         if(solveMaze(maze, solution, row+1, col)) return 1; //megprobaljuk lefele
  48.         if(solveMaze(maze, solution, row, col-1)) return 1; //megprobaljuk balra
  49.         if(solveMaze(maze, solution, row, col+1)) return 1; //megprobaljuk jobbra        
  50.         solution[row][col] = 0; //egyik lepes sem vezetett megoldasra
  51.     }
  52.     return 0;
  53. }
  54.  
  55.  
  56. int main(){
  57.     int maze[N][N], solution[N][N], i, j;
  58.     initMaze(maze);
  59.     printf("Labirintus:\n\n");
  60.     printMaze(maze);
  61.     for(i=0; i<N; i++)
  62.         for(j=0; j<N; j++)
  63.             solution[i][j] = 0;
  64.     if(solveMaze(maze, solution, START_ROW, START_COL)){
  65.         printf("\nEgy lehetseges megoldas:\n\n");
  66.         printMaze(solution);
  67.     }
  68.     else{
  69.         printf("\nNincs megoldas.\n\n");
  70.     }
  71.     return 0;
  72. }
  73.  
  74.  
  75. /*
  76. int main(){
  77.     int maze[N][N], solution[N][N], i, j, solvable=0;
  78.     while(!solvable){
  79.         initMaze(maze);
  80.         for(i=0; i<N; i++)
  81.             for(j=0; j<N; j++)
  82.                 solution[i][j] = 0;
  83.         if(solveMaze(maze, solution, START_ROW, START_COL)) solvable = 1;
  84.     }
  85.     printf("Labirintus:\n\n");
  86.     printMaze(maze);
  87.     printf("\nEgy lehetseges megoldas:\n\n");
  88.     printMaze(solution);
  89.     return 0;
  90. }
  91. */
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top