求个曼哈顿距离然后贪心
//#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 条评论) |