maze finally
This commit is contained in:
@@ -1,40 +1,89 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#define SIZE 12
|
#define SIZE 12
|
||||||
|
|
||||||
|
void mazeTraverse(char maze_data[SIZE][SIZE], int currentRow, int currentCol, int startRow, int startCol, int visited);
|
||||||
|
void printMaze(char maze_data[SIZE][SIZE]);
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
char maze[SIZE][SIZE];
|
|
||||||
|
|
||||||
// Initialize all rows
|
// Initialize all rows
|
||||||
char *maze_data[SIZE] = {
|
char maze_data[SIZE][SIZE] = {
|
||||||
"############",
|
{'#','#','#','#','#','#','#','#','#','#','#','#'},
|
||||||
"#...#......#",
|
{'#','.','.','.','#','.','.','.','.','.','.','#'},
|
||||||
"..#.#......#",
|
{'.','.','#','.','#','.','#','#','#','#','.','#'},
|
||||||
"###.#....#.#",
|
{'#','#','#','.','#','.','.','.','.','#','.','#'},
|
||||||
"#....###.#.#",
|
{'#','.','.','.','.','#','#','#','.','#','.','.'},
|
||||||
"#####.#.#.#.",
|
{'#','#','#','#','.','#','.','#','.','#','.','#'},
|
||||||
"#..#.#.#.#.#",
|
{'#','.','.','#','.','#','.','#','.','#','.','#'},
|
||||||
"##.#.#.#.#.#",
|
{'#','#','.','#','.','#','.','#','.','#','.','#'},
|
||||||
"#........#.#",
|
{'#','.','.','.','.','.','.','.','.','#','.','#'},
|
||||||
"######.###.#",
|
{'#','#','#','#','#','#','.','#','#','#','.','#'},
|
||||||
"#......#...#",
|
{'#','.','.','.','.','.','.','#','.','.','.','#'},
|
||||||
"############"
|
{'#','#','#','#','#','#','#','#','#','#','#','#'}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Copy the maze data (more efficient than individual assignments)
|
int startRow = 2, startCol = 0;
|
||||||
for(int i = 0; i < SIZE; i++) {
|
|
||||||
for(int j = 0; j < SIZE; j++) {
|
puts("Maze");
|
||||||
maze[i][j] = maze_data[i][j];
|
printMaze(maze_data);
|
||||||
}
|
puts(""); puts("Starting Maze Treversal");
|
||||||
}
|
|
||||||
|
mazeTraverse(maze_data, startRow, startCol, startRow, startCol, 0);
|
||||||
// Print the entire maze
|
|
||||||
printf("12x12 Maze:\n");
|
|
||||||
for(int i = 0; i < SIZE; i++) {
|
|
||||||
for(int j = 0; j < SIZE; j++) {
|
|
||||||
printf("%c", maze[i][j]);
|
|
||||||
}
|
|
||||||
printf("\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void mazeTraverse(char maze_data[SIZE][SIZE], int currentRow, int currentCol, int startRow, int startCol, int visited) {
|
||||||
|
if(!(currentRow == 0 || currentCol == 0) || visited > 0) {
|
||||||
|
maze_data[currentRow][currentCol] = 'X';
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("Step %d\n", visited++);
|
||||||
|
printMaze(maze_data);
|
||||||
|
puts("");
|
||||||
|
|
||||||
|
if ((currentRow == 0 || currentRow == SIZE-1 || currentCol == 0 || currentCol == SIZE-1) &&
|
||||||
|
!(currentRow == startRow && currentCol == startCol)) {
|
||||||
|
printf("Exit found at (%d, %d)!\n", currentRow, currentCol);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (visited > 0 && currentRow == startRow && currentCol == startCol) {
|
||||||
|
puts("No exit found!");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
//RIGHT HAND RULE!
|
||||||
|
//always turn right:
|
||||||
|
int move = 0;
|
||||||
|
int direct[4][2] = {{0, 1}, {1,0}, {0, -1}, {-1, 0}}; //Right, down, left, up
|
||||||
|
int searchRow, searchCol;
|
||||||
|
|
||||||
|
for(int i = 0; i < 4 && !move; i++) {
|
||||||
|
int directOrder[4] = {0, 1, 2, 3}; //Right, down, left, up
|
||||||
|
|
||||||
|
searchRow = currentRow + direct[directOrder[i]][0];
|
||||||
|
searchCol = currentCol + direct[directOrder[i]][1];
|
||||||
|
|
||||||
|
if (searchRow >= 0 && searchRow < SIZE && searchCol >= 0 && searchCol < SIZE && maze_data[searchRow][searchCol] != '#') {
|
||||||
|
if (maze_data[currentRow][currentCol] != 'X' || (currentRow == startRow && currentCol == startCol)) {
|
||||||
|
mazeTraverse(maze_data, searchRow, searchCol, startRow, startCol, visited+1);
|
||||||
|
move = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!move && !(currentRow == 0 || currentRow == SIZE-1 ||
|
||||||
|
currentCol == 0 || currentCol == SIZE-1) &&
|
||||||
|
!(currentRow == startRow && currentCol == startCol)) {
|
||||||
|
printf("Stuck at pos(%d, %d)\n", currentRow, currentCol);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void printMaze(char maze_data[SIZE][SIZE]) {
|
||||||
|
for(size_t i = 0; i < SIZE; i++) {
|
||||||
|
for(size_t j = 0; j < SIZE; j++) {
|
||||||
|
printf("%c\n", maze_data[i][j]);
|
||||||
|
}
|
||||||
|
puts("");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
76
DeitelC/Chapter7/maze7-25_2.c
Normal file
76
DeitelC/Chapter7/maze7-25_2.c
Normal file
@@ -0,0 +1,76 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#define SIZE 12
|
||||||
|
|
||||||
|
// Function prototypes
|
||||||
|
void mazeTraverse(char maze[SIZE][SIZE], int row, int col, int direction);
|
||||||
|
void printMaze(const char maze[SIZE][SIZE]);
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
char maze[SIZE][SIZE] = {
|
||||||
|
{'#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'},
|
||||||
|
{'#', '.', '.', '.', '#', '.', '.', '.', '.', '.', '.', '#'},
|
||||||
|
{'.', '.', '#', '.', '#', '.', '#', '#', '#', '#', '.', '#'},
|
||||||
|
{'#', '#', '#', '.', '#', '.', '.', '.', '.', '#', '.', '#'},
|
||||||
|
{'#', '.', '.', '.', '.', '#', '#', '#', '.', '#', '.', '.'},
|
||||||
|
{'#', '#', '#', '#', '.', '#', '.', '#', '.', '#', '.', '#'},
|
||||||
|
{'#', '.', '.', '#', '.', '#', '.', '#', '.', '#', '.', '#'},
|
||||||
|
{'#', '#', '.', '#', '.', '#', '.', '#', '.', '#', '.', '#'},
|
||||||
|
{'#', '.', '.', '.', '.', '.', '.', '.', '.', '#', '.', '#'},
|
||||||
|
{'#', '#', '#', '#', '#', '#', '.', '#', '#', '#', '.', '#'},
|
||||||
|
{'#', '.', '.', '.', '.', '.', '.', '#', '.', '.', '.', '#'},
|
||||||
|
{'#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Initial position and direction
|
||||||
|
int startRow = 2;
|
||||||
|
int startCol = 0;
|
||||||
|
int initialDirection = 0; // 0: Right, 1: Down, 2: Left, 3: Up
|
||||||
|
|
||||||
|
printf("The Maze:\n");
|
||||||
|
printMaze(maze);
|
||||||
|
printf("\nStarting Maze Traversal...\n\n");
|
||||||
|
|
||||||
|
mazeTraverse(maze, startRow, startCol, initialDirection);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void mazeTraverse(char maze[SIZE][SIZE], int row, int col, int direction) {
|
||||||
|
// Moves: Right, Down, Left, Up
|
||||||
|
int moves[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
|
||||||
|
int nextRow, nextCol;
|
||||||
|
|
||||||
|
maze[row][col] = 'X'; // Mark the current path
|
||||||
|
printMaze(maze);
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
// Check for an exit
|
||||||
|
if ((row == 0 || row == SIZE - 1 || col == 0 || col == SIZE - 1) && !(row == 2 && col == 0)) {
|
||||||
|
printf("Exit found at (%d, %d)!\n", row, col);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Right-hand rule logic
|
||||||
|
for (int i = 0; i < 4; ++i) {
|
||||||
|
int nextDirection = (direction + i + 3) % 4; // Try right, then straight, then left
|
||||||
|
nextRow = row + moves[nextDirection][0];
|
||||||
|
nextCol = col + moves[nextDirection][1];
|
||||||
|
|
||||||
|
if (nextRow >= 0 && nextRow < SIZE && nextCol >= 0 && nextCol < SIZE && maze[nextRow][nextCol] != '#') {
|
||||||
|
mazeTraverse(maze, nextRow, nextCol, nextDirection);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("Stuck at position (%d, %d)\n", row, col);
|
||||||
|
}
|
||||||
|
|
||||||
|
void printMaze(const char maze[SIZE][SIZE]) {
|
||||||
|
for (int i = 0; i < SIZE; ++i) {
|
||||||
|
for (int j = 0; j < SIZE; ++j) {
|
||||||
|
printf("%c ", maze[i][j]);
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
}
|
@@ -9,7 +9,7 @@ int main (void)
|
|||||||
printf("++ptr %d\n", ++ptr);
|
printf("++ptr %d\n", ++ptr);
|
||||||
|
|
||||||
void *vptr = &x;
|
void *vptr = &x;
|
||||||
printf("void ptr %d\n", vptr);
|
printf("void ptr %d\n", *vptr);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
127
DeitelC/Chapter7/test44.c
Normal file
127
DeitelC/Chapter7/test44.c
Normal file
@@ -0,0 +1,127 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#define SIZE 12
|
||||||
|
|
||||||
|
// Function prototypes
|
||||||
|
void mazeTraverse(char maze[SIZE][SIZE], int currentRow, int currentCol, int startRow, int startCol, int visited);
|
||||||
|
void printMaze(char maze[SIZE][SIZE]);
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
// Initialize the maze
|
||||||
|
char maze[SIZE][SIZE] = {
|
||||||
|
{'#','#','#','#','#','#','#','#','#','#','#','#'},
|
||||||
|
{'#','.','.','.','#','.','.','.','.','.','.','#'},
|
||||||
|
{'.','.','#','.','#','.','#','#','#','#','.','#'},
|
||||||
|
{'#','#','#','.','#','.','.','.','.','#','.','#'},
|
||||||
|
{'#','.','.','.','.','#','#','#','.','#','.','.'},
|
||||||
|
{'#','#','#','#','.','#','.','#','.','#','.','#'},
|
||||||
|
{'#','.','.','#','.','#','.','#','.','#','.','#'},
|
||||||
|
{'#','#','.','#','.','#','.','#','.','#','.','#'},
|
||||||
|
{'#','.','.','.','.','.','.','.','.','#','.','#'},
|
||||||
|
{'#','#','#','#','#','#','.','#','#','#','.','#'},
|
||||||
|
{'#','.','.','.','.','.','.','#','.','.','.','#'},
|
||||||
|
{'#','#','#','#','#','#','#','#','#','#','#','#'}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Starting position (from the maze - row 2, column 0)
|
||||||
|
int startRow = 2, startCol = 0;
|
||||||
|
|
||||||
|
printf("Initial Maze:\n");
|
||||||
|
printMaze(maze);
|
||||||
|
printf("\nStarting maze traversal...\n\n");
|
||||||
|
|
||||||
|
// Start maze traversal
|
||||||
|
mazeTraverse(maze, startRow, startCol, startRow, startCol, 0);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Recursive function to traverse the maze using right-hand rule
|
||||||
|
void mazeTraverse(char maze[SIZE][SIZE], int currentRow, int currentCol,
|
||||||
|
int startRow, int startCol, int visited) {
|
||||||
|
|
||||||
|
// Mark current position with 'X' if it's not the start position
|
||||||
|
if (!(currentRow == startRow && currentCol == startCol) || visited > 0) {
|
||||||
|
maze[currentRow][currentCol] = 'X';
|
||||||
|
}
|
||||||
|
|
||||||
|
// Display the maze after each move
|
||||||
|
printf("Step %d:\n", visited + 1);
|
||||||
|
printMaze(maze);
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
// Check if we've reached an exit (on the border but not at start)
|
||||||
|
if ((currentRow == 0 || currentRow == SIZE-1 || currentCol == 0 || currentCol == SIZE-1) &&
|
||||||
|
!(currentRow == startRow && currentCol == startCol)) {
|
||||||
|
printf("Exit found at (%d, %d)!\n", currentRow, currentCol);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if we've returned to start (no exit)
|
||||||
|
if (visited > 0 && currentRow == startRow && currentCol == startCol) {
|
||||||
|
printf("No exit found - returned to starting position.\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Right-hand rule: always keep right hand on the wall
|
||||||
|
// We'll check directions in this order: right, down, left, up
|
||||||
|
// This simulates always trying to turn right first
|
||||||
|
|
||||||
|
// Try moving in each direction according to right-hand rule
|
||||||
|
int moved = 0;
|
||||||
|
|
||||||
|
// Array of possible moves: right, down, left, up
|
||||||
|
int directions[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
|
||||||
|
|
||||||
|
// Find which direction we're currently facing (simplified approach)
|
||||||
|
// For simplicity, we'll check all directions and prioritize right turns
|
||||||
|
|
||||||
|
// Check right (relative to current direction - simplified)
|
||||||
|
int newRow, newCol;
|
||||||
|
|
||||||
|
// Try to move while following right-hand wall
|
||||||
|
// The algorithm: always try to turn right, if not possible go straight,
|
||||||
|
// if not possible turn left, if not possible turn around
|
||||||
|
|
||||||
|
// For this implementation, we'll use a simplified approach:
|
||||||
|
// Check all four directions and move to the first valid one that follows right-hand rule
|
||||||
|
|
||||||
|
for (int i = 0; i < 4 && !moved; i++) {
|
||||||
|
// Check directions in a specific order to simulate right-hand rule
|
||||||
|
int dirOrder[4] = {0, 1, 2, 3}; // right, down, left, up
|
||||||
|
|
||||||
|
newRow = currentRow + directions[dirOrder[i]][0];
|
||||||
|
newCol = currentCol + directions[dirOrder[i]][1];
|
||||||
|
|
||||||
|
// Check if the new position is valid and not a wall
|
||||||
|
if (newRow >= 0 && newRow < SIZE && newCol >= 0 && newCol < SIZE &&
|
||||||
|
maze[newRow][newCol] != '#') {
|
||||||
|
|
||||||
|
// Additional check to avoid immediate backtracking
|
||||||
|
if (maze[newRow][newCol] != 'X' ||
|
||||||
|
(newRow == startRow && newCol == startCol)) {
|
||||||
|
|
||||||
|
// Recursive call to continue traversal
|
||||||
|
mazeTraverse(maze, newRow, newCol, startRow, startCol, visited + 1);
|
||||||
|
moved = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If no move was possible and we're not at exit/start, we're stuck
|
||||||
|
if (!moved && !(currentRow == 0 || currentRow == SIZE-1 ||
|
||||||
|
currentCol == 0 || currentCol == SIZE-1) &&
|
||||||
|
!(currentRow == startRow && currentCol == startCol)) {
|
||||||
|
printf("Stuck at position (%d, %d)\n", currentRow, currentCol);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Function to print the maze
|
||||||
|
void printMaze(char maze[SIZE][SIZE]) {
|
||||||
|
for (int i = 0; i < SIZE; i++) {
|
||||||
|
for (int j = 0; j < SIZE; j++) {
|
||||||
|
printf("%c ", maze[i][j]);
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
}
|
@@ -9,13 +9,11 @@ int main(void)
|
|||||||
|
|
||||||
for(size_t j = 0; j < arr; j++) {
|
for(size_t j = 0; j < arr; j++) {
|
||||||
printf("For Element %d enter num!\n", j);
|
printf("For Element %d enter num!\n", j);
|
||||||
int num = 0;
|
scanf("%d", nums + j);
|
||||||
scanf("%d", &num);
|
|
||||||
nums[j] = num;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for(size_t j = 0; j < arr; j++) {
|
for(size_t j = 0; j < arr; j++) {
|
||||||
printf("Element %d = %d\n", j, nums[j]);
|
printf("Element %d = %d\n", j, *(nums + j));
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
10
w3resource/pointers/test.c
Normal file
10
w3resource/pointers/test.c
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
int x = 5;
|
||||||
|
int *pointX = &x;
|
||||||
|
printf("*pointX = %d and (*pointX+1) = %d and *(pointX+1) = %d\n", *pointX, (*pointX+1), *(pointX+1));
|
||||||
|
printf("*(&pointX) = %d &(*pointX) = %d *&pointX = %d &*pointX = %d\n", *(&pointX), &(*pointX), *&pointX, &*pointX);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
Reference in New Issue
Block a user