2025-03-21 14:33:29 +01:00

113 lines
3.5 KiB
C

#include <stdio.h>
#include "leelib.h"
int assignmentMatrix[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},
};
typedef struct cell {
int x;
int y;
} cell;
int isInBound(cell *c);
int isValidCrossing(cell *c);
int main(void) {
/* read the nr of blocked stations and the stations*/
int nrOfBlock = 0;
scanf("%d", &nrOfBlock);
for (int i = 0; i < nrOfBlock; i++) {
int x, y;
readEdge(&x, &y);
assignmentMatrix[x][y] = -1;
}
/* read the starting and end station*/
cell startCell, targetCell,currentCell;
readStation(&startCell.x, &startCell.y);
readStation(&targetCell.x, &targetCell.y);
currentCell=startCell;
/* the **algorithm**, expand*/
int v = 1;
assignmentMatrix[targetCell.x][targetCell.y] = v;
while (assignmentMatrix[startCell.x][startCell.y] == 0) {
for (int i = 0; i < 13; i++) {
for (int j = 0; j < 13; j++) {
if (assignmentMatrix[i][j] == v) {
if (i!=12) {
if (assignmentMatrix[i+1][j] == 0) {
assignmentMatrix[i+1][j] = v+1;
}
}
if (i!=0) {
if (assignmentMatrix[i-1][j] == 0) {
assignmentMatrix[i-1][j] = v+1;
}
}
if (j!=12) {
if (assignmentMatrix[i][j+1] == 0) {
assignmentMatrix[i][j+1] = v+1;
}
}
if (j!=0) {
if (assignmentMatrix[i][j-1] == 0) {
assignmentMatrix[i][j-1] = v+1;
}
}
}
}
}
v+=1;
}
/* the **algorithm**, trace*/
while (currentCell.x != targetCell.x || currentCell.y != targetCell.y) {
const int neighbours[4][2]={{1,0},{0,1},{0,-1},{-1,0}};
for (int i = 0; i < 4; i++) {
cell neighbourCell;
neighbourCell.x = currentCell.x + neighbours[i][0];
neighbourCell.y = currentCell.y + neighbours[i][1];
if (isInBound(&neighbourCell) && assignmentMatrix[neighbourCell.x][neighbourCell.y] != -1 && assignmentMatrix[neighbourCell.x][neighbourCell.y] < assignmentMatrix[currentCell.x][currentCell.y] ) {
currentCell=neighbourCell;
break;
}
}
if ( isValidCrossing(&currentCell)) {
printCrossingName(currentCell.x, currentCell.y);
}
}
return 0;
}
int isInBound(cell *c) {
if (c -> x >= 0 && c -> x <= 13 && c -> y >= 0 && c -> y <= 13) {
return 1;
}
return 0;
}
int isValidCrossing(cell *c) {
if (c -> x > 0 && c -> x <12 && c -> y > 0 && c -> y < 12) {
return 1;
}
return 0;
}