weinan030416

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

走迷宫

@起点,.终点,1障碍,0可以走,输出路径

复制代码
#include <iostream>
using namespace std;

int dx[4]={0,0,1,-1},dy[4]={1,-1,0,0};
int startx,starty,endx,endy,w,l; 
char puzzle[10][10];//最多支持10*10迷宫 
bool walked[10][10];
struct point
{
    int x;
    int y;
};
point path[100];

void dfs(int x,int y,int cnt)
{    
    path[cnt].x=x;//记录路径 
    path[cnt].y=y;
    cnt++;
    walked[x][y]=true;//标记走过
    
    if(x==endx&&y==endy)//走到终点 
    {
        for(int i=0;i<cnt-1;i++)
        cout<<path[i].x<<","<<path[i].y<<"->";
        cout<<path[cnt-1].x<<","<<path[cnt-1].y<<endl;
        return;
    }
    bool t=false;
    for(int k=0;k<4;k++)
    {
        int tx=x+dx[k],ty=y+dy[k];
        if(puzzle[tx][ty]!='1'&&walked[tx][ty]==false&&tx>=0&&ty>=0&&tx<w&&ty<l)//可以走 
        {
            t=true;
            dfs(tx,ty,cnt); 
            walked[tx][ty]=false; 
        }
    }
    if(!t)//无路可走 
    return;
}
 
int main()
{
    cin>>w>>l;
    for(int i=0;i<w;i++)
        for(int j=0;j<l;j++)
        {
            cin>>puzzle[i][j];
            //0可以走,1障碍,@起点,.终点 
            if(puzzle[i][j]=='@')
            {
                startx=i;
                starty=j;
            }
            if(puzzle[i][j]=='.')
            {
                endx=i;
                endy=j;
            }
        }
    dfs(startx,starty,0);    
}
复制代码

 最大岛屿面积

1陆地  0海洋

 

posted on   楠030416  阅读(35)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
点击右上角即可分享
微信分享提示