145 lines
4.0 KiB
C
145 lines
4.0 KiB
C
#include "leelib.c"
|
|
#include <stdio.h>
|
|
|
|
int main(){
|
|
int maze[13][13] = {{-1, -1, -1, -1, 0, -1, 0, -1, 0, -1, -1, -1, -1},
|
|
{-1, -1, -1, -1, 0, -1, 0, -1, 0, -1, -1, -1, -1},
|
|
{-1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1},
|
|
{-1, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, -1},
|
|
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
|
|
{-1, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, -1},
|
|
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
|
|
{-1, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, -1},
|
|
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
|
|
{-1, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, -1},
|
|
{-1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1},
|
|
{-1, -1, -1, -1, 0, -1, 0, -1, 0, -1, -1, -1, -1},
|
|
{-1, -1, -1, -1, 0, -1, 0, -1, 0, -1, -1, -1, -1},
|
|
};
|
|
|
|
|
|
int n_blocks;
|
|
printf("Enter number of blockades:\n");
|
|
scanf ("%d", &n_blocks);
|
|
//TO DO: INITIALISE EDGES
|
|
for(int k = 0; k<n_blocks ; k++)
|
|
{
|
|
int block_i, block_j;
|
|
int * ptr_block_i;
|
|
int * ptr_block_j;
|
|
ptr_block_i = &block_i;
|
|
ptr_block_j = &block_j;
|
|
|
|
printf("Enter block postion %d as <x> <y> <direction ('s'or 'e')>:\n", k + 1);
|
|
readEdge (ptr_block_i, ptr_block_j);
|
|
maze[block_i][block_j] = -1;
|
|
}
|
|
|
|
int start_station_i, start_station_j;
|
|
int * ptr_start_station_i;
|
|
int * ptr_start_station_j;
|
|
ptr_start_station_i = &start_station_i;
|
|
ptr_start_station_j = &start_station_j;
|
|
|
|
printf("Input start station number");
|
|
readStation(ptr_start_station_i, ptr_start_station_j);
|
|
|
|
int end_station_i, end_station_j;
|
|
int * ptr_end_station_i;
|
|
int * ptr_end_station_j;
|
|
ptr_end_station_i = &end_station_i;
|
|
ptr_end_station_j = &end_station_j;
|
|
|
|
printf("Input end station number");
|
|
readStation(ptr_end_station_i, ptr_end_station_j);
|
|
|
|
|
|
int v = 1;
|
|
maze[end_station_i][end_station_j] = v;
|
|
|
|
//expand phase
|
|
for(v; v<20 ; v++)
|
|
{
|
|
for(int i=0; i < 13; i++ )
|
|
{
|
|
for(int j = 0; j < 13; j++)
|
|
{
|
|
if(maze[i][j] == v)
|
|
{
|
|
// Check south
|
|
if (i + 1 < 13 && maze[i + 1][j] == 0)
|
|
{
|
|
maze[i+1][j] = v+1;
|
|
}
|
|
// Check north
|
|
if (i - 1 >= 0 && maze[i - 1][j] == 0)
|
|
{
|
|
maze[i-1][j] = v+1;
|
|
}
|
|
// Check east
|
|
if (j + 1 < 13 && maze[i][j+1] == 0)
|
|
{
|
|
maze[i][j+1] = v+1;
|
|
|
|
}
|
|
// Check west
|
|
if (j - 1 >= 0 && maze[i][j-1] == 0)
|
|
{
|
|
maze[i][j-1] = v+1;
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if(maze[start_station_i][start_station_j] == 0)
|
|
{
|
|
printf("No route could be found :( \n");
|
|
printMatrix (maze);
|
|
return 0;
|
|
}
|
|
|
|
|
|
printf("\n Path:\n");
|
|
int i = start_station_i;
|
|
int j = start_station_j;
|
|
for(int k=0; k <= 20; k++)
|
|
{
|
|
printCrossingName(i, j);
|
|
|
|
if(i == end_station_i && j == end_station_j)
|
|
{
|
|
break;
|
|
}
|
|
// Check south
|
|
if (i + 1 < 13 && maze[i+1][j] > 0 && maze[i + 1][j] < maze[i][j])
|
|
{
|
|
i++;
|
|
continue;
|
|
}
|
|
// Check north
|
|
if (i - 1 >= 0 && maze[i-1][j] > 0 && maze[i - 1][j] < maze[i][j])
|
|
{
|
|
i--;
|
|
continue;
|
|
}
|
|
// Check east
|
|
if (j + 1 < 13 && maze[i][j+1] > 0 && maze[i][j+1] < maze[i][j])
|
|
{
|
|
j++;
|
|
continue;
|
|
}
|
|
// Check west
|
|
if (j - 1 >= 0 && maze[i][j-1] && maze[i][j-1] < maze[i][j])
|
|
{
|
|
j--;
|
|
continue;
|
|
}
|
|
}
|
|
|
|
printf("\n Maze:\n");
|
|
printMatrix (maze);
|
|
return 0;
|
|
}
|