[洛谷] P2689 东南西北

阅读: 评论:0

[洛谷] P2689 东南西北

[洛谷] P2689 东南西北

求个曼哈顿距离然后贪心

//#pragma GCC optimize(2)
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <string>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <ctime>
#include <vector>
#include <fstream>
#include <list>
#include <iomanip>
#include <numeric>
using namespace std;
typedef long long ll;const int MAXN = 1e6 + 10;int arr[MAXN][2], bx, by, ex, ey, step = 0, n;bool judge(int tx, int ty, int x, int y)
{double tdis = sqrt((tx - ex) * (tx - ex) +(ty - ey) * (ty - ey));double rdis = sqrt((x - ex) * (x - ex) +(y - ey) * (y - ey));if(tdis < rdis)return true;return false;
}int main()
{//ios::sync_with_stdio(false);//cin.tie(0);     cout.tie(0);cin>>bx>>by>>ex>>ey>>n;char c;for(int i = 0; i < n; i++){cin>>c;switch(c){case 'E':arr[i][0] = 1, arr[i][1] = 0;break;case 'S':arr[i][0] = 0, arr[i][1] = -1;break;case 'W':arr[i][0] = -1, arr[i][1] = 0;break;case 'N':arr[i][0] = 0, arr[i][1] = 1;break;}}int tx, ty;for(int i = 0; i < n; i++){tx = bx + arr[i][0], ty = by + arr[i][1];if(judge(tx, ty, bx, by))bx = tx, by = ty, step++;if(bx == ex && by == ey){cout<<step<<endl;goto l1;}}cout<<"-1"<<endl;l1:return 0;
}

 

本文发布于:2024-01-30 20:54:32,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170661927322773.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:东南西北   洛谷
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23