with commented old version
This commit is contained in:
parent
52d6d72dfb
commit
5648b27810
71
DS-B/routeplanner/leelib.c
Normal file
71
DS-B/routeplanner/leelib.c
Normal file
@ -0,0 +1,71 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
void readEdge (int *i, int *j)
|
||||||
|
/* Scans input to read a blocked edge.
|
||||||
|
Returns the indices i and j in m[13][13] of an edge.
|
||||||
|
Call this function with pointers to the variables
|
||||||
|
that should receive the values, e.g.
|
||||||
|
readEdge (&x, &y);
|
||||||
|
*/
|
||||||
|
{
|
||||||
|
int cx, cy;
|
||||||
|
char d;
|
||||||
|
scanf ("%d %d %c", &cy, &cx, &d);
|
||||||
|
if (d == 's') {
|
||||||
|
*i = 2 + cy * 2 + 1;
|
||||||
|
*j = 2 + cx * 2;
|
||||||
|
}
|
||||||
|
else if (d == 'e') {
|
||||||
|
*i = 2 + cy * 2;
|
||||||
|
*j = 2 + cx * 2 + 1;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
fprintf (stderr, "error on direction: %c\n", d);
|
||||||
|
exit (1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void readStation (int *i, int *j)
|
||||||
|
/* Scans input to read a station.
|
||||||
|
Returns the indices i and j in m[13][13] of a station.
|
||||||
|
*/
|
||||||
|
{
|
||||||
|
int s;
|
||||||
|
scanf ("%d", &s);
|
||||||
|
switch (s) {
|
||||||
|
case 1: *i = 12, *j = 4; break;
|
||||||
|
case 2: *i = 12, *j = 6; break;
|
||||||
|
case 3: *i = 12, *j = 8; break;
|
||||||
|
case 4: *i = 8, *j = 12; break;
|
||||||
|
case 5: *i = 6, *j = 12; break;
|
||||||
|
case 6: *i = 4, *j = 12; break;
|
||||||
|
case 7: *i = 0, *j = 8; break;
|
||||||
|
case 8: *i = 0, *j = 6; break;
|
||||||
|
case 9: *i = 0, *j = 4; break;
|
||||||
|
case 10: *i = 4, *j = 0; break;
|
||||||
|
case 11: *i = 6, *j = 0; break;
|
||||||
|
case 12: *i = 8, *j = 0; break;
|
||||||
|
default: fprintf (stderr, "Illegal station\n"); exit (-1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void printCrossingName (int i, int j)
|
||||||
|
/* Print the name of the crossing with indices i and j in m[13][13]/ */
|
||||||
|
{
|
||||||
|
if ((i-2)%2 == 0 && (j-2)%2 == 0)
|
||||||
|
printf ("c%d%d ", (i-2)/2, (j-2)/2);
|
||||||
|
}
|
||||||
|
|
||||||
|
void printMatrix (int m[][13])
|
||||||
|
/* Print the elements of the matrix m[13][13]. */
|
||||||
|
{
|
||||||
|
int i, j;
|
||||||
|
|
||||||
|
for (i = 0; i < 13; i++) {
|
||||||
|
for (j = 0; j < 13; j++) {
|
||||||
|
printf ("%2d ", m[i][j]);
|
||||||
|
}
|
||||||
|
printf ("\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
19
DS-B/routeplanner/leelib.h
Normal file
19
DS-B/routeplanner/leelib.h
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
void readEdge (int *i, int *j);
|
||||||
|
/* Scans input to read a blocked edge.
|
||||||
|
Returns the indices i and j in m[13][13] of an edge.
|
||||||
|
Call this function with pointers to the variables
|
||||||
|
that should receive the values, e.g.
|
||||||
|
readEdge (&x, &y);
|
||||||
|
*/
|
||||||
|
|
||||||
|
void readStation (int *i, int *j);
|
||||||
|
/* Scans input to read a station.
|
||||||
|
Returns the indices i and j in m[13][13] of a station.
|
||||||
|
*/
|
||||||
|
|
||||||
|
void printCrossingName (int i, int j);
|
||||||
|
/* Print the name of the crossing with indices i and j in m[13][13]/ */
|
||||||
|
|
||||||
|
void printMatrix (int m[][13]);
|
||||||
|
/* Print the elements of the matrix m[13][13]. */
|
||||||
|
|
||||||
138
DS-B/routeplanner/main.c
Normal file
138
DS-B/routeplanner/main.c
Normal file
@ -0,0 +1,138 @@
|
|||||||
|
#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;
|
||||||
|
}
|
||||||
|
//printMatrix(assignmentMatrix);
|
||||||
|
|
||||||
|
/* the **algorithm**, trace*/
|
||||||
|
|
||||||
|
while (currentCell.x != targetCell.x || currentCell.y != targetCell.y) {
|
||||||
|
//printf("test\n");
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/*int moved=0;
|
||||||
|
if (currentCell.x!=12) {
|
||||||
|
if (assignmentMatrix[currentCell.x+1][currentCell.y] < assignmentMatrix[currentCell.x][currentCell.y] && assignmentMatrix[currentCell.x+1][currentCell.y] != -1) {
|
||||||
|
currentCell.x=currentCell.x+1;
|
||||||
|
moved=1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (currentCell.x!=0 && moved==0) {
|
||||||
|
if (assignmentMatrix[currentCell.x-1][currentCell.y] < assignmentMatrix[currentCell.x][currentCell.y] && assignmentMatrix[currentCell.x-1][currentCell.y] != -1) {
|
||||||
|
currentCell.x=currentCell.x-1;
|
||||||
|
moved=1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (currentCell.y!=12 && moved==0) {
|
||||||
|
if (assignmentMatrix[currentCell.x][currentCell.y+1] < assignmentMatrix[currentCell.x][currentCell.y] && assignmentMatrix[currentCell.x][currentCell.y+1] != -1) {
|
||||||
|
currentCell.y=currentCell.y+1;
|
||||||
|
moved=1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (currentCell.y!=0 && moved==0) {
|
||||||
|
if (assignmentMatrix[currentCell.x][currentCell.y-1] < assignmentMatrix[currentCell.x][currentCell.y] && assignmentMatrix[currentCell.x][currentCell.y-1] != -1) {
|
||||||
|
currentCell.y=currentCell.y-1;
|
||||||
|
}
|
||||||
|
}*/
|
||||||
|
if ( isValidCrossing(¤tCell)) {
|
||||||
|
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;
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user