《数据结构(A)》第3章设计型作业--栈与队的应用

设计型作业题目

3.3  (要求学号末位为奇数的学生必作,学号末位为偶数的学生选作。可以参考《数据结构题集(C 语言版)》,第96页,实习2 栈和队列及其应用,第2.1题:停车场管理,难度系数为3。题目有些改变)某商场有一个100个车位的停车场,当车位未满时,等待的车辆可以进入并计时;当车位已满时,必须有车辆离开,等待的车辆才能进入;当车辆离开时计算停留的时间,并且按照每小时10元收费。

汽车的输入信息格式可以是(进入/离开,车牌号,进入/离开时间),要求可以随时显示停车场内的车辆信息以及收费历史记录。

3.4  (要求学号末位为偶数的学生必作,学号末位为奇数的学生选作。这个题目不是《数据结构题集(C 语言版)》,第100页,实习2 栈和队列及其应用,第2.6题:银行业务模拟,习题集上题目的难度系数为5。留作研究型题目吧!)某银行营业厅共有6个营业窗口,设有排队系统语音叫号。该银行的业务分为公积金、个人卡、企业卡等3种。公积金业务指定1号窗口,个人卡业务指定2、3、4号窗口,企业卡业务指定5、6号窗口。但如果5、6号窗口全忙,而2、3、4号窗口有空闲时,企业卡业务也可以在空闲的2、3、4号窗口之一办理。

客户领号、业务完成可以作为输入信息,要求可以随时显示6个营业窗口的状态。

3.5  (所有学生必作,《数据结构题集(C 语言版)》,第26页,第3.32题的简化版:原题难度系数为4。参考本章课堂幻灯片。)

原第3.32题(参考本章幻灯片):

试利用循环队列编写求k阶Fibonacci序列中前n+1项(f0, f1 , f2 ,… fn)的算法,要求满足fn≤max而fn+1>max,其中max为某个约定的常数。(注意本题所用循环队列的容量仅为k,则在算法执行结束时,留在循环队列中的元素应是k阶斐波那契序列中的最后k项fn-k+1, … fn)。

本题具体化为下列要求:

已知4阶Fibonacci斐波那契序列如下:f0=f1=f2=0, f3= 1,  … , fi= fi-1+fi-2+fi-3+fi-4,利用容量为k=4的循环队列,构造序列的前n+1项(f0, f1 , f2 ,… fn),要求满足fn≤200而fn+1>200。

3.6  (选作题,八皇后问题,Eight Queens Problem):设8皇后问题的解为(x1, x2, x3, …,x8),约束条件为:在8x8的棋盘上,其中任意两个xi和xj不能位于棋盘的同行、同列及同对角线。要求用一位数组进行存储,输出所有可能的排列。

3.7  (所有学生必作。《数据结构题集(C 语言版)》,第105页,实习2 栈和队列及其应用,第2.9题:迷宫问题,Maze Problem,题目难度系数为4。请同学们务必阅读第105页至第115页,并严格按照习题集之上的要求撰写设计报告。)

迷宫求解:用二维矩阵表示迷宫,自动生成或者直接输入迷宫的格局,确定迷宫是否能走通,如果能走通,输出行走路线。

设计型作业题目解答

【第3.3题解答】

解题思路:

通过switch语句分别判断用户要执行的操作,车辆进入时,存入车牌号和停车时间,车辆离开时,输入车牌号和离开的时间,打印停车场信息,则将车辆信息逐个输出,为程序运行方便,将以下程序将停车场的位置设为3

实验结果截图:

 源代码:

main.cpp

