数据结构课题项目有一个函数写不好,请大家帮帮我啊!

the_passenger 2015-07-15 03:12:57
大一学生暑假数据结构课题设计作业,求写其中一个函数,实现求路径的功能

题目如下:

课题22:(85分)程序开始运行时显示一个迷宫地图,迷宫中央有一只老鼠,迷宫的右下方有一个粮仓。游戏的任务是使用键盘上的方向键操纵老鼠在规定的时间内走到粮仓处。

要求:

1) 老鼠形象可辨认,可用键盘操纵老鼠上下左右移动;

2) 迷宫的墙足够结实,老鼠不能穿墙而过;

3) 正确检测结果,若老鼠在规定时间内走到粮仓处,提示成功,否则提示失败;

4) 添加编辑迷宫功能,可修改当前迷宫,修改内容:墙变路、路变墙;

5) 找出走出迷宫的所有路径,以及最短路径;

利用序列化功能实现迷宫地图文件的存盘和读出等功能。

我已经写好了绝大部分代码了。只是第5个功能,还没有实现。

我用了一个二维数组p[a][b]来代表迷宫地图,“0”表示墙壁,“1”表示路,“3”表示终点粮仓。

现在问题就变成了:在这个二维数组中求到达目标位置的所有路径和最短路径,当然,只能上下左右走。

我老师建议我创建一个名为infor的结构体,把每一个点的上下左右四个临接点的数据(‘0’还是’1‘)输入进去,然后再包含一个,该节点是否已经被遍历的self信息(为了防止回到自己,走死循环),然后利用递归的思想,再对下一个点的四个临接点进行遍历。求出路径。

我按照老师思想写的递归程序如下: //其中x和y代表位置坐标,q代表由infor结构体构成的数组(里面的四个邻接点信息已经全部输入进去了),p代表迷宫地图的二维数组,都是0和1构成。
void find_path(int x,int y,infor **q,int **p)
{
if(p[x][y]!=3) //递归出口
{
q[x][y].self=1; //打上“已被遍历”的标志


if(q[x][y].up==1&&q[x-1][y].self!=1) //判断:若当前位置上方可以走,并且上方点未被遍历
{find_path(x-1,y,q,p);}

if(q[x][y].down==1&&q[x+1][y].self!=1)
{find_path(x+1,y,q,p);}

if(q[x][y].left==1&&q[x][y-1].self!=1)
{find_path(x,y-1,q,p);}

if(q[x][y].right==1&&q[x][y+1].self!=1)
{find_path(x,y+1,q,p);}

cout<<'('<<x<<','<<y<<')'<<"<-"; //由于递归的特性,应该输出的是倒序。
}
}

程序只能输出我的初始坐标,(10,10)->。。然后就啥也没有了。

上个图



...全文
158 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2015-07-15
  • 打赏
  • 举报
回复
仅供参考:
/**
 * @Title  老鼠走迷宫的拓展探究
 * @Author 孙琨
 * @Date   2013-11-16
 * @At     XUST
 * @All Copyright by 孙琨
 *
 */

#include <iostream>
using namespace std;

int maze[9][9] = { // 初始化迷宫,英文maze为“迷宫”
    {2,2,2,2,2,2,2,2,2},
    {2,0,0,0,0,0,0,0,2},
    {2,0,2,2,0,2,2,0,2},
    {2,0,2,0,0,2,0,0,2},
    {2,0,2,0,2,0,2,0,2},
    {2,0,0,0,0,0,2,0,2},
    {2,2,0,2,2,0,2,2,2},
    {2,0,0,0,0,0,0,0,2},
    {2,2,2,2,2,2,2,2,2}
};

int startI = 1,startJ = 1; // 入口行列坐标
int endI = 7,endJ = 7;     // 出口行列坐标

void visit(int i,int j)  // 自动搜寻路径
{
    int m,n;

    maze[i][j] = 1;

    if((i == endI) && (j == endJ))
    {
        cout << endl << "显示路径:" << endl;
        for(m=0; m<9; m++)
        {
            for(n=0; n<9; n++)
            {
                if(maze[m][n] == 2)
                    cout << "■";
                else if(maze[m][n] == 1)
                    cout << "♀";
                else
                    cout << "  ";
            }
            cout << endl;
        }
    }

    if(maze[i][j+1] == 0)
        visit(i,j+1);
    if(maze[i+1][j] == 0)
        visit(i+1,j);
    if(maze[i][j-1] == 0)
        visit(i,j-1);
    if(maze[i-1][j] == 0)
        visit(i-1,j);

    maze[i][j] = 0;

}

