穿越迷宫c语言程序设计教程课后答案,请大虾们帮忙把这个迷宫的C++代码改为C语言...

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

这个程序代码分了三个文件,我想把它揉合为一个C语言文件

可是面向对象类不知道怎么改成C语言

//maze.h文件

// Maze类的声明

#ifndef MAZE_H_

#define MAZE_H_

#include

#include

#include

using namespace std;

class Maze

{

public:

Maze();               // 构造函数

void solve();  // 迷宫求解函数

private:

vector cells;

// 用于存储迷宫的各处状态

int rows, columns;

// 迷宫的行数、列数

bool exitMaze( int row, int column);

// 从位置(row,column)出发,穿越迷宫,成功返回true

bool valid( int row, int column);

// 用于判断位置(row,column)是否可通行,是返回true

};

#endif

//maze.cpp文件

// Maze 类的定义

#include "Maze.h"

// 构造函数,对迷宫的状态进行初始化

Maze::Maze()

{

cout <

cout <

// 读入迷宫的状态

string cellstr;

cin >> cellstr;

while (cellstr != "ok")

{

cells.push_back(cellstr);

columns = cellstr.size();

// 迷宫阵的总列数

cin >> cellstr;

}

rows = cells.size();

// 迷宫阵的总行数

}

// 迷宫求解函数

void Maze::solve()

{

int row, column;

row = column = 0;

// 设置出发点为(0,0)位置

bool done = exitMaze( row, column);

// 穿越成功,显示穿越路径,否则报道迷宫无法穿越

if (done)

{

cout <

for (int i = 0; i 

{

for (int j = 0; j 

cout <

cout <

}// end for

}// end if

else

cout <

}

// 从位置(row,column)出发,穿越迷宫,成功返回true

bool Maze::exitMaze( int row, int column)

{

bool done = false;

// 如果位置(row,column)可通行,标记之,并依次向四周搜索着前进

if ( valid(row, column) )

{

// 走过的位置标记为'B'

cells[row][column] = 'B';

// 抵达终点时表示穿越完成

if ( row == (rows - 1) && column == (columns - 1) )

done = true;

else

{

// 未到达终点前,首先选择向右走

done = exitMaze ( row , column + 1);

// 右走失败,转向下走

if (!done)

done = exitMaze ( row + 1, column);

// 右走、下走失败,转向左走

if (!done)

done = exitMaze ( row, column - 1);

// 右走、下走、左走均失败,转向上走

if (!done)

done = exitMaze ( row - 1, column);

}

if (done)

cells[row][column] = 'P';

// 正确的路径标记为'P'

}

return(done);

}

// 判断位置(row,column)是否可通行,是则返回true

bool Maze::valid( int row, int column)

{

bool path = false;

// 如果位置(row,column)在迷宫内,且该处可通行

if ( row >= 0 && row = 0

&& column 

path = true;

return path;

}

//maze_main.cpp文件

// 迷宫模拟Maze类的测试函数

#include "Maze.h"

int main()

{

cout <

Maze MazeExc;

MazeExc.solve();

system("pause");

return 0;

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值