⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 maze.cpp

📁 经典迷宫问题求解
💻 CPP
字号:
#include<malloc.h>
#include"base.h"
#include<iostream.h>
#include<stdio.h>
#include"stack.h"
#include"maze.h"
#include<conio.h>
#include<stdlib.h>
#include<conio.h>
void Initialization();
void ReadCommand(char &cmd);
void Interpret(char cmd);
void PrintMaze(MazeType maze);
MazeType maze;
void main()
{
    char cmd;
    Initialization();
    gotoxy(1,5);
    cout<<"operation:";
    do{
		ReadCommand(cmd);
	    Interpret(cmd);
	}while(cmd!='q'&&cmd!='Q');
}


void Initialization()
{clrscr();
 cout<<"****************************************************"<<endl;
 cout<<"  CreatMaze--c  MazePath--m  PrintMaze--p  Quit--q"<<endl;
 cout<<"****************************************************"<<endl<<endl;
 gotoxy(1,22);
 cout<<"****************************************************"<<endl;
 cout<<"         Enter a operation code:c,m,p OR q"<<endl;
 cout<<"****************************************************"<<endl;
 gotoxy(11,5);
}

void ReadCommand(char &cmd)
{
	do{cmd=getchar();
}while(cmd!='c'&&cmd!='C'&&cmd!='m'&&cmd!='M'&&cmd!='p'&&cmd!='P'&&cmd!='q'&&cmd!='Q');
}

void Interpret(char cmd)
{
	switch(cmd)
{    case 'C':
     case 'c':
	      flag5=1;flag6=0;
	      cout<<"Please input the maze data's filename:";
	      cin>>filename;
	      FILE *p;int i;int j;
	      p=fopen(filename,"r");
		  while(p==NULL)
		  {clrscr();
		   Initialization();
		   gotoxy(1,4);
		   printf("Can't open the file!\n");
		   cout<<"Please input the filename of the maze:";
		   cin>>filename;
		   p=fopen(filename,"r");
		  }
	      fscanf(p,"%d",&rnum);
	      fscanf(p,"%d",&cnum);
		 for(i=0;i<rnum;i++)
			  for(j=0;j<cnum;j++)
				 fscanf(p,"%d",&a[i][j]);
			 fclose(p);
			 InitMaze(maze,a,rnum,cnum);
	       clrscr();
	       Initialization();
	       gotoxy(1,4);
	       cout<<"Maze has already been set up!"<<endl<<"operation:";
	       break;
    case 'M':
    case 'm':flag6=1;
	     if(flag5)
	     {
		 cout<<"Please input the begin and end of maze:"<<endl;
		 PosType start,end;
		 cout<<"begin:";
		 cin>>start.r>>start.c;
		 cout<<"end :";
		 cin>>end.r>>end.c;
		 if(MazePath(maze,start,end))
		      {clrscr();
		       Initialization();
		       gotoxy(1,4);
		      cout<<"There is a way from begin to end!Enter p to see the maze!"<<endl<<"operation:";
		       }
		 else
		      {clrscr();
		      Initialization();
		      gotoxy(1,4);
		      cout<<"There is no way!"<<endl;
		      gotoxy(11,5);}
		 break;
		 }
	    else
		 {  flag6=0;
			clrscr();
			Initialization();
			gotoxy(1,4);
			cout<<"error!!there is no maze!"<<endl<<"operation:";
			break;
		 }
    case 'P':
    case 'p':  if(flag5&&flag6)
		      { clrscr();
		       Initialization();
		       gotoxy(1,5);
		       PrintMaze(maze);
		       }
	       else
		 {clrscr();
		 Initialization();
		 gotoxy(1,4);
		 cout<<"There is no maze or you haven't input the Entry and Exit!"<<endl<<"operation:";
		 }
    }
}


void PrintMaze(MazeType maze)
{cout<<"The way from begin to end :"<<endl;
 for(int a=1;a<maze.m+1;a++)
	{for(int b=1;b<maze.n+1;b++)
	      printf("%c ",maze.arr[a][b]);
		     cout<<endl;
	  }
 cout<<"operation:";
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -