/* 迷路プログラム */ /* 言語:C */ #include #include /*ブール値の定義*/ #define TRUE 1 #define FALSE 0 typedef int BOOL; #define YOKO 7/*横幅*/ #define TATE 7/*縦幅*/ /*迷路データ(0:通れる道,1:壁,2:スタート,3:ゴール,4:通った道,5:正解ルート)*/ int maze[TATE][YOKO]={ {1,1,1,1,1,1,1}, {1,0,0,0,0,2,1}, {1,0,0,0,0,1,1}, {1,0,0,0,0,0,1}, {1,0,0,0,0,0,1}, {1,0,0,0,0,0,1}, {1,1,1,1,1,1,1} }; /*外部変数*/ int sx,sy;/*スタート地点*/ int gx=-1,gy=-1;/*ゴール地点*/ int sp,ri[100],rj[100];/*スタック*/ char chara[6][3]={" ","■","S","G","△","○"};/*キャラ*/ int count,anscount;/*行き止まりになった回数をカウント*/ int end=FALSE;/*終了判定(yes=true,no=false)*/ /*データ読み込み関数*/ void Input(char *file){ FILE *fin; int i,j,c; if((fin=fopen(file,"r"))==NULL){ fprintf(stderr,"Input error [%s]\n",file); return; } i=j=0; while((c=fgetc(fin))!=EOF){ if(c=='\n')continue; maze[i][j]=c-'0'; if(++j >= YOKO){ j=0; if(++i >= TATE){break;} } } fclose(fin); } /*初期化*/ void Init(){ int i,j; for(i=0;i"); i=atoi(gets(str)); printf("yoko-->"); j=atoi(gets(str)); printf("value(0:road,1:wall,2:start,3:goal)-->"); gets(str); maze[i][j]=str[0]-'0'; } /*全ての道が塞がっているかチェック関数*/ BOOL Check(){ int i,j; for(i=0;i"); gets(str); switch(str[0]){ case 'd':Disp();break; case 's': if(Solve(sy,sx)==FALSE){puts("解けない迷路");} else{Disp();} if(!end)printf("%dパターン中、一筆書き経路は%d通り。\n",count,anscount); break; case 'e':Edit();break; case 'l': printf("filename-->"); gets(file); if(file[0]!='\0')Input(file); break; case 'q':return 0; } } } /*----------------実行結果---------------- [d]isp,[s]olve,[e]dit,[l]oad,[q]uit -->d 0 1 2 3 4 5 6 0 ■■■■■■■ 1 ■    S■ 2 ■    ■■ 3 ■     ■ 4 ■     ■ 5 ■     ■ 6 ■■■■■■■ [d]isp,[s]olve,[e]dit,[l]oad,[q]uit -->s 解けない迷路 43207パターン中、一筆書き経路は0通り。 [d]isp,[s]olve,[e]dit,[l]oad,[q]uit -->e tate-->2 yoko-->5 value(0:road,1:wall,2:start,3:goal)-->0 [d]isp,[s]olve,[e]dit,[l]oad,[q]uit -->d 0 1 2 3 4 5 6 0 ■■■■■■■ 1 ■    S■ 2 ■     ■ 3 ■     ■ 4 ■     ■ 5 ■     ■ 6 ■■■■■■■ [d]isp,[s]olve,[e]dit,[l]oad,[q]uit -->s 解けない迷路 153745パターン中、一筆書き経路は824通り。 [d]isp,[s]olve,[e]dit,[l]oad,[q]uit -->l filename-->maze.txt [d]isp,[s]olve,[e]dit,[l]oad,[q]uit -->d 0 1 2 3 4 5 6 0 ■■■■■■■ 1 ■G■■ S■ 2 ■ ■ ■ ■ 3 ■   ■ ■ 4 ■■■ ■ ■ 5 ■     ■ 6 ■■■■■■■ [d]isp,[s]olve,[e]dit,[l]oad,[q]uit -->s (1,5)(2,5)(3,5)(4,5)(5,5)(5,4)(5,3)(4,3)(3,3)(3,2)(3,1)(2,1)(1,1) 0 1 2 3 4 5 6 0 ■■■■■■■ 1 ■G■■ S■ 2 ■○■ ■○■ 3 ■○○○■○■ 4 ■■■○■○■ 5 ■  ○○○■ 6 ■■■■■■■ [d]isp,[s]olve,[e]dit,[l]oad,[q]uit -->q Press any key to continue ---------------------------------------*/