CodeForces 888B Buggy Robot
来源:互联网 发布:淘宝管理费 编辑:程序博客网 时间:2024/05/18 04:59
Description:
Ivan has a robot which is situated on an infinite grid. Initially the robot is standing in the starting cell(0, 0). The robot can process commands. There are four types of commands it can perform:
- U — move from the cell (x, y) to (x, y + 1);
- D — move from (x, y) to(x, y - 1);
- L — move from (x, y) to(x - 1, y);
- R — move from (x, y) to(x + 1, y).
Ivan entered a sequence of n commands, and the robot processed it. After this sequence the robot ended up in the starting cell(0, 0), but Ivan doubts that the sequence is such that after performing it correctly the robot ends up in the same cell. He thinks that some commands were ignored by robot. To acknowledge whether the robot is severely bugged, he needs to calculate the maximum possible number of commands that were performed correctly. Help Ivan to do the calculations!
The first line contains one number n — the length of sequence of commands entered by Ivan (1 ≤ n ≤ 100).
The second line contains the sequence itself — a string consisting of n characters. Each character can be U, D, L or R.
Print the maximum possible number of commands from the sequence the robot could perform to end up in the starting cell.
4LDUR
4
5RRRUU
0
6LLRRRR
4
题目大意:
个机器人有如下的操作, 向上(U), 向下(D), 向左(L)和向右(R)。起初机器人在远点, 现在给机器人一条指令, 问机器人所能走的最大步数回到原点的值是多少。
解题思路:
我们可以贪心的走, 一对LR或一对UD都满足条件, 所以我们只要统计四种操作的个数, 答案就能计算出是2 * min(l, r)+ 2 * min(u, d)。
代码:
#include <iostream>
#include <sstream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iomanip>
#include <utility>
#include <string>
#include <cmath>
#include <vector>
#include <bitset>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
using namespace std;
/*
*ios::sync_with_stdio(false);
*/
typedef long long ll;
typedef unsigned long long ull;
const int dir[5][2] = {0, 1, 0, -1, 1, 0, -1, 0, 0, 0};
const ll ll_inf = 0x7fffffff;
const int inf = 0x3f3f3f;
const int mod = 1000000;
const int Max = (int) 1e6;
int n;
char arr[Max];
int main() {
//freopen("input.txt", "r", stdin);
scanf("%d", &n);
scanf("%s", arr);
int l = 0, r = 0, u = 0, d = 0;
for (int i = 0; i < n; ++i) {
if (arr[i] == 'L') l++;
else if (arr[i] == 'R') r++;
else if (arr[i] == 'U') u++;
else if (arr[i] == 'D') d++;
}
int ans = 2 * min(l , r) + 2 * min(u, d);
printf("%d\n", ans);
return 0;
}
- CodeForces 888B Buggy Robot
- Codeforces 888B Buggy Robot (模拟)
- codeforces 893B. Buggy Robot
- B. Buggy Robot
- Educational Codeforces Round 32 B. Buggy Robot(模拟)
- UVA Live 7693 (Codeforces Gym 101201B) Buggy Robot DP+bfs
- Buggy Robot Gym
- Buggy Robot Gym
- Buggy Robot (二维压一维最短路
- 【codeforces】C - Buggy Sorting
- Codeforces Round #323 B Robot's Task
- CodeForces 626A B - Robot Sequence
- CodeForces 246A Buggy Sorting
- Codeforces 8B Obsession With Robot 简单图遍历
- Codeforces Round #323 (Div. 2) B. Robot's Task
- Codeforces Round #323 (Div. 2) B. Robot's Task
- Codeforces Round #323 (Div. 2)B. Robot's Task
- Codeforces Round #323(div2) 583B. Robot's Task
- Pytorch学习笔记(三)线性回归与逻辑回归
- 选择排序法和冒泡排序法
- 触发器的小应用
- 沉思录(一)学习本身就是一件孤独而又崇高的事业
- 【SQL解惑】谜题8:调度打印机
- CodeForces 888B Buggy Robot
- 【SQL解惑】谜题9:空座位
- 零基础10分钟运行DQN图文教程 Playing Flappy Bird Using Deep Reinforcement Learning (Based on Deep Q Learning DQN
- 【SQL解惑】谜题10:社会保险号的工资
- C语言小笔记(1)
- PMD规则
- Lucene深入学习(5)Lucene的Document与Field
- 大学毕业半年回顾
- IP网段分析