{"problem":{"name":"I. Simple Robot","description":{"content":"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. Th","description_type":"Markdown"},"platform":"Codeforces","limit":{"time_limit":2000,"memory_limit":262144},"difficulty":"None","is_remote":true,"is_sync":true,"sync_url":null,"sign":"CF10110I"},"statements":[{"statement_type":"Markdown","content":"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.\n\nSaMer 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?\n\nThe first line of input contains a single integer T, the number of test cases.\n\nEach 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.\n\nFor each test case, print the minimum number of instructions which will be skipped, on a single line.\n\n## Input\n\nThe 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.\n\n## Output\n\nFor each test case, print the minimum number of instructions which will be skipped, on a single line.\n\n[samples]","is_translate":false,"language":"English"},{"statement_type":"Markdown","content":"**Definitions**  \nLet $ R, C \\in \\mathbb{Z}^+ $ denote the dimensions of the grid.  \nLet $ S \\in \\{ \\text{^}, \\text{v}, \\text{<}, \\text{>} \\}^* $ be the instruction string of length $ n $.  \nLet $ (x, y) \\in \\{1, \\dots, R\\} \\times \\{1, \\dots, C\\} $ be the initial position of the robot.  \n\n**Constraints**  \n1. $ 1 \\le R, C \\le 10^5 $  \n2. $ 1 \\le |S| \\le 2 \\times 10^5 $  \n\n**Objective**  \nFor each starting position $ (x, y) $, simulate the execution of $ S $:  \n- For each command $ s_i \\in S $, compute the next position $ (x', y') $ based on direction.  \n- If $ (x', y') \\notin \\{1, \\dots, R\\} \\times \\{1, \\dots, C\\} $, skip $ s_i $ (count as skipped).  \n- Otherwise, update $ (x, y) \\leftarrow (x', y') $.  \n\nFind:  \n$$\n\\min_{(x,y) \\in \\{1,\\dots,R\\} \\times \\{1,\\dots,C\\}} \\left( \\text{number of skipped commands starting from } (x,y) \\right)\n$$","is_translate":false,"language":"Formal"}],"meta":{"iden":"CF10110I","tags":[],"sample_group":[],"created_at":"2026-03-03 11:00:39"}}