列表

详情


NC236064. Wandering Robot

描述

DreamGrid creates a programmable robot to explore an infinite two-dimension plane. The robot has a basic instruction sequence and a "repeating parameter" k, which together form the full instruction sequence and control the robot.
There are 4 types of valid instructions in total, which are `U' (up), `D' (down), `L' (left) and `R' (right). Assuming that the robot is currently at (x,y), the instructions control the robot in the way below:
  • U: Moves the robot to .
  • D: Moves the robot to (x,y-1).
  • L: Moves the robot to (x-1,y).
  • R: Moves the robot to .
The full instruction sequence can be derived from the following equations


The robot is initially at (0,0) and executes the instructions in the full instruction sequence one by one. To estimate the exploration procedure, DreamGrid would like to calculate the largest Manhattan distance between the robot and the start point (0,0) during the execution of the nk instructions.
Recall that the Manhattan distance between (x_1,y_1) and (x_2,y_2) is defined as .

输入描述

There are multiple test cases. The first line of the input contains an integer T indicating the number of test cases. For each test case:
The first line contains two integers n and k (), indicating the length of the basic instruction sequence and the repeating parameter.
The second line contains a string (, ), where a_i indicates the i-th instruction in the basic instriction sequence.
It's guaranteed that the sum of of all test cases will not exceed .

输出描述

For each test case output one line containing one integer indicating the answer.

示例1

输入:

2
3 3
RUL
1 1000000000
D

输出:

4
1000000000

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

pypy3 解法, 执行用时: 454ms, 内存消耗: 30192K, 提交时间: 2022-04-03 10:06:44

t = int(input())
while t:
    t -= 1
    n, k = map(int, input().split())
    s = input()
    ans = 0
    x, y = 0, 0
    for i in s:
        if i == 'U':
            y += 1
        if i == 'D':
            y -= 1
        if i == 'L':
            x -= 1
        if i == 'R':
            x += 1
        ans = max(ans, abs(x)+abs(y))
    x = x*(k-1)
    y = y*(k-1)
    for i in s:
        if i == 'U':
            y += 1
        if i == 'D':
            y -= 1
        if i == 'L':
            x -= 1
        if i == 'R':
            x += 1
        ans = max(ans, abs(x)+abs(y))
    print(ans)

C++ 解法, 执行用时: 53ms, 内存消耗: 800K, 提交时间: 2022-06-18 20:41:47

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll T,n,k,x,y,l,s;
string S;

int main(){
	int i;
	cin>>T;
	while(T--){
		cin>>n>>k;
		cin>>S;
		x=y=s=0;
		for(i=0;i<n;i++){
			if(S[i]=='U')y++;
			else if(S[i]=='D')y--;
			else if(S[i]=='L')x--;
			else if(S[i]=='R')x++;
			l=abs(x)+abs(y);
			s=max(s,l); 
		}
		x*=(k-1),y*=(k-1);
		for(i=0;i<n;i++){
			if(S[i]=='U')y++;
			else if(S[i]=='D')y--;
			else if(S[i]=='L')x--;
			else if(S[i]=='R')x++;
			l=abs(x)+abs(y);
			s=max(s,l); 
		}
		cout<<s<<endl;
	}
}

上一题