NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
void printMaze(int);
void initializeMaze(int);
void solveMaze(int,int,int);
void delay(int);
/* i will explain this functions in their inside.*/
char matrix[100][100];
char matrixPrint[100][100];
int visited[100][100];
/* matrix is a matrix thati read from text file, matrixPrint is a matrix that i will print, visited is a matrix which i keep visited nodes with 1 not visiteds 0.*/
void main(){
int N=0,i,j;
FILE *fi;
char buffer[100],*token;
fi = fopen("data.txt","r");
while(!feof(fi)){
i = 0;
fgets(buffer,100,fi);
token = strtok(buffer,",");
while(token != NULL){
matrix[N][i] = token[0];
token = strtok(NULL,",");
i++;
}
N++;
}
fclose(fi);
/* Upside i read maze from text and write to matrix.*/
initializeMaze(N);
delay(350);
system("cls");
solveMaze(N,1,1);
}
void printMaze(int N){
int i,j;
for(i=0;i<N;i++){
for(j=0;j<N;j++){
printf("%c",matrixPrint[i][j]);
}
printf("n");
}
}
/* Upside i am printing the maze.*/
void initializeMaze(int N){
int i,j;
for(i=1;i<N;i++){
for(j=1;j<N;j++){
if(matrix[i][j] == 'B'){
matrixPrint[i-1][j-1] = '+';
matrixPrint[i-1][j+1] = '+';
matrixPrint[i+1][j-1] = '+';
matrixPrint[i+1][j+1] = '+';
}
else if(matrix[i][j] == '1'){
matrixPrint[i][j] = ' ';
}
}
}
for(i=0;i<N;i++){
for(j=0;j<N;j++){
if(matrix[i][j] =='0')
if(matrixPrint[i][j+1] == '+') matrixPrint[i][j] = '-';
else if(matrixPrint[i+1][j] == '+') matrixPrint[i][j] = '|';
}
}
for(i=0;i<N;i++){
for(j=0;j<N;j++) visited[i][j] = 0;
}
}
/* Upside i put + the corners of the B's. And when i see 1 i put ' '(space) and when i see 2 + with position up and down near of 0 i put | instead of 0 if opposite
i put - instead of 0.*/
void solveMaze(int N,int x,int y){
int i,j;
char tempMatrix[N][N];
if((x==N-2)&&(y==N-2)){
matrixPrint[x][y] = 'X';
printMaze(N);
printf("solved.");
exit(0);
}
/* Firstly if i reach the end of the maze i put X to matrixPrint[x][y] and finish the function.*/
else{
for(i=0;i<N;i++){
for(j=0;j<N;j++){
tempMatrix[i][j] = matrixPrint[i][j];
}
}
if(matrix[x][y+1] != 'B' && matrix[x-1][y] != 'B'){
if(x==1&&y==1) matrixPrint[x][y] = '*';
else matrixPrint[x][y] = 'I';
}
visited[x][y] = 1;
printMaze(N);
delay(350);
system("cls");
if((matrix[x][y+1] != '0')&&(visited[x][y+1] == 0)) solveMaze(N,x,y+1);
if((matrix[x+1][y] != '0')&&(visited[x+1][y] == 0)) solveMaze(N,x+1,y);
if((matrix[x-1][y] != '0')&&(visited[x-1][y] == 0)) solveMaze(N,x-1,y);
if((matrix[x][y-1] != '0')&&(visited[x][y-1] == 0)) solveMaze(N,x,y-1);
for(i=0;i<N;i++)
for(j=0;j<N;j++){
matrixPrint[i][j] = tempMatrix[i][j];
}
return;
}
/* If i didn't reach yet ; Firstly we are saving our old matrixPrint to tempMatrix because we will rewrite matrixPrint when we are solving maze. If we can't reach at the first
step we need to return our starting point. With tempMatrix we are saving our starting place. After these steps we are marking the visited[x][y] as visited and printing maze.
Then we are checking 4 side of x,y coordinats as recursive. When we find our maze at if condition we print maze last time and exit from the function.*/
}
void delay(int milliseconds){
long pause;
clock_t now,then;
pause = milliseconds*(CLOCKS_PER_SEC/1000);
now = then = clock();
while( (now-then) < pause )
now = clock();
}
/* This function just an extra. I used sleep first but it can only take seconds inside the function. I need milliseconds for my project. So i write this code. With this
function i can delay miliseconds.*/
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.