int main(void)
{
    int i,j;

    cout << "显示迷宫: " << endl;
    for(i=0; i<9; i++)
    {
        for(j=0; j<9; j++)
        {
            if(maze[i][j] == 2)
                cout << "■" ;
            else
                cout << "  " ;
        }
        cout << endl;
    }

    visit(startI,startJ);

    return 0;
}

赵4老师 2015-07-15
  • 打赏
  • 举报
回复
仅供参考:
#include <conio.h>
#include <windows.h>

void ConPrintAt(int x, int y, char *CharBuffer, int len)
{
   DWORD count;
   COORD coord = {x, y};
   HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
   SetConsoleCursorPosition(hStdOut, coord);
   WriteConsole(hStdOut, CharBuffer, len, &count, NULL);
}
void HideTheCursor()
{
   CONSOLE_CURSOR_INFO cciCursor;
   HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);

   if(GetConsoleCursorInfo(hStdOut, &cciCursor))
   {
      cciCursor.bVisible = FALSE;
      SetConsoleCursorInfo(hStdOut, &cciCursor);
   }
}

void ShowTheCursor()
{
   CONSOLE_CURSOR_INFO cciCursor;
   HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);

   if(GetConsoleCursorInfo(hStdOut, &cciCursor))
   {
      cciCursor.bVisible = TRUE;
      SetConsoleCursorInfo(hStdOut, &cciCursor);
   }
}
void GetWH(int *w,int *h) {
    HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
    CONSOLE_SCREEN_BUFFER_INFO csbi;
    if (GetConsoleScreenBufferInfo(hStdOut, &csbi)) {
        *w=csbi.srWindow.Right;
        *h=csbi.srWindow.Bottom;
    } else {
        *w=80;
        *h=25;
    }
}
void ClearConsole()
{
   //Get the handle to the current output buffer...
   HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
   //This is used to reset the carat/cursor to the top left.
   COORD coord = {0, 0};
   //A return value... indicating how many chars were written
   //   not used but we need to capture this since it will be
   //   written anyway (passing NULL causes an access violation).
   DWORD count;
   //This is a structure containing all of the console info
   // it is used here to find the size of the console.
   CONSOLE_SCREEN_BUFFER_INFO csbi;
   //Here we will set the current color
   if(GetConsoleScreenBufferInfo(hStdOut, &csbi))
   {
      //This fills the buffer with a given character (in this case 32=space).
      FillConsoleOutputCharacter(hStdOut, (TCHAR) 32, csbi.dwSize.X * csbi.dwSize.Y, coord, &count);
      FillConsoleOutputAttribute(hStdOut, csbi.wAttributes, csbi.dwSize.X * csbi.dwSize.Y, coord, &count);
      //This will set our cursor position for the next print statement.
      SetConsoleCursorPosition(hStdOut, coord);
   }
}

int main() {
    unsigned short k;
    int x,y,w,h;

    SetConsoleOutputCP(437);
    ClearConsole();
    GetWH(&w,&h);
    x=w/2;y=h/2;
    HideTheCursor();
    ConPrintAt(x,y,"\xdb",1);
    while (1) {
        Sleep(50);
        k=getch();
        if (27==k) break;//按Esc键退出
        if (0==k||0xe0==k) k|=getch()<<8;//非字符键
        switch (k) {
            case 0x48e0:case 0x04800://上
                if (y>0) {
                    ConPrintAt(x,y," ",1);
                    y--;
                    ConPrintAt(x,y,"\xdb",1);
                }
            break;
            case 0x50e0:case 0x05000://下
                if (y<h) {
                    ConPrintAt(x,y," ",1);
                    y++;
                    ConPrintAt(x,y,"\xdb",1);
                }
            break;
            case 0x4be0:case 0x04b00://左
                if (x>0) {
                    ConPrintAt(x,y," ",1);
                    x--;
                    ConPrintAt(x,y,"\xdb",1);
                }
            break;
            case 0x4de0:case 0x04d00://右
                if (x<w-1) {
                    ConPrintAt(x,y," ",1);
                    x++;
                    ConPrintAt(x,y,"\xdb",1);
                }
            break;
        }
//      cprintf("%04x pressed.\r\n",k);

    }
    ClearConsole();
    ShowTheCursor();
    return 0;
}

65,210

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