题目:
4. 读取file4.txt 文件中的字符数据,该文件中的数据用来描述一个正方形的迷宫,“#” 表示不可到达的地方,“—” 表示 可以到达的地方, 迷宫的 入口 和 出口都 在正方形的某个边上,要求找到 从 入口 到出口的 所有 路径(不走重复的路),并将每一种路径显示出来,显示方法 是 把 “—”改为“+”,
并将所有解保存到 file4_answer.txt
例如:file.txt中存放的 字符数据为:
# - # # # #
# - - - # #
- - # - - #
# - - - - #
# - # - - #
# # # - # #
则输出结果:
# + # # # #
# + + + # #
- - # + + #
# - - - + #
# - # + + #
# # # + # #
# + # # # #
# + + + # #
- - # + + #
# - - + + #
# - # + - #
# # # + # #
# + # # # #
# + + + # #
- - # + - #
# - - + + #
# - # + + #
# # # + # #
# + # # # #
# + + + # #
- - # + - #
# - - + - #
# - # + - #
# # # + # #
# + # # # #
# + - - # #
- + # - - #
# + + + + #
# - # + + #
# # # + # #
# + # # # #
# + - - # #
- + # - - #
# + + + - #
# - # + - #
# # # + # #
# + # # # #
# + - - # #
- + # + + #
# + + + + #
# - # + + #
# # # + # #
第一种解法:用栈,主要是回溯法,深度优先搜索路径
#include<stdio.h>
#include<windows.h>
char maze_char[20][20];//存储从文件中读取的'-''#'字符,构成迷宫
int m,n;
int direct[4][2]={{0,1},{1,0},{0,-1},{-1,0}};//右下左上四个方向
int remark[100][2];
int step=1;
int end_i,end_j;
//判断是否可以走
int can_go(int i,int j)
{
int k;
if(maze_char[i][j]=='#')
return 0;
for(k=0;k<step;k++){//判断此步是否已在路径中,在的话,返回0,否则构成回路
if(i==remark[k][0]&&j==remark[k][1])
return 0;
}
return 1;
}
//写入文件
void write()
{
FILE *fp;
int k,i,j;
for(k=0;k<step;k++){//把路径上的-号变成+号
maze_char[remark[k][0]][remark[k][1]]='+';
}
//打印路径
puts("--迷宫路径(以第一行第一个-号为入口,最后一行第一个-号为出口)----");
for(i=1;i<m-1;i++){
for(j=1;j<n-1;j++)
printf("%c ",maze_char[i][j]);
printf("\n");
}
if((fp=fopen("document/file4_answer.txt","a"))==NULL)
{
printf("can not open the file!!!\n");
exit(0);
}
fputs("\n***********************************************\n",fp);
for(i=1;i<m-1;i++){
for(j=1;j<n-1;j++)
{
fputc(maze_char[i][j],fp);
fputc(' ',fp);
}
fputc('\n',fp);
}
if(fclose(fp))
{
printf("can not close the file!!!\n");
exit(0);
}
//写完文件后,把路径的+号变成-号
for(k=0;k<step;k++){
maze_char[remark[k][0]][remark[k][1]]='-';
}
}
//搜索路径
void search(int i,int j)
{
int k=0;
if(i==end_i&&j==end_j){//找到出口
write();
return;
}
for(k=0;k<4;k++){
if(can_go(i+direct[k][0],j+direct[k][1])){
remark[step][0]=i+direct[k][0];
remark[step][1]=j+direct[k][1];
step++;
search(i+direct[k][0],j+direct[k][1]);
step--;//search完四个方向后,返回上一个路径
}
}
}
void main()
{
char ch;
int i=1,j=1;//m和n表示m*n维的迷宫(为了便宜判断,以m*n是围上一层墙的迷宫)
FILE *fp;
if((fp=fopen("document/file4.txt","r"))==NULL)
{
printf("can not open the file!!!\n");
exit(0);
}
//从文件中读取迷宫
while((ch=fgetc(fp))!=EOF)
{
if(ch=='\n')
{
j=1;
i++;
}
else if(ch!=' ')//丢弃空格
{
if(i==1&&ch=='-')//第一行的第一个'-'作为入口
{
remark[0][0]=i;
remark[0][1]=j;
}
maze_char[i][j]=ch;
j++;
}
}
m=i+2;
n=j+1;
for(j=1;j<n-1;j++)
if(maze_char[m-2][j]=='-'){//把最后一行的第一个'-'作为出口
end_i=m-2;
end_j=j;
break;
}
//给迷宫围上一层墙
for(i=0;i<m;i++){
maze_char[0][i]='#';
maze_char[m-1][i]='#';
}
for(j=0;j<n;j++){
maze_char[j][0]='#';
maze_char[j][n-1]='#';
}
puts("----迷宫----");
for(i=1;i<m-1;i++){
for(j=1;j<n-1;j++)
printf("%c ",maze_char[i][j]);
printf("\n");
}
search(remark[0][0],remark[0][1]);
}
第二种解法: 用队列,广度优先搜索
#include<stdio.h>
#include<windows.h>
typedef struct node
{
int i,j,pre;
}Node;
Node remark[100];//记录每一个位置所能到达的位置
int count=1;//记录remark中元素的个数
char maze_char[20][20];//存储从文件中读取的'-''#'字符,构成迷宫
int direct[4][2]={{0,1},{1,0},{0,-1},{-1,0}};//右下左上四个方向
void search(int front)
{
int k;
int i=remark[front].i;
int j=remark[front].j;
if(front>=count)
return ;
for(k=0;k<4;k++)
{
if(maze_char[i+direct[k][0]][j+direct[k][1]]=='-'&&maze_char[i+direct[k][0]][j+direct[k][1]]!='*'){
if(remark[remark[front].pre].i==i+direct[k][0]&&remark[remark[front].pre].j==j+direct[k][1])
{
}else{
remark[count].i=i+direct[k][0];
remark[count].j=j+direct[k][1];
remark[count].pre=front;
maze_char[i+direct[k][0]][j+direct[k][1]]='*';
count++;
}
}
}
search(front+1);
}
void main()
{
char ch;
int i=1,j=1,m=0,n=0;//m和n表示m*n维的迷宫(为了便宜判断,以m*n是围上一层墙的迷宫)
int end_i,end_j;
FILE *fp;
if((fp=fopen("document/file4.txt","r"))==NULL)
{
printf("can not open the file!!!\n");
exit(0);
}
//从文件中读取迷宫
while((ch=fgetc(fp))!=EOF)
{
if(ch=='\n')
{
j=1;
i++;
}
else if(ch!=' ')//丢弃空格
{
if(i==1&&ch=='-')//第一行的第一个'-'作为入口
{
remark[0].i=i;
remark[0].j=j;
remark[0].pre=-1;
}
maze_char[i][j]=ch;
j++;
}
}
m=i+2;
n=j+1;
for(j=1;j<n-1;j++)
if(maze_char[m-2][j]=='-'){
end_i=m-2;
end_j=j;
}
//给迷宫围上一层墙
for(i=0;i<m;i++){
maze_char[0][i]='#';
maze_char[m-1][i]='#';
}
for(j=0;j<n;j++){
maze_char[j][0]='#';
maze_char[j][n-1]='#';
}
for(i=0;i<m;i++){
for(j=0;j<n;j++)
printf("%c ",maze_char[i][j]);
printf("\n");
}
search(0);
for(i=0;i<count;i++){
if(remark[i].i==end_i&&remark[i].j==end_j){
j=i;
while(remark[j].pre>=0){
maze_char[remark[j].i][remark[j].j]='+';
j=remark[j].pre;
}
}
else
maze_char[remark[i].i][remark[i].j]='-';
}
maze_char[remark[0].i][remark[0].j]='+';
printf("***************************************************\n");
for(i=0;i<m;i++){
for(j=0;j<n;j++)
printf("%c ",maze_char[i][j]);
printf("\n");
}
//把改变后的maze_char写到文件file4_answer.txt中
if((fp=fopen("document/file4_answer.txt","w"))==NULL)
{
printf("can not open the file!!!\n");
exit(0);
}
for(i=1;i<m-1;i++){
for(j=1;j<n-1;j++)
{
fputc(maze_char[i][j],fp);
fputc(' ',fp);
}
fputc('\n',fp);
}
if(fclose(fp))
{
printf("can not close the file!!!\n");
exit(0);
}
}