c++世界生存手册(启程——3)

故事

你,小D和小H拿着赢来的新武器准备出发,发现没有钱买船票。
完蛋了,要正式出发还要船票,但你们的钱在之前逃脱密室的时候就连同一起被炸掉了,应该怎么办呢。
啊啊啊,要崩溃了
不过好在船长喜欢聪明的人,想到这次航行要经过死亡峡谷,所以他将地图拿了出来给你们来找路线。他苦思冥想才大概画出来的地图,让你们找路线,并承诺只要找出来正确的路线,就可以让你们免费上船。听到这个消息,身无分文的你们就二话不说答应了。

题目

船长的船需要离开死亡峡谷,但峡谷却错综复杂,请找到一条最快的路径离开死亡峡谷。
峡谷mn格(表示有m行、n列),其中有可走的路也有不可走的路,如果用1表示可以走,0表示不可以走,文件读入这mn个数据和起始点、结束点(起始点和结束点都是用两个数据来描述的,分别表示这个点的行号和列号)。走时只能是上下左右四个方向。如果一条路都不可行,则输出相应信息(用-1表示无路)。


#include<bits/stdc++.h>
using namespace std;
int sum[50000][2];
int ax,ay,bx,by,k,pd;
int cx[4]={0,-1,0,1};
int cy[4]={-1,0,1,0};
bool temp[17][17];
int map[17][17];
void print()
{
    if(pd==0)
    {
        pd=1; 
    }
    for(int h=0;h<=k-1;h++)
    cout<<"("<<sum[h][0]<<","<<sum[h][1]<<")"<<"->"; 
    cout<<"("<<bx<<","<<by<<")"<<endl;
}
void walk(int x,int y)
{
    if(x==bx&&y==by)
    {
        print();
        return;
    }
    else
    {
        for(int i=0;i<=3;i++)
        if(map[x+cx[i]][y+cy[i]]==1&&temp[x+cx[i]][y+cy[i]]==0)
        {
            temp[x][y]=1;
            sum[k][0]=x;
            sum[k][1]=y;
            k++;
            walk(x+cx[i],y+cy[i]);
            temp[x][y]=0;
            k--;
        }
    }
}
int main()
{
    int m,n;
    cin>>m>>n;
    for(int i=1;i<=m;i++)
        for(int j=1;j<=n;j++)
            cin>>map[i][j];

    cin>>ax>>ay;
    cin>>bx>>by;
    walk(ax,ay);
    if(pd==0) cout<<"-1";
    return 0;
}

———————————分割线———————————

English

Story

You, Dr.D and Dr.H, with the new weapons they won, are ready to go and find that they have no money to buy tickets.

At the end of the day, you need a ticket for the official departure, but when your money escaped from the secret chamber, it was blown up together. What should we do?

Luckily,the capital like smart people,he remember this trip will pass the Death Valley.So he give you the map,promised if you can find the way can safely pass the Death Vallry,you can go with him without any money!

Problem

The ship belongs of the captial need to leave the Death Valley, but it is very hard,please find the most quickly.

The Death Valley has m*n checks,it has the way can go and the way can’t go.We use ‘1’ means it can go,‘0’ means it can’t go,

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值