|洛谷|模拟|P2689 东南西北

本文提供了一种解决洛谷2689题目(关于路径选择算法)的方法,通过确定两个主要方向并根据风向调整路径来计算最短距离。代码采用C++实现,包括读取起点和终点坐标、确定移动方向以及根据每一步风向更新剩余距离。

https://www.luogu.org/problem/show?pid=2689

由题意显然只用朝两个方向走,确定下来这两个方向以后,比对每个时刻的风向即可

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
#define ms(i,j) memset(i, j, sizeof i);
int x1,x2,y1,y2,T;
char dirx, diry;
char ch;
int ans;
int abss(int x)
{
    return x>=0 ? x : -x;
}
int main()
{
    scanf("%d%d%d%d\n%d\n", &x1, &y1, &x2, &y2, &T);
    int x = x2-x1; int y = y2-y1;
    int ax = abss(x) , ay = abss(y);
    if (x>0) dirx = 'N'; else dirx = 'S';
    if (y>0) diry = 'E'; else diry = 'W';
    for (int i=1;i<=T;i++) 
    {
        scanf("%c", &ch);
        if (ch==dirx) ax--;
        if (ch==diry) ay--;
        if (ax<=0&&ay<=0) break;
        scanf("\n");
    }
    if (ax<=0&&ay<=0) printf("%d\n",abss(x)+abss(y)); else printf("-1\n");
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值