Skip to content

Instantly share code, notes, and snippets.

@henrybear327
Created April 8, 2015 14:20
Show Gist options
  • Save henrybear327/0e3af6d14c8f0a6af94c to your computer and use it in GitHub Desktop.
Save henrybear327/0e3af6d14c8f0a6af94c to your computer and use it in GitHub Desktop.
ITSA Problem 4.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void walk_through(int row, int column);
int size, input[20][20];
char output[20][20];
int flag = 0;
int main()
{
while(scanf("%d", &size) != EOF) {
for(int i = 0; i < size; i++) {
for(int j = 0; j < size; j++) {
scanf("%d", &input[i][j]);
output[i][j] = input[i][j] + '0';
//printf("%c", output[i][j]);
}
//printf("\n");
}
output[1][1] = '#';
walk_through(1, 1);
flag = 0;
for(int i = 0; i < size; i++) {
for(int j = 0; j < size; j++) {
if(j != size - 1)
printf("%c ", output[i][j]);
else
printf("%c", output[i][j]);
}
printf("\n");
}
return 0;
}
}
void walk_through(int row, int column)
{
if(row == size - 2 && column == size - 2) {
flag = 1;
return;
}
if(row + 1 < size && input[row + 1][column] != 1 && output[row + 1][column] != '#' && flag == 0) { /*down*/
output[row + 1][column] = '#';
walk_through(row + 1, column);
}
if(row - 1 >= 0 && input[row - 1][column] != 1 && output[row - 1][column] != '#' && flag == 0) { /*up*/
output[row - 1][column] = '#';
walk_through(row - 1, column);
}
if(column - 1 >= 0 && input[row][column - 1] != 1 && output[row][column - 1] != '#' && flag == 0) { /*left*/
output[row][column - 1] = '#';
walk_through(row, column - 1);
}
if(column + 1 < size && input[row][column + 1] != 1 && output[row][column + 1] != '#' && flag == 0) { /*right*/
output[row][column + 1] = '#';
walk_through(row, column + 1);
}
if(flag == 0) {
output[row][column] = '*';
return;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment