CodeForces - 748C Santa Claus and Robot
题意:机器人在网格线上行走,从p1点开始,沿最短路径到p2,再沿最短路径到p3,依此类推。在此过程中留下了行走的运动轨迹,由“RLDU”表示。问若只给出运动轨迹,求最少的pi点的个数。
分析:pi到pi+1是沿最短路径走的,因此在此路径中不可能同时出现“UD”两个方向(“LR”同理)。因此只要同时出现,那一定证明此刻已往下一个目标点走。
#pragma comment(linker, "/STACK:102400000, 102400000") #include<cstdio> #include<cstring> #include<cstdlib> #include<cctype> #include<cmath> #include<iostream> #include<sstream> #include<iterator> #include<algorithm> #include<string> #include<vector> #include<set> #include<map> #include<stack> #include<deque> #include<queue> #include<list> #define Min(a, b) ((a < b) ? a : b) #define Max(a, b) ((a < b) ? b : a) const double eps = 1e-8; inline int dcmp(double a, double b){ if(fabs(a - b) < eps) return 0; return a > b ? 1 : -1; } typedef long long LL; typedef unsigned long long ULL; const int INT_INF = 0x3f3f3f3f; const int INT_M_INF = 0x7f7f7f7f; const LL LL_INF = 0x3f3f3f3f3f3f3f3f; const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f; const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1}; const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1}; const int MOD = 1e9 + 7; const double pi = acos(-1.0); const int MAXN = 200000 + 10; const int MAXT = 10000 + 10; using namespace std; char a[MAXN]; map<char, int> mp; int vis[5]; void init(){ mp['L'] = 0; mp['R'] = 1; mp['U'] = 2; mp['D'] = 3; } bool judge(int x){ if(x == 0 && vis[1]) return true; if(x == 1 && vis[0]) return true; if(x == 2 && vis[3]) return true; if(x == 3 && vis[2]) return true; return false; } int main(){ init(); int n; while(scanf("%d", &n) == 1){ scanf("%s", a); int cnt = 1; memset(vis, 0, sizeof vis); for(int i = 0; i < n; ++i){ int tmp = mp[a[i]]; if(judge(tmp)){ memset(vis, 0, sizeof vis); ++cnt; } vis[tmp] = 1; } printf("%d\n", cnt); } return 0; }