first
This commit is contained in:
parent
711c7beb56
commit
e5853fa96c
@ -13,6 +13,7 @@ void hacker_script(char *string, int len);
|
||||
struct pizza {
|
||||
char name[size];
|
||||
float price;
|
||||
|
||||
};
|
||||
|
||||
int main() {
|
||||
|
16
sk1a/Makefile
Normal file
16
sk1a/Makefile
Normal file
@ -0,0 +1,16 @@
|
||||
CC = gcc
|
||||
CFLAGS = -g -Wall -Werror
|
||||
|
||||
all: program
|
||||
|
||||
program: maze.o main.o
|
||||
$(CC) $(CFLAGS) maze.o main.o -o program
|
||||
|
||||
maze.o: maze.c
|
||||
$(CC) $(CFLAGS) -c maze.c -o maze.o
|
||||
|
||||
main.o: main.c
|
||||
$(CC) $(CFLAGS) -c main.c -o main.o
|
||||
|
||||
clean:
|
||||
rm -rf program *.o *.exe *.exe.stackdump
|
39
sk1a/main.c
Normal file
39
sk1a/main.c
Normal file
@ -0,0 +1,39 @@
|
||||
#define SZ 5
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "maze.h"
|
||||
|
||||
void print_solution(char* matrix,int sz){
|
||||
for (int i = 0; i < sz * sz; i++){
|
||||
printf("%c ",matrix[i]);
|
||||
if (i % sz == (sz-1)){
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main(){
|
||||
char tmaze[SZ+1][SZ+1]={
|
||||
"*xxx ",
|
||||
" x",
|
||||
" xx ",
|
||||
" x ",
|
||||
" x ",
|
||||
};
|
||||
char maze[SZ*SZ];
|
||||
memset(maze,' ',SZ*SZ);
|
||||
for (int i= 0; i < SZ; i++){
|
||||
for (int j= 0; j < SZ; j++){
|
||||
if (tmaze[i][j] == 'x'){
|
||||
maze[i*SZ+j] = 'x';
|
||||
}
|
||||
}
|
||||
}
|
||||
int r = solve_maze(maze,SZ);
|
||||
if (!r){
|
||||
printf("Nenasiel som riesenie\n");
|
||||
}
|
||||
print_solution(maze,SZ);
|
||||
|
||||
return 0;
|
||||
}
|
126
sk1a/maze.c
Normal file
126
sk1a/maze.c
Normal file
@ -0,0 +1,126 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "maze.h"
|
||||
#include <assert.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define CHECKED_BIT 16
|
||||
#define DIRECTION_BITS 15
|
||||
|
||||
enum direction {
|
||||
NORTH = 8,
|
||||
EAST = 4,
|
||||
SOUTH = 2,
|
||||
WEST = 1,
|
||||
NONE = 0
|
||||
};
|
||||
|
||||
int solve_maze(char* maze, int size) {
|
||||
if (maze[0] == 'x') {
|
||||
return 0;
|
||||
}
|
||||
|
||||
char maze2d[size][size];
|
||||
for (int y = 0; y < size; y++) {
|
||||
for (int x = 0; x < size; x++) {
|
||||
maze2d[y][x] = maze[size * y + x] == 'x' ? 'x' : 0;
|
||||
}
|
||||
}
|
||||
|
||||
int posY = 0, posX = 0;
|
||||
|
||||
while (1) {
|
||||
enum direction next_step;
|
||||
|
||||
if (posX + 1 != size && maze2d[posY][posX + 1] != 'x' && !(maze2d[posY][posX + 1] & CHECKED_BIT)) {
|
||||
next_step = EAST;
|
||||
} else if (posY + 1 != size && maze2d[posY + 1][posX] != 'x' && !(maze2d[posY + 1][posX] & CHECKED_BIT)) {
|
||||
next_step = SOUTH;
|
||||
} else if (posX - 1 >= 0 && maze2d[posY][posX - 1] != 'x' && !(maze2d[posY][posX - 1] & CHECKED_BIT)) {
|
||||
next_step = WEST;
|
||||
} else if (posY - 1 >= 0 && maze2d[posY - 1][posX] != 'x' && !(maze2d[posY - 1][posX] & CHECKED_BIT)) {
|
||||
next_step = NORTH;
|
||||
} else {
|
||||
next_step = NONE;
|
||||
}
|
||||
|
||||
if (next_step != NONE) {
|
||||
maze2d[posY][posX] |= CHECKED_BIT;
|
||||
switch (next_step) {
|
||||
case EAST:
|
||||
posX += 1;
|
||||
maze2d[posY][posX] |= WEST;
|
||||
break;
|
||||
case SOUTH:
|
||||
posY += 1;
|
||||
maze2d[posY][posX] |= NORTH;
|
||||
break;
|
||||
case WEST:
|
||||
posX -= 1;
|
||||
maze2d[posY][posX] |= EAST;
|
||||
break;
|
||||
case NORTH:
|
||||
posY -= 1;
|
||||
maze2d[posY][posX] |= SOUTH;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
char came_from = maze2d[posY][posX] & DIRECTION_BITS;
|
||||
maze2d[posY][posX] = CHECKED_BIT;
|
||||
switch (came_from) {
|
||||
case EAST:
|
||||
posX += 1;
|
||||
break;
|
||||
case SOUTH:
|
||||
posY += 1;
|
||||
break;
|
||||
case WEST:
|
||||
posX -= 1;
|
||||
break;
|
||||
case NORTH:
|
||||
posY -= 1;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (posX == 0 && posY == 0) {
|
||||
return 0;
|
||||
} else if (posX == size - 1 && posY == size - 1) {
|
||||
maze2d[0][0] = '*';
|
||||
|
||||
//cleanup data bits
|
||||
for (int i = 0; i < size; i++) {
|
||||
for (int j = 0; j < size; j++) {
|
||||
if (maze2d[i][j] == CHECKED_BIT || maze2d[i][j] == 0) {
|
||||
maze2d[i][j] = ' ';
|
||||
} else if (maze2d[i][j] != 'x') {
|
||||
maze2d[i][j] = '*';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//cleanup redundant routes
|
||||
for (int i = size - 2; i >= 0; i--) {
|
||||
for (int j = size - 2; j >= 0; j--) {
|
||||
if (maze2d[i][j] == '*' && maze2d[i + 1][j] == '*' && maze2d[i + 1][j + 1] == '*' && maze2d[i][j + 1] == '*') {
|
||||
maze2d[i + 1][j + 1] = ' ';
|
||||
maze2d[i][j + 1] = ' ';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//serialize
|
||||
for (int i = 0; i < size; i++) {
|
||||
for (int j = 0; j < size; j++) {
|
||||
maze[i * size + j] = maze2d[i][j];
|
||||
}
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
22
sk1a/maze.h
Normal file
22
sk1a/maze.h
Normal file
@ -0,0 +1,22 @@
|
||||
#ifndef _MAZEH
|
||||
#define _MAZEH
|
||||
|
||||
/**
|
||||
* Funkcia by mala zobrať vstupnú mriežku a
|
||||
* vyznačiť na nej cestu z ľavého horného rohu do pravého dolného rohu.
|
||||
* Mriežka je uložená do jednorozmerného poľa, pričom najprv ide prvý riadok,
|
||||
* za ním druhý a tak ďalej.
|
||||
*
|
||||
* Na mriežke sa nachádzajú znaky:
|
||||
* ' ' - voľné miesto
|
||||
* 'x' - stena. Stena nesmie byť prepísaná.
|
||||
* '*' - poloha potkana. Na začiatku je na 0,0.
|
||||
*
|
||||
* @param maze Štvorcová mriežka rozmeru size x size.
|
||||
* @param size Rozmer mriežky
|
||||
* @return 1 ak existuje riešenie, 0 inak.
|
||||
*/
|
||||
int solve_maze(char* maze,int size);
|
||||
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue
Block a user