#include "Chapter3_3.h"
int main() {
  int comm;
  int no, e1, time, e2;
  int i, j;
  SqStack* St, * St1;//St是停车场,St1是在有车离开时记录为该车移开位置的车辆
  SqQueue* Qu;   //Qu是候车场
  InitStack(St);
  InitStack(St1);
  InitQueue(Qu);
  do
  {
    printf("***********1:到达          2:离开 **********\n");
    printf("**********3:显示停车场    4 : 显示候车场***\n");
    printf("***************0 : 退出: *******************\n");
    scanf_s("%d", &comm);
    switch (comm)
    {
    case 1:     /*汽车到达*/
      printf("输入车号和时间(设车号和时间均为整数): ");
      scanf_s("%d%d", &no, &time);
      if (!StackFull(St))         /*停车场不满*/
      {
        Push(St, no, time);
        printf("  >>停车场位置:%d\n", St->top + 1);
      }
      else                        /*停车场满*/
      {
        if (!QueueFull(Qu))     /*候车场不满*/
        {
          EnQueue(Qu, no);
          printf("停车场已满");
          printf("停车场已满,进入候车场\n");
          printf("  >>候车场位置:%d\n", Qu->rear);
        }
        else
          printf("  >>候车场已满,不能停车\n");
      }
      break;
    case 2:     /*汽车离开*/
      printf("输入车号和时间(设车号和时间均为整数): ");
      scanf_s("%d%d", &no, &time);
      for (i = 0; i <= St->top && St->CarNo[i] != no; i++);  //在栈中找
      if (i > St->top)
        printf("  >>未找到该编号的汽车\n");
      else
      {
        for (j = i; j <= St->top; j++)
        {
          Pop(St, &e1, &e2);
          Push(St1, e1, e2);        /*倒车到临时栈St1中*/
        }
        Pop(St, &e1, &e2);              /*该汽车离开*/
        printf("  >>%d汽车停车费用:%d\n", no, (time - e2) * Price);
        while (!StackEmpty(St1))    /*将临时栈St1重新回到St中*/
        {
          Pop(St1, &e1, &e2);
          Push(St, e1, e2);
        }
        if (!QueueEmpty(Qu))        /*队不空时,将队头进栈St*/
        {
          DeQueue(Qu, &e1);
          Push(St, e1, time);       /*以当前时间开始计费*/
        }
      }
      break;
    case 3:     /*显示停车场情况*/
      if (!StackEmpty(St))
      {
        printf("  >>停车场中的车辆:"); /*输出停车场中的车辆*/
        DisStack(St);
      }
      else
        printf("  >>停车场中无车辆\n");
      break;
    case 4:     /*显示候车场情况*/
      if (!QueueEmpty(Qu))
      {
        printf("  >>候车场中的车辆:"); /*输出候车场中的车辆*/
        DisQueue(Qu);
      }
      else
        printf("  >>候车场中无车辆\n");
      break;
    case 0:     /*结束*/
      if (!StackEmpty(St))
      {
        printf("  >>停车场中的车辆:"); /*输出停车场中的车辆*/
        DisStack(St);
      }
      if (!QueueEmpty(Qu))
      {
        printf("  >>候车场中的车辆:"); /*输出候车场中的车辆*/
        DisQueue(Qu);
      }
      break;
    default:    /*其他情况*/
      printf("  >>输入的命令错误\n");
      break;
    }
  } while (comm != 0);
  return 0;
}

Chapter 3_3.h

#include <stdio.h>
#include <malloc.h>
#define N 3//停车场内最多的停车数
#define M 4//候车场内最多的车辆数
#define Price 10//每小时收费10元
typedef struct {
  int CarNo[N];//车牌号
  int CarTime[N];//进场时间
  int top;//栈顶指针
}SqStack;
typedef struct {
  int CarNo[M];//车牌号
  int front, rear;//头指针和尾指针
}SqQueue;//定义顺序队列的循环类型
//顺序栈的基本操作
void InitStack(SqStack* S);
int StackEmpty(SqStack* S);
int StackFull(SqStack* S);
int Push(SqStack* S, int e1, int e2);
int Pop(SqStack* S, int* e1, int* e2);
void DisStack(SqStack* S);

//循坏队列的基本操作
void InitQueue(SqQueue* Q);
int QueueEmpty(SqQueue* Q);
int QueueFull(SqQueue* Q);
int EnQueue(SqQueue* Q, int e);
int DeQueue(SqQueue* S, int* e);
void DisQueue(SqQueue* Q);

Chapter3_3.cpp

#include "Chapter3_3.h"
void InitStack(SqStack* S) {
  S = (SqStack*)malloc(sizeof(SqStack));
  S->top = -1;
}

int StackEmpty(SqStack* S) {
  return (S->top = -1);
}

int StackFull(SqStack* S) {
  return (S->top == N - 1);
}

int Push(SqStack* S, int e1, int e2) {
  if (S->top == N - 1)
    return 0;
  S->top++;
  S->CarNo[S->top] = e1;
  S->CarTime[S->top] = e2;
  return 1;
}

int Pop(SqStack* S, int* e1, int* e2) {
  if (S->top == -1)
    return 0;
  *e1 = S->CarNo[S->top];
  *e2 = S->CarTime[S->top];
  S->top--;
  return 1;
}

void DisStack(SqStack* S) {
  int i;
  for (i = S->top; i >= 0; i--) {
    printf("车牌号为%d", S->CarNo[i]);
    printf("\n");
  }
}

