(Edit: Whoops, I misread you and thought you were saying you couldn't remember the brute-force one, not the more elegant solution. Ah what the hell, it was fun to write. :)
A recursive implementation of depth-first search should only take ten minutes to write. OK, here, I'll do it without looking anything up. Start time 8:55pm.
#include <stdio.h>
#include <memory.h>
const int WIDTH = 32; // adjust to suit
const int HEIGHT = 32; // adjust to suit
const bool WALLS[WIDTH][HEIGHT] = {
// enter maze layout here, true = wall
// this isn't a test of file IO knowledge
// make sure the outer perimeter of the maze is
// fully closed with walls
};
const int START_X = 16; // adjust to suit
const int START_Y = 16; // adjust to suit
const int GOAL_X = 16; // adjust to suit
const int GOAL_Y = 16; // adjust to suit
bool search_maze(bool **searched, int cur_x, int cur_y,
int start_x, int start_y) {
const int DIRS = 4;
const int DIR_X = { -1, 0, 1, 0 };
const int DIR_Y = { 0, 1, 0, -1 };
searched[cur_x][cur_y] = true;
for (int i=0; i<DIRS; i++) {
int new_x = cur_x + DIR_X[i];
int new_y = cur_y + DIR_Y[i];
if (WALLS[new_x][new_y])
continue;
// if moving in this direction lands us on the start
// point, or searching starting here returns true,
// then we're done and our current pos is on the path
if ((new_x == start_x && new_y == start_y) ||
(!searched[new_x][new_y] && search_maze(
searched, new_x, new_y, start_x, start_y)) {
printf("Go to %d, %d\n", new_x, new_y);
return true;
}
}
return false;
}
int main(int argc, char **argv) {
bool searched[WIDTH][HEIGHT];
memset(searched, 0, sizeof(searched));
printf("Starting at %d, %d\n", START_X, START_Y);
search_maze(searched, GOAL_X, GOAL_Y, START_X, START_Y);
printf("Goal at %d, %d\n", GOAL_X, GOAL_Y);
return 0;
}
End time 9:12pm. So 17 minutes (what's that rule about estimating software dev time?), and I haven't run and tested it yet, but it should show what the interviewer wants.