/*============================================================
鉆迷宮<2.0>
迷宮用二維數組存儲;
迷宮隨機生成;
前進(jìn)方向只有四個(gè),就是上下左右;
用棧存儲走過(guò)的路,碰壁可以返回;
TC2.0下編譯通過(guò)!
============================================================*/
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <dos.h>
#define UP 1 /*用于存儲方向的常量*/
#define DOWN 2
#define LEFT 3
#define RIGHT 4
#define OK 0
#define ERROR -1struct maze{
int left;
int top;
int right;
int bottom;
int sign; /*記號(0表示空白,1表示墻,2表示走過(guò)的路,3表示走過(guò)并且返回的路,4老鼠所在位置)*/
}lab[22][42];/*定義迷宮存儲結構*/typedef struct SNode{
int data;
struct SNode *next;
}SNode;typedef struct {
int length;
SNode *top;
}STACK;/*定義存儲走過(guò)路線(xiàn)的棧*//*棧初始化*/
void InitStack(STACK *S)
{
S->top=NULL;
S->length=NULL;
}/*元素e入棧*/
int Push(STACK *S,int e)
{
SNode *p;
p=(SNode *)malloc(sizeof(SNode));
if(!p) return ERROR;
p->data=e;
p->next=S->top;
S->top=p;
S->length++;
return OK;
}/*棧頂元素出棧,e帶回棧頂元數*/
int Pop(STACK *S,int *e)
{
SNode *p;
if(S->top==NULL) return ERROR;
p=S->top;
*e=p->data;
S->top=p->next;
S->length--;
free(p);
return OK;
}
/*判斷S是否為空棧*/
int Empty(STACK S)
{
if(S.top==NULL) return OK;
return ERROR;
}/*初始化圖形顯示*/
int initialize(void)
{
int gdriver, gmode,errorcode;
gdriver=VGA;
gmode=VGAHI;
initgraph(&gdriver, &gmode, "d:\c源碼");
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* return with error code */
}
return 0;
}void showmaze(int i,int j)
{/*顯示迷宮函數*/
switch(lab[i][j].sign)
{
case 0: setfillstyle(SOLID_FILL,LIGHTBLUE);break;
case 1: setfillstyle(SOLID_FILL,MAGENTA); break;
case 2: setfillstyle(SOLID_FILL,GREEN);break;
case 3: setfillstyle(SOLID_FILL,DARKGRAY);break;
case 4: setfillstyle(SOLID_FILL,BLUE);break;
}
bar(lab[i][j].left,lab[i][j].top,lab[i][j].right,lab[i][j].bottom);
}/*生成迷宮函數*/
void initialmaze()
{
int i,j,n,leftx=100,topy=50,rightx=110,bottomy=60;
srand((int)time(0));
for(i=0;i<22;i++)/*隨機成生迷宮*/
for(j=0;j<42;j++)
{
lab[i][j].left=leftx+j*10;
lab[i][j].top=topy+i*10;
lab[i][j].right=rightx+j*10;
lab[i][j].bottom=bottomy+i*10;
n=rand()%20;
if(n<5)
lab[i][j].sign=1;
else
lab[i][j].sign=0;
}
for(i=0;i<42;i++)/*成生迷宮四周*/
{
lab[0][i].sign=1;
lab[21][i].sign=1;
}
for(i=0;i<22;i++)/*成生迷宮四周*/
{
lab[i][0].sign=1;
lab[i][41].sign=1;
}
lab[1][0].sign=0;/*為迷宮留入口及出口*/
lab[1][1].sign=0;
lab[1][2].sign=0;
lab[20][41].sign=0;
lab[20][40].sign=0;
lab[20][39].sign=0;
for(i=0;i<22;i++)/*隨機成生迷宮*/
for(j=0;j<42;j++)
showmaze(i,j);
}int main(void)
{
int i,j,way;
char flag='0';
STACK S;/*定義一個(gè)用于存儲老鼠走過(guò)的路線(xiàn)的棧*/
initialize();/*初始化圖形顯示*/
InitStack(&S);/*初始化棧*/
setbkcolor(LIGHTBLUE);/*設置背景色*/
setcolor(MAGENTA);/*設置前景色*/
initialmaze();/*成生迷宮*/
i=1;/*初始化老鼠位置*/
j=0;
lab[i][j].sign=4;
showmaze(i,j);/*顯示迷宮*/
setfillstyle(SOLID_FILL,BLUE);
bar(120,300, 480, 350);
moveto(130,310);
outtext("1.QUICK 2.SLOW");
moveto(130,330);
outtext("Chooses 1 or 2");
while(flag!='1' && flag!='2') flag=getche();
bar(120,300, 480, 350);
moveto(130,320);
if(flag=='2')
outtext("You Chooses 2.SLOW");
do{
lab[i][j].sign=2;
showmaze(i,j);
if(lab[i][j+1].sign==0)/*RIGHT*/
{
j++;
Push(&S,RIGHT);
}
else if(lab[i+1][j].sign==0)/*DOWN*/
{
i++;
Push(&S,DOWN);
}
else if(lab[i][j-1].sign==0)/*LEFT*/
{
j--;
Push(&S,LEFT);
}
else if(lab[i-1][j].sign==0)/*UP*/
{
i--;
Push(&S,UP);
}
else /*沒(méi)路*/
{
if(Empty(S)==OK) /*已經(jīng)退回起點(diǎn)*/
{
setfillstyle(SOLID_FILL,BLUE);
bar(120,300, 480, 350);
moveto(130,310);
outtext("The labyrinth does not have the outlet!");
moveto(130,330);
outtext("Press any key to exit...");
getche();
exit(1);
}
else/*返回一步*/
{
Pop(&S,&way);
lab[i][j].sign=3;
showmaze(i,j);
switch(way)
{
case RIGHT:j--;break;
case DOWN:i--;break;
case LEFT:j++;break;
case UP:i++;break;
}
}
}
lab[i][j].sign=4;
showmaze(i,j);/*顯示迷宮*/
if(flag=='2')
{
delay(90000);
sound(700);
delay(10000);
nosound();
}
}while(i!=20 || j!=41);/*走到出口*/
setfillstyle(SOLID_FILL,BLUE);
bar(120,300, 480, 350);
moveto(130,310);
outtext("Found a road!");
moveto(130,330);
outtext("Press any key to exit...");
getche();
closegraph();/*關(guān)閉圖形顯示*/
return 0;
}
迷宮文件boya.ice:
8
9
#########
#s0##0###
#0##00###
#0##0####
#0000####
#0##0####
#0##00e##
#0000####
package maze;
import java.io.*;
import java.util.*;
public class Maze{
private char[][] maze;//迷宮數組
private int startX,startY,endX,endY;//迷宮起點(diǎn),終點(diǎn)的位置
private int x,y,step=0;//迷宮長(cháng)寬及步驟
//依據輸入的文件名創(chuàng )建對象
private Maze(String fileName){
try{
LinkedList aList=new LinkedList();//用于存儲文件每行的內容
BufferedReader files=new BufferedReader(new FileReader("map\\"+fileName));
//將每行的內容依次加入到LinkedList中
String temp=new String();
int i=0;
while((temp=files.readLine())!=null){
aList.add(temp);
}
files.close();
//讀取并設置迷宮的長(cháng)寬
x=Integer.parseInt((String)aList.getFirst())+2;
aList.removeFirst();
y=Integer.parseInt((String)aList.getFirst())+2;
aList.removeFirst();
//依據長(cháng)寬對迷宮進(jìn)行初始化
maze=new char[x][y];
//將迷宮的賦予外圍墻
for(i=0;i<x;i++){
maze[i][0]='#';
maze[i][y-1]='#';
}
for(i=0;i<y;i++){
maze[0][i]='#';
maze[x-1][i]='#';
}
//將LinkedList中內容讀入數組
Iterator it=aList.iterator();
i=1;
char[] row;
while(it.hasNext()){
temp=((String)it.next());
row=new char[y-2];
row=temp.toCharArray();
for(int j=1;j<y-1;j++){
maze[i][j]=row[j-1];
if(maze[i][j]=='s'){
startX=i;
startY=j;
maze[i][j]='0';
}
else if(maze[i][j]=='e'){
endX=i;
endY=j;
maze[i][j]='0';
}
}
i++;
}
}
catch(FileNotFoundException e){
System.out.println("File Name Input Wrong!!!");
}
catch(IOException e){
System.out.println("Wrong Input!!!");
}
}
//遞歸方法尋找路徑
private boolean findWay(int x,int y){
if(maze[endX][endY]=='i')
return true;
else
if(maze[x][y]=='0'){
maze[x][y]='i';
if(findWay(x-1,y))
return true;
else if(findWay(x+1,y))
return true;
else if(findWay(x,y+1))
return true;
else if(findWay(x,y-1))
return true;
else{
maze[x][y]='c';
return false;
}
}
else return false;
}
//打印迷宮路徑
private void show(){
maze[startX][startY]='s';
maze[endX][endY]='e';
for(int i=1;i<x-1;i++){
for(int j=1;j<y-1;j++){
if(maze[i][j]=='i'){
maze[i][j]=' ';
step++;
}
else if(maze[i][j]=='c') maze[i][j]='0';
System.out.print(maze[i][j]);
}
System.out.println("");
}
System.out.println("I Have went "+step+" Steps To The End!");
}
public static void main(String arg[]){
try{
System.out.println("Boya(8*9)\n"+"Ice(10*12)\n"+"Sky(15*17)\n"+"Input the map name:");
BufferedReader is=new BufferedReader(new InputStreamReader(System.in));
for(;;){
String input=new String();
input=is.readLine().trim();
if(input.equals("q")) break;
else{
Maze boya=new Maze(input+".ice");
if(boya.findWay(boya.startX,boya.startY)){
boya.show();
}
else System.out.println("No Ways to the end!");
}
System.out.println("Input another map name or input 'q' to quit:");
}
is.close();
}
catch(IOException e){
System.out.println("Wrong Input!!!");
}
catch(NullPointerException e){
System.out.println("Wrong Input!!!");
}
}
}
聯(lián)系客服