void InitQueue(SqQueue* Q) {
  Q = (SqQueue*)malloc(sizeof(SqQueue));
  Q->front = Q->rear = 0;
}

int QueueEmpty(SqQueue* Q) {
  return (Q->front = Q->rear);
}

int QueueFull(SqQueue* Q) {
  return ((Q->rear + 1) % M == Q->front);
}

int EnQueue(SqQueue* Q, int e) {
  if ((Q->rear + 1) % M == Q->front)
    return 0;
  Q->rear = (Q->rear + 1) % M;
  Q->CarNo[Q->rear] = e;
  return 1;
}

int DeQueue(SqQueue* Q, int* e) {
  if (Q->front == Q->rear)
    return 0;
  Q->front = (Q->front + 1) % M;
  *e = Q->CarNo[Q->front];
  return 1;
}

void DisQueue(SqQueue* Q) {
  int i;
  i = (Q->front + 1) % M;
  printf("%d", Q->CarNo[i]);
  while ((Q->rear - i + M) % M > 0) {
    i = (i + 1) % M;
    printf("%d", Q->CarNo[i]);
  }
  printf("\n");
}

【第3.4题解答】

运行结果截图:

源代码:

main.cpp

#include "Chapter3_4.h"
int main() {
  printf(" \n\n                    \n\n");
  printf("  ***************************************************\n\n");
  printf("  *                简易柜台模拟系统                *\n \n");
  printf("  ***************************************************\n\n");
  Quene S1, S2, S3, S4, S5, S6;
  InitQueue(S1);
  InitQueue(S2);
  InitQueue(S3);
  InitQueue(S4);
  InitQueue(S5);
  InitQueue(S6);
  int num;
  while (1) {
	WelcomePrint();
	scanf_s("%d", &num);
	getchar();
	switch (num) {
	case 1: {
	  int k;
	  Choice();
	  scanf_s("%d", &k);
	  getchar();
	  char ch;
	  if (k == 1) {
		printf("请输入要插入的姓名:\n");
		scanf_s("%c", &ch, 1);
		Encycque(&S1, ch);
	  }
	  else if (k == 2) {
		printf("请输入要插入的姓名:\n");
		scanf_s("%c", &ch, 1);
		if (QueueEmpty(S2)) {
		  Encycque(&S2, ch);
		}
		else if (QueueEmpty(S3)) {
		  Encycque(&S3, ch);
		}
		else if (QueueEmpty(S4)) {
		  Encycque(&S4, ch);
		}
		else {
		  Encycque(&S2, ch);
		}

	  }
	  else if (k == 3) {
		printf("请输入要插入的姓名:\n");
		scanf_s("%c", &ch, 1);
		if (QueueEmpty(S5)) {
		  Encycque(&S5, ch);
		}
		else if (QueueEmpty(S6)) {
		  Encycque(&S6, ch);
		}
		else if (QueueEmpty(S4)) {
		  Encycque(&S4, ch);
		}
	  }
	  break;
	}
	case 2:{
	  int j;
	  printf("请输入完成的柜台号:\n");
	  scanf_s("%d", &j);

	  switch (j) {
	  case 1:
		Delcycque(&S1);
		break;
	  case 2:
		Delcycque(&S2);
		break;
	  case 3:
		Delcycque(&S3);
		break;
	  case 4:
		Delcycque(&S4);
		break;
	  case 5:
		Delcycque(&S5);
		break;
	  case 6:
		Delcycque(&S6);
		break;
	  }
	  break;
	}
	case 3:
	  printf("1号柜台的排队状况:\n");
	  Traverseque(S1);
	  printf("2号柜台的排队状况:\n");
	  Traverseque(S2);
	  printf("3号柜台的排队状况:\n");
	  Traverseque(S3);
	  printf("4号柜台的排队状况:\n");
	  Traverseque(S4);
	  printf("5号柜台的排队状况:\n");
	  Traverseque(S5);
	  printf("6号柜台的排队状况:\n");
	  Traverseque(S6);
	  break;
	case 4:
	  exit(-1);
	}
  }
  return 0;
}

Chapter3_4.h

#pragma once
#pragma once
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<malloc.h>
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
#define MAXQSIZE 5 
typedef int ElemType;
typedef int Status;

typedef struct {
  ElemType quene[MAXQSIZE];
  int front, rear;
}Quene;

