自己根据解迷宫程序编写 一笔画 的路线,栈的编写出问题了

Youth1996 2017-07-03 08:00:24
问题来源于LYNE这个游戏。
要求:三种颜色的线,三笔画完。可以横竖斜画,中间某些特殊符号(交点)可以多次路过(三种颜色都可以走)。路线除了交点处,其他不可交叉。
引用
#define xsize 2
#define ysize 3
#define OVERFLOW -2
#define ERROR 0
#define NULL 0
#define true 1
#define TRUE 1
#define false 0
#define FALSE 0
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10

#include <string.h>
#include <stdlib.h>
#include <stdio.h>

struct{
int color; //所属类型 0通用;1,2,3正常;4,5,6,开始;7,8,9结束。
int x; //横向坐标
int y; //纵向坐标
int val;//对象的值
}num[xsize][ysize];

//定义栈元素类型
typedef struct LStackElem
{
int x;//x坐标
int y;//y坐标
int val;//值
}LStackElem;


//定义栈
typedef struct {
LStackElem * base;
LStackElem * top;
int stackSize;
}LStack;


//初始化栈-------构造一个空栈
void initStack(LStack *s) {
s->base = (LStackElem *)malloc(STACK_INIT_SIZE * sizeof(LStackElem));
if (!s->base) {
printf("in initStack()...Failed to initalize the LStack ,no enough space! exit now. ");
exit(OVERFLOW);//存储分配失败
}
s->top = s->base;
s->stackSize = STACK_INIT_SIZE;
}


//向栈中添加元素
void push(LStack *s,LStackElem e) {
//向栈中添加元素前先判断栈是否还有空间容纳新元素
if (s->top - s->base >= s->stackSize) { //栈满,追加元素
s->base = (LStackElem *)realloc(s->base, (STACK_INIT_SIZE+STACKINCREMENT) * sizeof(LStackElem));
if (!s->base) {
printf("in push()...Failed to realloc the LStack ,no enough space! exit now. ");
exit(OVERFLOW);//存储分配失败
}
s->top = s->base + s->stackSize; //因为是重新分配了空间,所以base的值其实已经改变,所以top的值也就相应的改变,才能指向新的迷宫栈
s->stackSize += STACKINCREMENT;
}
//将新元素加到栈顶
*(s->top++) = e;
}

//获得栈顶元素
LStackElem getTop(LStack *s) {
if (s->top == s->base) {
printf("in getTop(),empty stack! exit now. ");
exit(ERROR);
}
else {
return *(s->top - 1);
}
}

//删除栈顶元素
void pop(LStack *s) {
//若栈不为空,则删除s的栈顶元素
if (s->top == s->base) {
printf("in pop(),empty stack! exit now. ");
exit(ERROR);
}
else {
--(s->top);
}
}

LStack realPath,path;

//判断当前位置是否走过
int unPass(LStack path,LStackElem cur) {//这里不能传path的地址,否则在遍历过程中它的top值就真的被改了 !!
int flag = 1;
while(path.top != path.base)
{
LStackElem e = *(path.top - 1);
if (e.x == cur.x&& e.y == cur.y)
{
flag = 0;
}
//每循环一次令头指针下移一个位置
(path.top)--;
}
return flag;
}


int i, j, n, data;
char c;
int shuru()
{
for(j=0; j<xsize; j++)
{
for(i=0; i<ysize; i++)
{
putchar('-');
}
putchar('\n');

}


for(j=0; j<xsize; j++)
{
for(i=0; i<ysize; i++)
{
scanf("%d", &data);
num[i][j].color=data%10;
data /= 10;
num[i][j].y=data%10;
data /= 10;
num[i][j].x=data%10;

if(num[i][j].color==0)
{
data /= 10;
num[i][j].val=data%10;
}
else
{
num[i][j].val=1;
}

printf("%d%d%d%d\n", num[i][j].x, num[i][j].y, num[i][j].color, num[i][j].val);
}

}


}

//获得东北面相邻的位置
LStackElem getEN(LStackElem cur) {
if(cur.x != 0&&cur.x != xsize-1) {
cur.x -= 1;
cur.y += 1;
cur.val = num[cur.x][cur.y].val;
}
return cur;
}
//获得东面相邻的位置
LStackElem getE(LStackElem cur) {
if(cur.y != xsize-1) {
cur.y += 1;
cur.val = num[cur.x][cur.y].val;
}
return cur;
}
//获得东南面相邻的位置
LStackElem getES(LStackElem cur) {
if(cur.x != xsize-1&&cur.y != ysize-1) {
cur.x += 1;
cur.y += 1;
cur.val = num[cur.x][cur.y].val;
}
return cur;
}
//获得南面相邻的位置
LStackElem getS(LStackElem cur) {
if(cur.x != ysize-1) {
cur.x += 1;
cur.val = num[cur.x][cur.y].val;
}
return cur;
}
//获得西南面相邻的位置
LStackElem getWS(LStackElem cur) {
if(cur.x != ysize-1&&cur.y != 0) {
cur.x += 1;
cur.y -= 1;
cur.val = num[cur.x][cur.y].val;
}
return cur;
}
//获得西面相邻的位置
LStackElem getW(LStackElem cur) {
if(cur.y != 0) {
cur.y -= 1;
cur.val = num[cur.x][cur.y].val;
}
return cur;
}
//获得西北面相邻的位置
LStackElem getWN(LStackElem cur) {
if(cur.x != 0&&cur.y != 0) {
cur.x -= 1;
cur.y -= 1;
cur.val = num[cur.x][cur.y].val;
}
return cur;
}
//获得北面相邻的位置
LStackElem getN(LStackElem cur) {
if(cur.x != 0) {
cur.x -= 1;
cur.val = num[cur.x][cur.y].val;
}
return cur;
}

//获得下一个可通行的位置,按东南西北的方向试探,!!!!!!只写了四个方向,没写完。
LStackElem getNext(LStackElem cur) {
LStackElem next;
next.x = next.y=next.val = -1;
if(getE(cur).val != 0 && unPass(path,getE(cur))) {
next = getE(cur);
}
else if(getS(cur).val != 0 && unPass(path,getS(cur))) {
next = getS(cur);
}
else if(getW(cur).val != 0 && unPass(path,getW(cur))) {
next = getW(cur);
}
else if(getN(cur).val != 0 && unPass(path,getN(cur))) {
next = getN(cur);
}
//如果当前位置的四面或为墙或已走过,则返回的next的val值为-1
return next;
}

int getMazePath(){//获得LYNE路径的函数

LStackElem start,end,cur;
start.x = 0;
start.y = 0;
start.val = num[start.x][start.y].val;
end.x = xsize-1;
end.y = ysize-1;
end.val = num[end.x][end.y].val;

cur = start;

do
{
if (unPass(path,cur)) {//如果当前位置未曾走到过
push(&realPath,cur);
push(&path,cur);
cur = getNext(cur);
if (cur.x == end.x && cur.y == end.y) { //到达出口了,则跳出循环,并返回true
//把出口结点放入路径中
push(&realPath,cur);
push(&path,cur);
//直接跳出函数(而不只是跳出这个循环 )
return true;
}
else if(cur.val == -1) {//当前位置的四面或为墙或已走过
//删除真实路径的栈顶元素
pop(&realPath);
cur = getTop(&realPath);//令cur指向栈顶元素
}
}
else {//如果当前位置已经走过,说明原来测试的方向不对,现在尝试其它方向
cur = getNext(cur);
if (cur.val == -1) {//仍不通,删除真实路径的栈顶元素
pop(&realPath);
cur = getTop(&realPath);//令cur指向栈顶元素
}
}
} while (cur.x != end.x || cur.y != end.y);
}

//打印迷宫路径
void printMazePath(LStack *s) {
LStackElem e;
while (s->base < (s->top-1)) {
e = *(s->base);//先指向栈底元素,以后依次向上增1
printf("(%d,%d)→",e.x+1,e.y+1);
(s->base)++;
}

e = *(s->base);
printf("(%d,%d)",e.x+1,e.y+1);
}

main(){
//初始化栈
initStack(&realPath);
initStack(&path);
getMazePath();
shuru();
printf("LYNE的路径是:\n\n");
printMazePath(&realPath);
getchar();
}


其中输入为3位或者4位,3位分别为x,y,点的种类文中(为color),因为这个默认的点只能走一次,color为0,即可以走多次的点,此时输入4位数,分别为:可以通过该点的次数,x,y,color(0)。
现在想要简单运行一下 栈出问题了。
例如输入 111 122 131 211 221 231 应该出现(1,1)到(2,1)到(2,2)到(2,3)这条路线。
...全文
170 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2017-07-05
  • 打赏
  • 举报
回复
仅供参考:
#pragma comment(lib,"user32")
#pragma comment(lib,"gdi32")
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
HWND WINAPI GetConsoleWindow();
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);
    }
}
int main() {
    HWND  hwnd;
    HDC   hdc;
    HFONT hfont;

    system("color F0");
    system("cls");
    HideTheCursor();
    hwnd  = GetConsoleWindow();
    hdc   = GetDC(hwnd);
    hfont = CreateFont(48, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "华文楷体");
    SelectObject(hdc,hfont);
    TextOut(hdc,10,10,"地球人都知道!",14);
    MoveToEx(hdc,5,5,NULL);
    LineTo(hdc,300,  5);
    LineTo(hdc,300, 60);
    LineTo(hdc,  5, 60);
    LineTo(hdc,  5,  5);
    DeleteObject(hfont);
    ReleaseDC(hwnd,hdc);
    getchar();
    system("color 07");
    system("cls");
    ShowTheCursor();
    return 0;
}
赵4老师 2017-07-05
  • 打赏
  • 举报
回复
代码功能归根结底不是别人帮自己看或讲解或注释出来的;而是被自己静下心来花足够长的时间和精力亲自动手单步或设断点或对执行到某步获得的中间结果显示或写到日志文件中一步一步分析出来的。 提醒:再牛×的老师也无法代替学生自己领悟和上厕所! 单步调试和设断点调试(VS IDE中编译连接通过以后,按F10或F11键单步执行,按Shift+F11退出当前函数;在某行按F9设断点后按F5执行停在该断点处。)是程序员必须掌握的技能之一。
赵4老师 2017-07-05
  • 打赏
  • 举报
回复
Youth1996 2017-07-05
  • 打赏
  • 举报
回复
引用 5 楼 zhao4zhong1 的回复:
仅供参考:
#pragma comment(lib,"user32")
#pragma comment(lib,"gdi32")
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
HWND WINAPI GetConsoleWindow();
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);
    }
}
int main() {
    HWND  hwnd;
    HDC   hdc;
    HFONT hfont;

    system("color F0");
    system("cls");
    HideTheCursor();
    hwnd  = GetConsoleWindow();
    hdc   = GetDC(hwnd);
    hfont = CreateFont(48, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "华文楷体");
    SelectObject(hdc,hfont);
    TextOut(hdc,10,10,"地球人都知道!",14);
    MoveToEx(hdc,5,5,NULL);
    LineTo(hdc,300,  5);
    LineTo(hdc,300, 60);
    LineTo(hdc,  5, 60);
    LineTo(hdc,  5,  5);
    DeleteObject(hfont);
    ReleaseDC(hwnd,hdc);
    getchar();
    system("color 07");
    system("cls");
    ShowTheCursor();
    return 0;
}
好吧,赵老师,还是不太懂。
赵4老师 2017-07-04
  • 打赏
  • 举报
回复
仅供参考:
/**
 * @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;
}

Youth1996 2017-07-04
  • 打赏
  • 举报
回复
#define OVERFLOW -2
#define ERROR 0
#define NULL 0
#define true 1
#define TRUE 1
#define false 0
#define FALSE 0
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10

#include <stdio.h>   
#include <stdlib.h>   
/*
初始化迷宫,1表示通道,0表示墙
*/
int maze[8][8] = {
    1,1,0,1,1,1,0,1,
    1,1,0,1,1,1,0,1,
    1,1,1,1,0,0,1,1,
    1,0,0,0,1,1,1,1,
    1,1,1,0,1,1,1,1,
    1,0,1,1,1,0,1,1,
    1,0,0,0,1,0,0,1,
    0,1,1,1,1,1,1,1
};


//定义栈元素类型 
typedef struct MStackElem
{
    int x;//x坐标
    int y;//y坐标
    int val;//maze[x][y]的值
}MStackElem;


//定义栈
typedef struct {
    MStackElem * base;
    MStackElem * top;
    int stackSize;
}MStack;

//=============Stack的实现========================

//初始化栈-------构造一个空栈
void initStack(MStack *s) {
    s->base = (MStackElem *)malloc(STACK_INIT_SIZE * sizeof(MStackElem));
    if (!s->base) {
       printf("in initStack()...Failed to initalize the MStack ,no enough space! exit now. ");
       exit(OVERFLOW);//存储分配失败
    }
    s->top = s->base;
    s->stackSize = STACK_INIT_SIZE;
}


//向栈中添加元素
void push(MStack *s,MStackElem e) {
     //向栈中添加元素前先判断栈是否还有空间容纳新元素
    if (s->top - s->base >= s->stackSize) { //栈满,追加元素
       s->base = (MStackElem *)realloc(s->base, (STACK_INIT_SIZE+STACKINCREMENT) * sizeof(MStackElem));
       if (!s->base) {
        printf("in push()...Failed to realloc the MStack ,no enough space! exit now. ");
        exit(OVERFLOW);//存储分配失败
       }
       s->top = s->base + s->stackSize; //因为是重新分配了空间,所以base的值其实已经改变,所以top的值也就相应的改变,才能指向新的迷宫栈 
       s->stackSize += STACKINCREMENT;
    }
    //将新元素加到栈顶 
    *(s->top++) = e;
}

//获得栈顶元素
MStackElem getTop(MStack *s) {
if (s->top == s->base) {
   printf("in getTop(),empty stack! exit now. ");
   exit(ERROR);
}
else {
   return *(s->top - 1);
}
}

//删除栈顶元素
void pop(MStack *s) {
//若栈不为空,则删除s的栈顶元素
    if (s->top == s->base) {
       printf("in pop(),empty stack! exit now. ");
       exit(ERROR);
    }
    else {
       --(s->top);
    }
}


//=====================================求解迷宫的相关操作=====================//
//构造两个栈,一个用来保存探索中的全部路径,一个用来保存有效路径
MStack realPath,path;

//判断当前位置是否走过
int unPass(MStack path,MStackElem cur) {//这里不能传path的地址,否则在遍历过程中它的top值就真的被改了 !! 
    int flag = 1;
    while(path.top != path.base)
    {
       MStackElem e = *(path.top - 1);
       if (e.x == cur.x&& e.y == cur.y)
       {
        flag = 0;
       }
       //每循环一次令头指针下移一个位置 
       (path.top)--;
    }
    return flag;
}

//获得东面相邻的位置
MStackElem getEast(MStackElem cur) {
           if(cur.y != 7) {//当y==7时已到了迷宫右边界,不能再向东(右)行了 
                cur.y += 1;
                cur.val = maze[cur.x][cur.y];
           } 
           return cur;// 当y==7时返回的是它本身 
} 
//获得南面相邻的位置
MStackElem getSouth(MStackElem cur) {
           if(cur.x != 7) {//当x==7时已到了迷宫下边界,不能再向南(下)行了 
                cur.x += 1;
                cur.val = maze[cur.x][cur.y];
           }
           return cur;// 当x==7时返回的是它本身 
}
//获得西面相邻的位置
MStackElem getWest(MStackElem cur) {
           if(cur.y != 0) {//当y==0时已到了迷宫左边界,不能再向西(左)行了 
                cur.y -= 1;
                cur.val = maze[cur.x][cur.y];
           }
           return cur;// 当y==0时返回的是它本身 
}
//获得北面相邻的位置
MStackElem getNorth(MStackElem cur) {
     if(cur.x != 0) {//当cur.x==0时表示在迷宫的上边界,不能再向北(上)行了 
        cur.x -= 1;
        cur.val = maze[cur.x][cur.y];
     }
     return cur;// 当cur.x==0时返回的还是它本身 
}


//获得下一个可通行的位置,按东南西北的方向试探
MStackElem getNext(MStackElem cur) {
MStackElem next;
next.x = next.y=next.val = -1;
if(getEast(cur).val != 0 && unPass(path,getEast(cur))) {
   next = getEast(cur);
}
else if(getSouth(cur).val != 0 && unPass(path,getSouth(cur))) {
   next = getSouth(cur);
}
else if(getWest(cur).val != 0 && unPass(path,getWest(cur))) {
   next = getWest(cur);
}
else if(getNorth(cur).val != 0 && unPass(path,getNorth(cur))) {
   next = getNorth(cur);
}
//如果当前位置的四面或为墙或已走过,则返回的next的val值为-1 
return next;
}

int getMazePath(){//获得迷宫路径的函数

    MStackElem start,end,cur; 
    start.x = 0;
    start.y = 0;
    start.val = maze[start.x][start.y];
    end.x = 7;
    end.y = 7; 
    end.val = maze[end.x][end.y];
    
    cur = start; //设定当前为位置为"入口位置"
    //没有重载=运算符,可以这样用吗,结果对吗?试试吧:
    /*                                               
    printf("%d",cur.x);
    printf("%d",cur.y); 
    printf("%d",cur.val);                                                 
    */
    //恩,结果是正确的,即可以通过cur=start来把start的值赋给cur.
    //对了,想起来了,记得以前在学C++的复制构造函数时书上好像说过, 用=(等号)复制类对象时系统会调用由系统提供的复制的构造函数什么的,
    //唉,记不清了,还是基础不扎实啊,以后可不能轻视基础了@_@ ...
    
    do 
    {                                                  
        if (unPass(path,cur)) {//如果当前位置未曾走到过
            push(&realPath,cur);
            push(&path,cur);
            cur = getNext(cur);
            if (cur.x == end.x && cur.y == end.y) { //到达出口了,则跳出循环,并返回true 
               //把出口结点放入路径中 
                push(&realPath,cur);
                push(&path,cur);
                //直接跳出函数(而不只是跳出这个循环 ) 
                return true;
            }
            else if(cur.val == -1) {//当前位置的四面或为墙或已走过
                 //删除真实路径的栈顶元素
                 pop(&realPath);
                 cur = getTop(&realPath);//令cur指向栈顶元素
             }
        }
       else {//如果当前位置已经走过,说明原来测试的方向不对,现在尝试其它方向
            cur = getNext(cur);
            if (cur.val == -1) {//仍不通,删除真实路径的栈顶元素
               pop(&realPath);
               cur = getTop(&realPath);//令cur指向栈顶元素
            }
       }
    } while (cur.x != end.x || cur.y != end.y);
}

//打印迷宫路径
void printMazePath(MStack *s) {//为了安全,这里不传MStack的地址,以防在遍历的过程中把它们的top或base的值也修改了 
     /*注:这种方法实际是倒序打印出路径,让人看着别扭,所以我用下面的方法"倒序遍历这个栈 ' 
     while (s->top != s->base) {
       MStackElem e = *(s->top-1);
       printf("maze[%d][%d]----->",e.x,e.y);
       (s->top)--;
    }
    */
     MStackElem e;
     while (s->base < (s->top-1)) {
      e = *(s->base);//先指向栈底元素,以后依次向上增1 
       printf("(%d,%d)→",e.x+1,e.y+1);
       (s->base)++;
     }
     //最后一个结点没有后继,所以不再输出"------>" 
     e = *(s->base);
     printf("(%d,%d)",e.x+1,e.y+1);
}

main(){
      //初始化栈
    initStack(&realPath);
    initStack(&path); 
    getMazePath();
    printf("迷宫的路径是:\n\n");
    printMazePath(&realPath);
    getchar();
}
这是我找的迷宫的源代码,想做到类似https://tieba.baidu.com/p/4410994513的效果。
Youth1996 2017-07-04
  • 打赏
  • 举报
回复
引用 1 楼 zhao4zhong1 的回复:
仅供参考:
/**
 * @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;
}

https://tieba.baidu.com/p/4410994513 赵老师,想做到大概这样的效果,老鼠走迷宫这个出来没有具体的路线,从哪个坐标到哪个坐标,这个必须要用到栈吗?
Youth1996 2017-07-04
  • 打赏
  • 举报
回复
引用 1 楼 zhao4zhong1 的回复:
仅供参考:
/**
 * @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;
}

谢谢赵老师,我再研究研究

64,654

社区成员

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

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