87 lines
1.7 KiB
C
87 lines
1.7 KiB
C
|
#include <stdio.h>
|
||
|
#include <stdlib.h>
|
||
|
#include <stdbool.h>
|
||
|
#include <unistd.h>
|
||
|
|
||
|
char map[100][100];
|
||
|
int visited[100][100];
|
||
|
int counter = 0;
|
||
|
|
||
|
bool isDrafty(int x, int y) {
|
||
|
// Determines whether there's a trap in the immediate vicinity of the square we're standing on
|
||
|
return (map[y][x] == '.' && (map[y+1][x] == 'T' || map[y-1][x] == 'T' || map[y][x+1] == 'T' || map[y][x-1] == 'T'));
|
||
|
}
|
||
|
|
||
|
|
||
|
void move(int mx, int my, int w, int h) {
|
||
|
// Moves our x, y by 1 until a wall is found or becomes drafty
|
||
|
char c = map[my][mx];
|
||
|
|
||
|
// Avoid visiting visited nodes(endless loop occurs)
|
||
|
if (visited[my][mx]) {
|
||
|
return ;
|
||
|
}
|
||
|
visited[my][mx] = true;
|
||
|
|
||
|
// Stop if drafty or wall. Increment counter if gold is found.
|
||
|
if (c == '#') {
|
||
|
return ;
|
||
|
}
|
||
|
|
||
|
// printf("x: %d, y: %d, c: %c\n", mx, my, c);
|
||
|
|
||
|
if (c == 'G') {
|
||
|
counter++;
|
||
|
}
|
||
|
|
||
|
if(c == 'T' || isDrafty(mx, my)){
|
||
|
return ;
|
||
|
}
|
||
|
|
||
|
// Move (as long as we're not stepping out of bounds)
|
||
|
if (mx < w) {
|
||
|
move(mx+1, my, w, h); //Right
|
||
|
}
|
||
|
if (my < h) {
|
||
|
move(mx, my+1, w, h); // Up
|
||
|
}
|
||
|
if (mx > 0) {
|
||
|
move(mx-1, my, w, h); //Left
|
||
|
}
|
||
|
if (my > 0) {
|
||
|
move(mx, my-1, w, h); // Down
|
||
|
}
|
||
|
|
||
|
return ;
|
||
|
}
|
||
|
|
||
|
int main(int argc, char **argv) {
|
||
|
int w, h;
|
||
|
scanf("%d %d", &w, &h);
|
||
|
int px, py;
|
||
|
|
||
|
// Populate map
|
||
|
for (int i=0; i<h; i++) {
|
||
|
for (int j=0; j<w; j++) {
|
||
|
scanf(" %c", &map[i][j]);
|
||
|
// Set player coordinates
|
||
|
if (map[i][j] == 'P') {
|
||
|
py = i;
|
||
|
px = j;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
for (int i=0; i < 100; i++) {
|
||
|
for(int j=0; j < 100; j++) {
|
||
|
visited[i][j] = false;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Initiate traversal
|
||
|
move(px, py, w, h);
|
||
|
|
||
|
// Print result
|
||
|
printf("%d\n", counter);
|
||
|
return 0;
|
||
|
}
|