Status Initquene(Quene* Q);
Status Delcycque(Quene* Q);
Status Encycque(Quene* Q, ElemType x);
Status Traverseque(Quene Q);

Chapter3_4.c

#include "Chapter3_4.h"
Status Initquene(Quene* Q) {
  Q->front = 0;
  Q->rear = 0;
  return OK;
}

Status Delcycque(Quene* Q, ElemType* e) {
  if (Q->front == Q->rear) {
	return(NULL);
  }
  else {
	*e = Q->quene[Q->front];
	Q->front = (Q->front + 1) % MAXQSIZE;
	return OK;
  }
}

Status Encycque(Quene* Q, ElemType x) {
  if ((Q->rear + 1) % MAXQSIZE == Q->front) {
	return ERROR;
  }
  else {
	Q->quene[Q->rear] = x;
	Q->rear = (Q->rear + 1) % MAXQSIZE;
	return OK;
  }
}

Status Traverseque(Quene Q) {
  for (int i = 0; i < 4; i++) {
	printf("第%d个元素是:%d\n", i + 1, Q.quene[i]);
  }
  return OK;
}

【第3.5题解答】

解题思路:

根据f0=f1=f2=0, f3= 1,建立递推公式: fi= fi-1+fi-2+fi-3+fi-4,利用队列的增加、删除等基本操作当有元素进入队列时,如果尾指针不指向最后一个元素,元素直接插入,否则,利用Q->rear = (Q->rear + 1) % MAXSIZE进行循环队列的操作。

实验结果截图:

源代码:

main.cpp

#include "SqQuence.h"
int main()
{
  SqQueue* Q = InitQueue();
  //往队列中插入前四个元素
  EnQueue(Q, 0);
  EnQueue(Q, 0);
  EnQueue(Q, 0);
  EnQueue(Q, 1);
  int a[5] = { 0,0,0,1 };
  int count = 4;
  for (int i = 0; i < 4; i++) {
    printf("f%d:%d\n", i, a[i]);
  }
  int temp = 0;
  while (temp <= 200) {//当fn≤200而fn+1>200,结束循坏
    count++;
    temp = Q->base[0] + Q->base[1] + Q->base[2] + Q->base[3];
    printf("f%d: %d\n", count, temp);
    Delete(Q);
    EnQueue(Q, temp);
  }
  return 0;
}


SqQuence.h
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 4//修改这里可以改变队列的阶数
//队列的数据类型定义
typedef struct {
  int* base;
  int front;//头指针
  int rear;//尾指针
  int length;//队列的长度
}SqQueue;
SqQueue* InitQueue();//初始化一个队列
void EnQueue(SqQueue* Q, int e);//往队列中插入元素
int Delete(SqQueue* Q);//删除队列中第一个元素
int Fib_4(SqQueue* Q, int n);//利用斐波那契递推式计算函数值

SqQuence.cpp

#include "SqQuence.h"
SqQueue* InitQueue() {
  SqQueue* Q = (SqQueue*)malloc(sizeof(SqQueue));//动态分配内存空间
  Q->base = (int*)malloc(sizeof(int) * MAXSIZE);
  if (!Q->base)
    exit(0);
  Q->front = 0;
  Q->rear = 0;
  Q->length = 0;
  return Q;
}
void EnQueue(SqQueue* Q, int e) {
  if (Q->length >= MAXSIZE) return; //队列已满
  Q->base[Q->rear] = e;
  Q->rear = (Q->rear + 1) % MAXSIZE;
  Q->length++;
}

int Delete(SqQueue* Q) {
  if (Q->length == 0) return 0;//队列为空
  int e = Q->base[Q->front];
  Q->front = (Q->front + 1) % MAXSIZE;
  Q->length--;
  return e;
}

int Fib_4(SqQueue* Q, int n) {
  if (n < 0) return -1;
  if (n < 3) return 0;
  if (n == 3) return 1;
  int cnt = 1;
  int temp = 0;//临时存储利用递推公式算的值
  while (cnt <= n - 3) {
    temp = Q->base[0] + Q->base[1] + Q->base[2] + Q->base[3];
    Delete(Q);//删除队列中的第一个元素
    EnQueue(Q, temp);//插入临时存储的值
    cnt++;
  }
  return temp;
}

【第3.6题解答】

解题思路:

  每行先假设一个位置,然后检验是否和前面的位置有不合规则的,即下一个棋子在前一个棋子的同行、同列、对角线上,如果不合规则,则换一个位置检验,如果无法继续进行下一行的检验操作,就达到了8种输出结果。

 源代码:

#include <stdio.h>
#include <math.h>
int queenpos[8];//棋盘上有八个位置
int num = 1;
void Queen(int k);
int main()
{
  Queen(0);//从0开始,棋子第一个点为(0,0)
  return 0;
}

void Queen(int k) {
  int i, j;
  if (k == 8) {
    //所有情况均已摆放,可以输出
    printf("Case %d:\n", num++);
    for (i = 0; i < 8; i++) {
      printf("第%d行的棋子位置:%d\n", i + 1, queenpos[i]);
    }
    printf("\n");
  }
  else {
    //摆放第k行
    for (i = 0; i < 8; i++)
    {
      //确定列的数字
      for (j = 0; j < k; j++) {
        //对k行之前进行重复判断
        if (queenpos[j] == i || abs(queenpos[j] - i) == (k - j) ){
          break;
        }
      }
      if (j == k) {
        queenpos[k]=i;
          Queen(k+1);
      }
    }
  }
}

【第3.7题解答】

解题思路:

若当前位置“可通”,则纳入路径,继续前进;若当前位置“不可通”,则后退,换方向继续探索;四周“均无通路”,则将当前位置从路径中删除出去。

实验结果截图:

(1)迷宫无通路的情况

 (2)迷宫有通路的情况

 源代码:

main.cpp

#include "Chapter3_7.h"
int main() {
  MazeType maze;
  int row, col,i,j;
  printf("请输入迷宫的行数和列数,空格隔开:");
  scanf_s("%d %d", &row, &col);
  printf("请输入迷宫,0代表通路,1代表障碍:\n");
  int b[100][100];
  for (int i = 0; i < row; i++) {
    for (j = 0; j < col; j++) {
      scanf_s("%d", &b[i][j]);
    }
  }
  InitMaze(&maze, b, row, col);
  PosType Start, End;
  printf("请输入入口的坐标点,用空格隔开:");
  scanf_s("%d %d", &Start.x, &Start.y);
  printf("请输入出口的坐标点,用空格隔开:");
  scanf_s("%d %d", &End.x, &End.y);
  printf("开始的迷宫为:\n");
  PrintMaze(maze, row, col);
  if (MazePath(&maze, Start, End)) {
    PrintMaze(maze, row, col);
  }
  else {
    printf("该迷宫没有出路");
  }
  return 0;
}

Chapter 3_7.h

#pragma once 
#include <stdio.h>
#include <stdlib.h>
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#define _CRT_SECURE_NO_WARNINGS
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define TRUE 1
#define FALSE 0
typedef int Status;
typedef struct {
  int x, y;//点的坐标
}PosType;

typedef struct {
  PosType seat;//当前的坐标
  int direction;//运动的方向
}SElemType;
typedef struct {
  SElemType* base;
  SElemType* top;
  int stacksize;
}SqStack;

typedef struct {
  char arr[11][10];
}MazeType;

Status InitStack(SqStack* S);//初始化一个栈来存放走过的路
Status Push(SqStack* S, SElemType e);//将走得通的方向压入栈
Status Pop(SqStack* S, SElemType* e);//走不通,删除该方向
Status StackEmpty(SqStack S);//判断栈是否为空
Status GetTop(SqStack S, SElemType* e);//获取栈顶元素
Status StackClear(SqStack* S);
//用户自定义初始化一个迷宫
Status InitMaze(MazeType* maze, int(*a)[100], int row, int col);
int MazePath(MazeType* maze, PosType start, PosType end);
int Pass(MazeType maze, PosType curpos);//如果该方向能通过,返回1
int Same(PosType pos1, PosType pos2);//
PosType NextPos(PosType curpos, int d);//判断下一个坐标
Status MarkPrint(MazeType* maze, PosType pos);//标记走过的路
Status FootPrint(MazeType* maze, PosType pos);
Status PrintMaze(MazeType maze, int row, int col);//打印输出

Chapter 3_7.cpp

#include "Chapter3_7.h"
Status InitStack(SqStack* S) {
  S->base = (SElemType*)malloc(STACK_INIT_SIZE * sizeof(SElemType));
  if (!S->base) {
    exit(-1);
  }
  S->top = S->base;
  S->stacksize = STACK_INIT_SIZE;
}

Status Push(SqStack* S, SElemType e) {
  if (S->top - S->base >= S->stacksize) {
    S->base = (SElemType*)realloc(S->base, (S->stacksize + STACKINCREMENT)
      * sizeof(SElemType));
    if (!S->base) {
      exit(-1);
    }
    S->top = S->base + S->stacksize;
    S->stacksize += STACKINCREMENT;
  }
  *S->top++ = e;
  return OK;
}

Status Pop(SqStack* S, SElemType* e) {
  if (S->top == S->base) exit(-1);
  *e = *(--S->top);
  return OK;
}

Status StackEmpty(SqStack S) {
  if (S.top != S.base) return FALSE;
  else return TRUE;
}

Status GetTop(SqStack S, SElemType* e) {
  if (S.top == S.base) return FALSE;
  *e = *(S.top - 1);
  return OK;
}

Status StackClear(SqStack* S) {
  S->top = S->base;
  return OK;
}

//用户自定义输入一个迷宫
Status InitMaze(MazeType* maze, int(*a)[100], int row, int col) {
  int i, j;
  for (i = 0; i < (row + 2); i++) {//迷宫四周围上围墙
    maze->arr[i][0] = '#';
    maze->arr[i][row] = '#';
  }
  for (j = 0; j < (col + 1); j++) {
    maze->arr[0][j] = '#';
    maze->arr[10][j] = '#';
  }
  for (i = 1; i < (row + 1); i++) {
    for (j = 1; j < (col + 1); j++) {
      if (a[i - 1][j - 1] == 1) {//如果输入为1,标记为障碍
        maze->arr[i][j] = '#';
      }
      else {
        maze->arr[i][j] = '&';//如果输入为0,标记为通路
      }
    }
  }
  return OK;
}

int MazePath(MazeType* maze, PosType start, PosType end) {
  SqStack S;
  InitStack(&S);
  SElemType e ;
  PosType curpos = start;
  int curstep = 1;
  int found = FALSE;
  do {
    if (Pass(*maze, curpos)) {
      //返回值为1,当前位置可以走通
      e = { curpos,1 };
      Push(&S, e);
      FootPrint(maze, curpos);
      if (Same(curpos, end)) found = TRUE;//出口
      else {
        //下一个点
        curpos = NextPos(curpos, 1);
      }
      curstep++;
    }
    else {
      //当前位置不通过
      if (!StackEmpty(S)) {
        Pop(&S, &e);//退出当前失败的结点
        while ((e.direction == 4) && !StackEmpty(S)) {
          //回溯到过去不能用的点
          MarkPrint(maze, e.seat);//标记退格的结点
          Pop(&S, &e);
          curstep--;
        }
      }
      if (e.direction < 4) {
        e.direction++;
        Push(&S, e);//试试没有走完方向的点
        curpos = NextPos(e.seat, e.direction);
      }
    }
  }while (!StackEmpty(S) && !found);
  return found;
}

int Pass(MazeType maze, PosType curpos) {
  //检测当前位置能否走通
  if (maze.arr[curpos.x][curpos.y] != '#' &&
    maze.arr[curpos.x][curpos.y] != '*' &&
    maze.arr[curpos.x][curpos.y] != '@') {
    return 1;
  }
  else
    return 0;
}

int Same(PosType pos1, PosType pos2) {
  //检测两个点是否一致
  if (pos1.x == pos2.x && pos1.y == pos2.y) {
    return 1;
  }
  else
    return 0;
}

PosType NextPos(PosType curpos, int d) {
  //给出当前位置和方向,输出下一个点的坐标
  PosType k = { 0,0 };
  if (d == 1) {
    k.x = curpos.x;
    k.y = curpos.y+1;//向东走
  }
  else if (d == 2) {
    k.x = curpos.x + 1;//向南走
    k.y = curpos.y;
  }
  else if (d == 3) {
    k.x = curpos.x;
    k.y = curpos.y - 1;//向西走
  }
  else if (d == 4) {
    k.x = curpos.x - 1;//向北走
    k.y = curpos.y;
  }
  return k;
}

Status MarkPrint(MazeType* maze, PosType pos) {
  //对走不通的路进行标记
  maze->arr[pos.x][pos.y] = '@';
  return OK;
}

Status FootPrint(MazeType* maze, PosType pos) {
  //对通路进行标记
  maze->arr[pos.x][pos.y] = '*';
  return OK;
}

Status PrintMaze(MazeType maze, int row, int col) {
  //打印任意规模大小的迷宫
  printf("迷宫的通路为:用*表示\n");
  int i, j;
  for (i = 0; i < (row + 2); i++) {
    for (j = 0; j < (col + 2); j++) {
      printf("%c ", maze.arr[i][j]);
    }
    printf("\n");
  }
  return OK;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值