?? labyreinth.cpp
字號:
//程序名:labyrinth.cpp
// 程序功能:迷宮通路尋找的實現(xiàn)
// 作者:黃秋旋
// 日期:2008.12.20
// 版本:1.0
// 修改內(nèi)容:
// 修改日期:
// 修改作者:
//對應(yīng)類實現(xiàn)文件: labyrinth.h
// Stack.h
#include"labyrinth.h"
////////////////////////////////////////////////////////////////////////////////////////////////////
//函數(shù)名:主函數(shù)
//函數(shù)返回值:無
void main()
{
int i,j;
char ch;
choice:cout<<"是否重新輸入迷宮:Y/y:是,N/n:否 \n請選擇(Y/N):"; //選擇是否重新輸入迷宮
cin>>ch;
if(ch=='Y' || ch=='y') //選擇重新輸入迷宮
{
cout<<"\n請輸入8行10列的迷宮矩陣,其中最外層均為“1”!\n";
lg:for(i=0;i<m+2;i++) //輸入迷宮
for(j=0;j<n+2;j++)
{
cin>>maze[i][j];
mark[i][j]=0; //初始化未訪問標(biāo)志
}//for
}//if:選擇Y
else if(ch=='N' || ch=='n')//選擇從文件中讀入迷宮
{
ifstream fip;
fip.open("labyrinth.txt",ios::in|ios::binary); //打開文件
if(fip.fail()) //打開失敗
{
cout<<"文件不存在!請重新輸入迷宮!";
goto lg; //回到if重新輸入迷宮
}//else if
int in;
for(i=0;i<m+2;i++)
{
for(j=0;j<n+2;j++)
{
fip>>in; //讀取迷宮數(shù)據(jù)
maze[i][j]=in;
cout<<maze[i][j]<<' '; //輸出迷宮
mark[i][j]=0; //初始化未訪問標(biāo)志
}//for
cout<<endl;
}//for
fip.close(); //關(guān)閉文件
}//else if
else //輸出錯誤,提示重新輸入
{
cout<<"請重新輸入你的選擇!";
goto choice; //重新選擇讀入迷宮的方式
}//else
cout<<"(x,y,d)中的x,y分別表示迷宮中的坐標(biāo)!"<<endl<<endl;
cout<<"0: 向↓,1: 向↘,2: 向→, 3:向↖"<<endl<<endl;
cout<<"4: 向↑,5: 向↖,6: 向←, 7: 向↘" <<endl<<endl;
cout<<"有兩種方式輸出迷宮路徑,"<<endl<<endl;
cout<<"Y/y:逆序(遞歸算法),N/n:順序(非遞歸非遞歸)! \n";
cout<<"\n請輸入你的選擇: ";
choice1:cin>>ch; //選擇想調(diào)用的算法
cout<<endl;
if(ch=='N' || ch=='n') //選擇非遞歸算法
{
cout<<"采用順序輸出迷宮路徑: \n";
cout<<"\nd 表示做到下一個坐標(biāo)的方向!\n";
cout<<"\n路徑為:\n";
Seek(); //調(diào)用尋路非遞歸函數(shù)
}
else if(ch=='Y' || ch=='y') //選擇遞歸算法
{
cout<<"采用逆序輸出迷宮路徑! \n";
cout<<"\nd 表示從前一個坐標(biāo)走到該坐標(biāo)的方向\n";
cout<<"\n路徑為:\n";
mark[1][1]=1; //加訪問標(biāo)志
if(Seekd(1,1)) //調(diào)用遞歸時用
cout<<"("<<1<<","<<1<<")"<<endl; //尋找到通路時,輸出迷宮入口處的坐標(biāo)
}//else if
else //輸出錯誤,提示重新輸入
{
cout<<"\n輸入錯誤,請重新輸入你的選擇:";
goto choice1; //重新選擇算法
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -