Last active
August 29, 2015 14:03
-
-
Save kimitoboku/9431d02724869a081caf to your computer and use it in GitHub Desktop.
maze
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
#include<stdlib.h> | |
void disp(int ary[],int w,int h){ | |
int i,j; | |
for(i=1;i<w-1;i++){ | |
for(j=1;j<h-1;j++){ | |
switch(ary[i*w + j]){ | |
case 0:printf("#");break; | |
case 1:printf(".");break; | |
case 2:printf("G");break; | |
} | |
} | |
puts(""); | |
} | |
} | |
int solve(int ary[],int st,int gl,int w){ | |
int x = gl/10; | |
int y = gl%10; | |
if(ary[(x-1)*w + y] == 1){ | |
int f; | |
ary[(x-1)*w + y] = 0; | |
x--; | |
f = solve(ary,st,(x*10 + y),w); | |
x++; | |
if(f==0){ | |
printf("%d %d\n",x-1,y); | |
return 0; | |
} | |
ary[(x-1)*w + y] = 1; | |
}else if(st == gl){ | |
return 0; | |
} | |
if(ary[(x+1)*w + y] == 1){ | |
int f; | |
ary[(x+1)*w + y] = 0; | |
x++; | |
f = solve(ary,st,(x*10 + y),w); | |
x--; | |
if(f==0){ | |
printf("%d %d\n",x+1,y); | |
return 0; | |
} | |
ary[(x+1)*w + y] = 1; | |
}else if(st == gl){ | |
return 0; | |
} | |
if(ary[x*w + (y-1)] == 1){ | |
int f; | |
ary[x*w + (y-1)] = 0; | |
y--; | |
f = solve(ary,st,(x*10 + y),w); | |
y++; | |
if(f==0){ | |
printf("%d %d\n",x,y-1); | |
return 0; | |
} | |
ary[x*w + (y-1)] = 1; | |
}else if(st == gl){ | |
return 0; | |
} | |
if(ary[x* w + (y+1)] == 1){ | |
int f; | |
ary[x* w + (y+1)] = 0; | |
y++; | |
f = solve(ary,st,(x*10 + y),w); | |
y--; | |
if(f==0){ | |
printf("%d %d\n",x,y+1); | |
return 0; | |
} | |
ary[x* w + (y+1)] = 1; | |
}else if(st == gl){ | |
return 0; | |
} | |
return 1; | |
} | |
int main(){ | |
FILE *fp; | |
fp = fopen("maze.txt","r"); | |
if(fp == NULL){ | |
puts("Can not Open File"); | |
exit(1); | |
} | |
int w,h; | |
fscanf(fp,"%d",&w); | |
fscanf(fp,"%d",&h); | |
int i,j; | |
int ary[(w+2)*(h+2)]; | |
for(i=0;i<(w+2)*(h+2);i++){ | |
ary[i] = 0; | |
} | |
int buf; | |
for(i=1;i<w+1;i++){ | |
for(j=1;j<h+1;j++){ | |
fscanf(fp,"%d",&ary[i*(w+2)+j]); | |
} | |
} | |
disp(ary,w+2,h+2); | |
for(i=0;i<(w+2)*(h+2);i++){ | |
if(ary[i] == 2){ | |
break; | |
} | |
} | |
i = i/(w+2) * 10 + i%(w+2); | |
int x,y; | |
printf("Start X Point:"); | |
scanf("%d",&x); | |
printf("Start Y Point:"); | |
scanf("%d",&y); | |
int f = solve(ary,x*10+y,i,w+2); | |
if(f==1){ | |
puts("Can't arrive"); | |
} | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 6 | |
1 1 0 0 0 0 | |
0 1 0 1 1 0 | |
0 1 0 0 0 0 | |
0 1 0 1 1 2 | |
0 1 1 1 0 0 | |
0 0 0 0 0 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment