Simple Robot Gym - 101102I (思维)
SaMer is building a simple robot that can move in the four directions: up (^), down (v), left (<), and right (>). The robot receives the commands to move as a string and executes them sequentially. The robot skips the commands that make it go outside the table.
SaMer has an R×C table and a string of instructions. He wants to place the robot on some cell of the table without rotating it, such that the robot will skip the minimum number of instructions. Can you help him find the number of instructions that will be skipped by the robot if it was placed optimally?
Input
The first line of input contains a single integer T, the number of test cases.
Each test case contains two space-separated integers R and C (1 ≤ R, C ≤ 105), the number of rows and the number of columns in the table, followed by a non-empty string of no more than 200000 characters representing the initial instructions. The instructions are executed in the given order from left to right.
Output
For each test case, print the minimum number of instructions which will be skipped, on a single line.
Example
2
2 2 >^<v>^^<v
3 4 >^<^vv<>v>>v
1
2 题意:
给一个n*m的格子,以及一串移动序列,如果移动超出边界,则会跳过这个操作,问最少会跳过多少个序列(起点未定)
思路:
没必要确定起点,如果在同一方向上走出的距离长度超出了棋盘长度的,那就是要跳过的操作。
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<ctime>
#define fuck(x) cout<<#x<<" = "<<x<<endl;
#define debug(a,i) cout<<#a<<"["<<i<<"] = "<<a[i]<<endl;
#define ls (t<<1)
#define rs ((t<<1)+1)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = ;
const int maxm = ;
const int inf = 2.1e9;
const ll Inf = ;
const int mod = ;
const double eps = 1e-;
const double pi = acos(-); char s[maxn]; int main()
{
// ios::sync_with_stdio(false);
// freopen("in.txt","r",stdin); int T;
scanf("%d",&T);
while(T--){
int n,m;
scanf("%d%d",&n,&m);
scanf("%s",s);
int len=strlen(s);
int x,y;
x=y=;
int x1,x2,y1,y2;
x1=x2=y1=y2=;
int ans=;
for(int i=;i<len;i++){
if(s[i]=='^'){y++;if(y-y1>=n){ans++;y--;continue;}}
else if(s[i]=='v'){y--;if(y2-y>=n){ans++;y++;continue;}}
else if(s[i]=='>'){x++;if(x-x1>=m){ans++;x--;continue;}}
else{
x--;if(x2-x>=m){ans++;x++;continue;}
}
// cout<<i<<" "<<x<<" "<<x1<<endl;
x1=min(x1,x);
x2=max(x2,x);
y1=min(y1,y);
y2=max(y2,y);
}
printf("%d\n",ans);
} return ;
}
/*
5
1 5
>>>>><<<<<
*/
Simple Robot Gym - 101102I (思维)的更多相关文章
- Gym - 101981E 思维
Gym - 101981EEva and Euro coins 题意:给你两个长度皆为n的01串s和t,能做的操作是把连续k个相同的字符反转过来,问s串能不能变成t串. 一开始把相同的漏看了,便以为是 ...
- Problem D. Berland Railroads Gym - 101967D (思维)
题目链接:https://cn.vjudge.net/contest/274029#problem/D 题目大意:给你0-9每个数的个数,然后让你找出最大的数,满足的条件是任意三位相连的都能被三整除. ...
- Mujin Programming Challenge 2017A - Robot Racing【思维题】
题意: 给你n个人的位置,每个人能往后跳一格或两格到无人的位置,跳到0位置,这个人消失,n个人消失组成一个排列,问有多少种排列. 思路: 额,搞了一整场这个A...代码也巨挫了. 处理成1,3,5,7 ...
- Little Difference Gym - 101612L 思维
题意: 给你一个数n,你需要输出它可以由那几个数相乘构成,我们设可以由x个数构成,这x个数中最小值为minn,最大值为maxx,那么要求maxx-minn<=1 问你满足上面要求的情况有多少种. ...
- Equal Numbers Gym - 101612E 思维
题意: 给你n个数vi,你有k次操作.每一次操作你可以从n个数里面挑一个数,然后使得这个数乘于一个正整数.操作完之后,这n个数里面不同数的数量就是权值.你要使得这个值尽可能小. 题解: 如果a%b== ...
- Consonant Fencity Gym - 101612C 暴力二进制枚举 Intelligence in Perpendicularia Gym - 101612I 思维
题意1: 给你一个由小写字母构成的字符串s,你可以其中某些字符变成大写字母.如果s中有字母a,你如果想把a变成大写,那s字符串中的每一个a都要变成A 最后你需要要出来所有的字符对,s[i]和s[i-1 ...
- Gazebo機器人仿真學習探索筆記(一)安裝與使用
Gazebo提供了多平臺的安裝和使用支持,大部分主流的linux,Mac以及Windows,這裏結合ROS以Ubuntu爲例進行介紹. 首先是參考資料:http://gazebosim.org/tut ...
- Codeforces Gym 100610 Problem K. Kitchen Robot 状压DP
Problem K. Kitchen Robot Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/10061 ...
- codeforce gym/100495/problem/K—Wolf and sheep 两圆求相交面积 与 gym/100495/problem/E—Simple sequence思路简述
之前几乎没写过什么这种几何的计算题.在众多大佬的博客下终于记起来了当时的公式.嘚赶快补计算几何和概率论的坑了... 这题的要求,在对两圆相交的板子略做修改后,很容易实现.这里直接给出代码.重点的部分有 ...
随机推荐
- Leetcode783.Minimum Distance Between BST Nodes二叉搜索树结点最小距离
给定一个二叉搜索树的根结点 root, 返回树中任意两节点的差的最小值. 示例: 输入: root = [4,2,6,1,3,null,null] 输出: 1 解释: 注意,root是树结点对象(Tr ...
- Word画线条5大技巧,简单实用!
[Word画线条5大技巧,简单实用!]1.输入三个“=”,回车,就是一条双直线:2.输入三个“~”,回车,就是一条波浪线:3.输入三个“”回车,就是一条虚线:4.输入三个“-”,回车,就是一条直线:5 ...
- Pyhton 单行、多行注释方法
一.python单行注释的符号 井号#常被用作单行注释符号,在代码中使用#时,它右边的任何数据都会被忽略,当做是注释.类似c++的// 二.批量.多行注释的符号 多行注释是用三引号: ”’ 注释内容 ...
- Minimum Depth of Binary Tree最短深度
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- 三级分销会员一次查询出来的SQL语句
SELECT p.id AS partyId, p.parent_id AS parentId, pul.username AS userName, p.city, p.birth_date AS b ...
- python 数据的写入
- deepin 安装golang protobuf
1.安装库文件protobuf,地址:https://github.com/protocolbuffers/protobuf/releases 我电脑是deepin 64位的,所以我直接下载https ...
- java根据年月获取当前月的每一天日期
public static List<String> getDayByMonth(int yearParam,int monthParam){ List list = ne ...
- intellij idea怎么设置java帮助文档
打开idea我引用的jar包都放在 Project Structure-->Modules-->libs文件夹(双击) 双击jar包所在文件夹,跳出对话框. 1.如果api对应的javad ...
- 洛谷P1616 疯狂的采药
//完全背包 #include<bits/stdc++.h> using namespace std; ; ; int n,m,v[maxn],w[maxn],f[maxv]; int m ...