| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2752 人关注过本帖
标题:[求助]c++迷宫问题出错了,大佬求解答
只看楼主 加入收藏
return_0
Rank: 8Rank: 8
来 自:五维空间
等 级:禁止访问
威 望:3
帖 子:512
专家分:838
注 册:2020-1-28
结帖率:100%
收藏
已结贴  问题点数:30 回复次数:10 
[求助]c++迷宫问题出错了,大佬求解答
图片附件: 游客没有浏览图片的权限,请 登录注册

程序代码:
/*
    Name: 
    Copyright: 
    Author: 
    Date: 19-03-20 19:33
    Description: 
*/

#include<iostream>
using namespace std;
int n,m,xs,ys,xe,ye;
bool flag;
bool checksame(int a,int b){
    if(a==xe&&b==ye)return true;
        flag=true;
    return false;
}
char a[100][100];
int dirx[4]={-1,1,0,0};
int diry[4]={0,0,1,-1};
bool vis[100][100]={false};
void dfs(int x,int y){
    if(checksame(x,y)){
        return ;
    }
    for(int i=0;i<4;++i){
        int xx=x+dirx[i];
        int yy=y+diry[i];
        if(vis[xx][yy]!=true&&a[xx][yy]=='.'){
            vis[xx][yy]=true;
            dfs(xx+1,yy+1);
            vis[xx][yy]=false;
            cout<<xx<<" "<<yy<<endl; 
        }
    }
}
int main(){
    cin>>n>>m>>xs>>ys>>xe>>ye;
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            cin>>a[i][j];
        }
    }
    dfs(xs,ys);
    if(flag)cout<<"YES";
    else cout<<"NO";
    return 0;
}
搜索更多相关主题的帖子: false 迷宫 flag return int 
2020-03-19 19:58
return_0
Rank: 8Rank: 8
来 自:五维空间
等 级:禁止访问
威 望:3
帖 子:512
专家分:838
注 册:2020-1-28
收藏
得分:0 
这是深搜巩固,老师要求用深搜

2020-03-19 19:58
return_0
Rank: 8Rank: 8
来 自:五维空间
等 级:禁止访问
威 望:3
帖 子:512
专家分:838
注 册:2020-1-28
收藏
得分:0 
我已经对了

2020-03-19 20:30
return_0
Rank: 8Rank: 8
来 自:五维空间
等 级:禁止访问
威 望:3
帖 子:512
专家分:838
注 册:2020-1-28
收藏
得分:0 
大家就抢回复吧

2020-03-19 20:31
return_0
Rank: 8Rank: 8
来 自:五维空间
等 级:禁止访问
威 望:3
帖 子:512
专家分:838
注 册:2020-1-28
收藏
得分:0 
分也不好意思收回了

2020-03-19 20:31
wmf2014
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
等 级:贵宾
威 望:216
帖 子:2039
专家分:11273
注 册:2014-12-6
收藏
得分:10 
没有代码不好意思回复啊。
程序代码:
#include <stdio.h>
int dfs(char a[][6],int x,int y)
{
    int i,j,k,b[4][2]={-1,0,0,-1,1,0,0,1};
    if(x<0||y<0||x>4||y>4||a[x][y]=='#')return 0;
    if(a[x][y]=='E')return 1;
    k=a[x][y];
    a[x][y]='#';
    for(i=0;i<4;i++)
    {
        j=dfs(a,x+b[i][0],y+b[i][1]);
        if(j)break;
    }
    a[x][y]=k;
    return j;
}
void main()
{
    char a[5][6]={
        "S.###",
        "#.###",
        ".....",
        ".####",
        "#...E",      //把这一行的“#”换成“.”就显示Yes
    };
    if(dfs(a,0,0))printf("Yes\n");
    else printf("No\n");
}


[此贴子已经被作者于2020-3-19 21:32编辑过]


能编个毛线衣吗?
2020-03-19 20:36
lin5161678
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:45
帖 子:1136
专家分:3729
注 册:2011-12-3
收藏
得分:10 
dfs(xx+1,yy+1);
你这个+1操作毫无理由啊
xx yy 就是前进一步之后的位置
递归应该是从这个位置开始
你无缘无故 +1 变成斜着走一步
不管怎么解释都不对
应该dfs xx yy

https://zh.
2020-03-19 22:21
叶纤
Rank: 8Rank: 8
等 级:禁止访问
威 望:1
帖 子:658
专家分:848
注 册:2019-11-22
收藏
得分:10 
论坛即将出现一个大佬,大佬慷慨,大佬万岁,感谢大佬慷慨解囊

把学习时间浪费在混坛上是傻瓜行为,更何况自己的水平连一两都没到。
2020-03-20 00:30
return_0
Rank: 8Rank: 8
来 自:五维空间
等 级:禁止访问
威 望:3
帖 子:512
专家分:838
注 册:2020-1-28
收藏
得分:0 
回复 7楼 lin5161678
我说了我已经解决了~

2020-03-20 08:51
maomao12345
Rank: 2
来 自:五位神奇空间
等 级:禁止访问
威 望:2
帖 子:127
专家分:64
注 册:2020-3-23
收藏
得分:0 
#include<iostream>
#include<conio.h>
#include<cmath>
#include<Windows.h>
#include<ctime>
#include<cstdlib>
#include<stack>
#include<string>
using namespace std;
int xk[1001];
int yk[1001];
int t[10010];
bool key[100];
char maps[100][12][12];
int step;
int allstep;
void pg(){
    cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
}
int clr;
int x;
int y;
int xe;
int ye;
/*●■*/
void getmap(int level){
    pg();
    for(int i=0;i<12;++i){
        for(int j=0;j<12;++j){
            if(maps[level][i][j]=='w')cout<<"■";
            if(maps[level][i][j]=='f')cout<<"●";
            if(maps[level][i][j]=='e')cout<<"╳ ";
            if(maps[level][i][j]=='p')cout<<"♀";
            if(maps[level][i][j]=='.')cout<<"□";
        }
        cout<<"\n";
    }
    cout<<"Step count: "<<step<<"\n";
}
void setmap(int level){
    for(int i=0;i<12;++i){
        for(int j=0;j<12;++j){
            if(i==x&&j==y)maps[level][i][j]='p';
            else if(i==xe&&j==ye){
                maps[level][i][j]='e';
            }
            else if(i==xk[level]&&j==yk[level]){
                maps[level][i][j]='f';
            }
            else if(i==0||i==11||j==0||j==11){
                maps[level][i][j]='w';
            }
            else maps[level][i][j]='.';
        }
    }
}
void new_(int level){
    for(int i=0;i<12;++i){
        for(int j=0;j<12;++j){
            if(i==x&&j==y)maps[level][i][j]='p';
            else if(i==xe&&j==ye){
                maps[level][i][j]='e';
            }
            else if(i==xk[level]&&j==yk[level]){
                maps[level][i][j]='f';
            }
        }
    }
}
void fillwall(int x1,int y1,int x2,int y2,int level){
    if(y1==y2){
        for(int i=x1;i<x2;i++){
            maps[level][i][y1]='w';
        }
    }
    if(x1==x2){
        for(int i=y1;i<y2;i++){
            maps[level][x1][i]='w';
        }
    }
}
void debug(int level){
    cout<<"\n";
    for(int i=0;i<12;++i){
        for(int j=0;j<12;++j){
            cout<<maps[level][i][j];
        }
        cout<<"\n";
    }
}
void putnum(int start , int end, int amt){
    for(int i=start;i<end;i++){
        t[i]=amt;
    }
}
void print(string s,bool flg,char a,int point){
    for(int i=0;i<s.length();++i){
        cout<<s[i];
        if(flg){
            system("color a0");
            system("color 0a");
        }
        Sleep(t[point]);
        point++;
    }
    cout<<a;
}
char see(){
    while(kbhit()){
        return getch();
    }
}
void fillbox(int as,int bs,int ae,int be,int n){
    int cx=as;
    int cy=be;
    int dx=ae;
    int dy=bs;
    fillwall(as,bs,cx,cy,n);
    fillwall(as,bs,dx,dy,n);
    fillwall(ae,be,cx,cy,n);
    fillwall(ae,be,dx,dy,n);
}
void fills(int a,int b,int c,int d,int n,char ch){
    for(int i=a;i<=c;i++){
        for(int j=b;j<=d;j++){
            maps[n][i][j]=ch;
        }
    }
}
void clears(){
    for(int i=0;i<100;++i){
        xk[i]=100;
        yk[i]=100;
    }
}
void fillair(int a,int b,int n){
    maps[n][a][b]='.';
}
void start(){
    allstep=0;
    clr=1;
//    load out all the maps
    for(int i=0;i<10000;i++)cout<<"\n";
//    map 1
    clears();
    key[1]=true;
    x=1;
    y=1;
    xe=1;
    ye=1;
    setmap(1);
//    map 2
    key[2]=true;
    setmap(2);
    fillwall(2,9,11,9,2);
    fillwall(1,7,10,7,2);
    fillwall(2,5,11,5,2);
    fillwall(1,3,10,3,2);
//    map 3
    setmap(3);
    key[3]=false;
    xk[3]=3;
    yk[3]=4;
    fillwall(5,5,5,5,3);
    fillbox(2,2,8,8,3);
    fillbox(4,4,6,6,3);
    fillbox(9,9,1,5,3);
    fillbox(7,2,7,4,3);
    fillwall(2,2,4,2,3);
//    map 4
    setmap(4);
    key[4]=false;
    xk[4]=5;
    yk[4]=5;
    fills(2,2,9,9,4,'w');
    fills(3,3,8,8,4,'.');
    fills(4,4,7,7,4,'w');
    fills(5,5,6,6,4,'.');
    fillair(5,2,4);
    fillair(6,7,4);
//    map 5
    setmap(5);
    key[5]=false;
    fills(1,4,8,10,5,'w');
    fills(1,5,7,10,5,'.');
    fills(1,6,6,10,5,'w');
    fills(1,7,5,10,5,'.');
    fills(1,8,4,10,5,'w');
    fills(1,9,3,10,5,'.');
    xk[5]=3;
    yk[5]=9;
    fills(2,9,2,9,5,'w');
    fillair(1,8,5);
    fillair(6,10,5);
    fillair(1,4,5);
    fills(2,1,2,2,5,'w');
    fills(4,2,4,3,5,'w');
    fills(6,1,6,2,5,'w');
    fills(8,3,8,4,5,'w');
//    map 6
    setmap(6);
    key[6]=false;
    xk[6]=8;
    yk[6]=9;
    fills(2,2,9,9,6,'w');
    fills(2,3,9,3,6,'.');
    fills(2,5,9,5,6,'.');
    fills(2,7,9,7,6,'.');
    fills(2,10,9,10,6,'w');
    fills(2,9,9,9,6,'.');
    fills(2,1,2,9,6,'w');
    fills(9,1,9,9,6,'w');
    fillair(2,1,6);
    fillair(8,2,6);
    fillair(3,4,6);
    fillair(8,6,6);
    fillair(3,8,6);
    fillair(9,1,6);
//    map 7
    setmap(7);
    key[7]=false;
    xk[7]=2;
    yk[7]=10;
    fills(1,2,10,9,7,'w');
    fills(2,1,10,1,7,'w');
    fills(1,10,9,10,7,'w');
    fills(2,1,6,1,7,'.');
    fills(6,2,6,3,7,'.');
    fills(1,3,5,3,7,'.');
    fills(3,4,3,5,7,'.');
    fills(1,4,1,6,7,'.');
    fills(2,6,2,8,7,'.');
    fills(1,8,1,9,7,'.');
    fills(3,8,3,9,7,'.');
    fills(2,10,3,10,7,'.');
    fillair(7,3,7);
    fills(8,1,8,3,7,'.');
    fillair(9,1,7);
    fills(10,1,10,8,7,'.');
    fills(5,5,9,5,7,'.');
    fillair(9,9,7);
    fills(5,6,5,10,7,'.');
    fills(6,10,7,10,7,'.');
    fills(5,7,7,7,7,'.');
    fills(8,7,8,10,7,'.');
    fillair(9,10,7);
    fills(8,9,8,9,7,'w');
}
int main(){
    again:
    start();
    int stage=1;
    if(clr==0||clr==1){
        system("color 0a");
    }
    else if(clr==2){
        system("color fc");
    }
    else if(clr==3){
        system("color 41");
    }
    else if(clr==4){
        system("color ce");
    }
    else if(clr==6){
        system("color 09");
    }
    else if(clr==7){
        system("color 21");
    }
    else if(clr==5){
        system("color f0");
    }
//    putnum(0,3,50);
//    putnum(3,7,200);
//    putnum(7,10,50);
    putnum(0,10,100);
    string s[100]={"===MAZE===","----------------------------------------","CLICK A TO START        CLICK B FOR INFO","CLICK C FOR SETTINGS     CLICK D TO QUIT","----------------------------------------"};//16,24,
    int pin=0;
    print(s[0],0,'\n',0);
    pin=0;
    putnum(0,10,50);
    c:
    putnum(0,160,10);
    for(int i=1;i<=4;++i){
        print(s[i],0,'\n',pin);
    }
    bool start=false;
    while(1){
        switch(see()){
            case 'a':
                start=true;
                break;
            case 'b':
                goto a;
                break;
            case 'c':
                goto b;
                break;
            case 'd':
                return 0;
            default:
                continue;
        }
        if(start==true)goto stt;
    }
    stt:
        step=1;
        if(stage==8){
            cout<<"CONGRATULATIONS, YOU FINISHED ALL THE LEVELS!\nYOU FINISH ALL THE LEVELS WITH "<<allstep<<" STEPS!\nCLICK 'A' TO TRY AGAIN\nCLICK 'B' TO QUIT.\n";
            Sleep(100);
            while(1){
                switch(see()){
                    case 'a':
                        goto again;
                    case 'b':
                        return 0;
                    default:
                        continue;
                }
            }
        }
        pg();
        if(stage==3){
            cout<<"HINT:STEP ON THE CIRCLE FIRST, AND THEN GO TO THE END POINT\n";
        }
        x=1;
        y=1;
        xe=10;
        ye=10;
        new_(stage);
        getmap(stage);
        while(1){
//            getmap(stage);
            switch(see()){
                case 'w':
                    if(maps[stage][x-1][y]!='w'){
                        maps[stage][x-1][y]=='p';
                        maps[stage][x][y]='.';
                        x--;
                        new_(stage);
                        getmap(stage);
                        step++;
//                        debug(stage);
                    }
                    break;
                case 's':
                    if(maps[stage][x+1][y]!='w'){
                        maps[stage][x+1][y]=='p';
                        maps[stage][x][y]='.';
                        x++;
                        new_(stage);
                        getmap(stage);
                        step++;
//                        debug(stage);
                    }
                    break;
                case 'a':
                    if(maps[stage][x][y-1]!='w'){
                        maps[stage][x][y-1]=='p';
                        maps[stage][x][y]='.';
                        y--;
                        new_(stage);
                        getmap(stage);
                        step++;
//                        debug(stage);
                    }
                    break;
                case 'd':
                    if(maps[stage][x][y+1]!='w'){
                        maps[stage][x][y+1]=='p';
                        maps[stage][x][y]='.';
                        y++;
                        new_(stage);
                        getmap(stage);
                        step++;
//                        debug(stage);
                    }
                    break;
                default:
                    break;   
            }
            if(x==xk[stage]&&y==yk[stage]){
                key[stage]=true;
            }
            if(x==xe&&y==ye&&key[stage]==true){
                stage++;
                cout<<"\nLEVEL COMPLETE!";
                Sleep(1000);
                pg();
                pg();
                allstep+=step;
                step=0;
                goto stt;
            }
        }
    a:
        pg();
        cout<<"CLICK A TO GO LEFT\n";
        cout<<"CLICK D TO GO RIGHT\n";
        cout<<"CLICK W TO GO UP\n";
        cout<<"CLICK S TO GO DOWN\n";
        cout<<"MOVE TO THE END POINT 'X' TO WIN\n";
        cout<<"EAT the dot TO GET KEY\n";
        goto c;
    b:
        if(clr==0||clr==1){
            system("color 0a");
        }
        else if(clr==2){
            system("color fc");
        }
        else if(clr==3){
            system("color 41");
        }
        else if(clr==4){
            system("color ce");
        }
        else if(clr==5){
            system("color f0");
        }
        else if(clr==6){
            system("color 09");
        }
        else if(clr==7){
            system("color 21");
        }
        pg();
        cout<<"CLICK B TO BACK\n";
        cout<<"start at NO."<<stage<<" stage\n click 'w' to add stage\n click 's' to minute stage\n";
        cout<<"click 1~7 to choose color\n";
        while(1){
            switch(see()){
                case 'b':
                    goto c;
                case 'w':
                    if(stage<7){
                        stage++;
                        cout<<"add success";
                        Sleep(200);
                        goto b;
                    }
                    else{
                        cout<<"add failed, ("<<stage+1<<">7)";
                        system("color 04");
                        Sleep(500);
                        goto b;
                    }
                case 's':
                    if(stage>1){
                        stage--;
                        cout<<"remove success";
                        Sleep(500);
                        goto b;
                    }
                    else{
                        cout<<"minute failed, ("<<stage-1<<"<1)";
                        system("color 04");
                        Sleep(500);
                        goto b;
                    }
                case '1':
                    clr=1;
                    goto b;
                case '2':
                    clr=2;
                    goto b;
                case '3':
                    clr=3;
                    goto b;
                case '4':
                    clr=4;
                    goto b;
                case '5':
                    clr=5;
                    goto b;
                case '6':
                    clr=6;
                    goto b;
                case '7':
                    clr=7;
                    goto b;
            }
        }
    return 0;
}

一个快乐的小小孩
2020-03-25 09:22
快速回复:[求助]c++迷宫问题出错了,大佬求解答
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017566 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved