博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
迷宫C描述——栈的举例
阅读量:4679 次
发布时间:2019-06-09

本文共 3454 字,大约阅读时间需要 11 分钟。

 
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define MAXSIZE 50
#define ERROR -1
#define OK 0
#define FALSE 0
#define TRUE 1
typedef 
enum{RIGHT,DOWN,LEFT,UP} Direction;
typedef 
enum{YES,NO} MarkTag;
typedef 
struct position{
    
int x;
    
int y;
}Position;
typedef 
struct{
    
int order;
    Position seat;
    Direction di;
}SElemType;
typedef 
struct{
    SElemType *elem;
    
int top;
}Stack;
char maze[MAXSIZE+
2][MAXSIZE+
2];
int InitStack(Stack *S){
    S->elem=(SElemType *)malloc(MAXSIZE*MAXSIZE*
sizeof(SElemType));
    
if(!S->elem) 
return ERROR;
    S->top=
0;
    
return OK;
}
int Push(Stack *S,SElemType e){
    
if(S->top>=MAXSIZE*MAXSIZE) 
return ERROR;
    S->elem[S->top++]=e;
    
return OK;
}
int Pop(Stack *S,SElemType *e){
    
if(S->top<=
0
return ERROR;
    *e=S->elem[--S->top];
    
return OK;
}
int Empty(Stack S){
    
if(S.top==
0
return TRUE;
    
return FALSE;
}
int createMaze(
char *filename,Position *startpos,Position *endpos){
    FILE *fp;
    
int i,j,rows,cols,temp;
    Position start,end;
    fp=fopen(filename,
"
r
");
    
if(!fp){
        printf(
"
open file %s error!\n
",filename);
        
return ERROR;
    }
    
if(!feof(fp)){
        fscanf(fp,
"
%d %d 
",&rows,&cols);
        fscanf(fp,
"
%d %d 
",&start.x,&start.y);
        fscanf(fp,
"
%d %d 
",&end.x,&end.y);
    }
    
for(i=
1;i<=rows;i++){
        
for(j=
1;j<=cols;j++){
            fscanf(fp,
"
%d 
",&temp);
            maze[i][j]=
48+temp;
        }
    }
    fclose(fp);
    
    
for(i=
0,j=
0;i<=rows+
1;i++)maze[i][j]=
'
1
';
    
for(i=
0,j=cols+
1;i<=rows+
1;i++)maze[i][j]=
'
1
';
    
for(i=
0,j=
0;j<=cols+
1;j++)maze[i][j]=
'
1
';
    
for(i=rows+
1,j=
0;j<=cols+
1;j++)maze[i][j]=
'
1
';
    *startpos=start;
    *endpos=end;
    
    
for (i=
0;i<=rows+
1;i++)
    {
        
for(j=
0;j<=cols+
1;j++)
        {
            printf(
"
%c
",maze[i][j]);
        }
        printf(
"
\n
");
    }
    
return OK;
}
int canPass(Position curpos){
    
if(maze[curpos.x][curpos.y]==
'
0
'
return TRUE;
    
return FALSE;
}
void markPos(Position curpos,MarkTag tag){
    
switch(tag){
        
case YES:maze[curpos.x][curpos.y]=
'
.
';
break;
        
case NO:maze[curpos.x][curpos.y]=
'
#
';
break;
    }
}
Position nextPos(Position curpos,Direction dir){
    Position nextpos;
    
switch(dir){
        
case RIGHT:nextpos.x=curpos.x;nextpos.y=curpos.y+
1;
break;
        
case DOWN:nextpos.x=curpos.x+
1;nextpos.y=curpos.y;
break;
        
case LEFT:nextpos.x=curpos.x;nextpos.y=curpos.y-
1;
break;
        
case UP:nextpos.x=curpos.x-
1;nextpos.y=curpos.y;
break;
    }
    
return nextpos;
}
Direction nextDir(Direction dir){
    
switch(dir){
        
case RIGHT:
return DOWN;
        
case DOWN:
return LEFT;
        
case LEFT:
return UP;
    }
}
int Solve(Stack *S,Position start,Position end){
    Position curpos;
    SElemType e;
    
int curstep=
1;
    
if(InitStack(S)==ERROR) 
return FALSE;
    curpos=start;
    
do{
        
if(canPass(curpos)){
            markPos(curpos,YES);
            e.order=curstep;e.seat=curpos;e.di=RIGHT;
            Push(S,e);
            
if(curpos.x==end.x && curpos.y==end.y)
                
return TRUE;
            curpos=nextPos(curpos,RIGHT);
            curstep++;
        }
        
else
        {
            
if(!Empty(*S)){
                
if(Pop(S,&e)==ERROR) 
return FALSE;
                
while(e.di==UP && !Empty(*S)){
                    curpos=e.seat;markPos(curpos,NO);
                    
if(Pop(S,&e)==ERROR)
return FALSE;
                }
                
if(e.di!=UP){
                    e.di=nextDir(e.di);
                    Push(S,e);
                    curpos=nextPos(e.seat,e.di);
                }
            }
        }
    }
while(!Empty(*S));
    
return FALSE;
}
void main(
void)
{
    Position startPos,endPos;
    Stack path;
    SElemType e;
    
char *fname=
"
in.txt
";
    
if(createMaze(fname,&startPos,&endPos)==ERROR) 
return;
    Solve(&path,startPos,endPos);
    
while(!Empty(path)){
        Pop(&path,&e);
        printf(
"
(%d,%d)\n
",e.seat.x,e.seat.y);
    }
    system(
"
pause
");
}
 
in.txt
8 8  
1 1 8 8 
0 0 1 0 0 0 1 0 
0 0 1 0 0 0 1 0 
0 0 0 0 1 1 0 0 
0 1 1 1 0 0 0 0 
0 0 0 1 0 0 0 0 
0 1 0 0 0 1 0 0 
0 1 1 1 0 1 1 0 
1 0 0 0 0 0 0 0 

转载于:https://www.cnblogs.com/13yan/archive/2012/01/21/2328384.html

你可能感兴趣的文章
Oracle-建表course
查看>>
Java常用的非受检异常
查看>>
HDOJ-2054
查看>>
centos7安装eclipse
查看>>
Web:AJAX的详解
查看>>
两种比较器Comparable 和 Comparator
查看>>
S2JDBC テーブルを利用した独自仕様のid採番メソッド
查看>>
P3698 [CQOI2017]小Q的棋盘
查看>>
动态规划入门 洛谷P2409 Y的积木
查看>>
【第一季】CH04_FPGA设计Verilog基础(一)Enter a post title
查看>>
算法的基本概念
查看>>
2018-2019-1 20189206 《Linux内核原理与分析》第八周作业
查看>>
股票买卖问题
查看>>
Matlab+ModelSim“傻瓜化”设计数字滤波器
查看>>
直接数字频率合成器(DDS)基本原理
查看>>
转载:【小作品】STM32无线WIFI视频小车制作剖析(上)
查看>>
echarts学习网站
查看>>
原生的js轮播图
查看>>
字符串操作、文件操作,英文词频统计预处理
查看>>
telnet不能用!!!提示:-bash: telnet: command not found
查看>>