ZOJ4115 Wandering Robot
模拟 思维题
AC代码:
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 typedef unsigned long long ull; 5 6 char a[100005]; 7 8 int main(){ 9 int t, n, k; 10 cin>>t; 11 ll x0, y0, x, y; 12 ll ans1, ans2; 13 while (t--){ 14 memset(a, 0, sizeof(a)); 15 cin>>n>>k; 16 cin>>a; 17 x0 = 0, y0 = 0; 18 ans1 = 0, ans2 = 0; 19 for (int i = 0; i < n; i++){ 20 if (a[i] == 'L') x0--; 21 else if (a[i] == 'R') x0++; 22 else if (a[i] == 'U') y0++; 23 else if (a[i] == 'D') y0--; 24 if (abs(x0) + abs(y0) > ans1) 25 ans1 = abs(x0) + abs(y0); 26 } 27 ans2 = (k - 1) * (abs(x0) + abs(y0)); 28 x = x0 * (k - 1); 29 y = y0 * (k - 1); 30 for (int i = 0; i < n; i++){ 31 if (a[i] == 'L') x--; 32 else if (a[i] == 'R') x++; 33 else if (a[i] == 'U') y++; 34 else if (a[i] == 'D') y--; 35 ans2 = max(ans2, abs(x) + abs(y)); 36 } 37 cout<<max(ans1, ans2)<<endl; 38 } 39 return 0; 40 }