CF294D.Shaass and Painter Robot

省选/NOI-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Shaass thinks a kitchen with all white floor tiles is so boring. His kitchen floor is made of n·m square tiles forming a n × m rectangle. Therefore he's decided to color some of the tiles in black so that the floor looks like a checkerboard, which is no two side-adjacent tiles should have the same color.

Shaass wants to use a painter robot to color the tiles. In the beginning the robot is standing in a border tile (x__s, y__s) facing a diagonal direction (i.e. upper-left, upper-right, down-left or down-right). As the robot walks in the kitchen he paints every tile he passes even if it's painted before. Painting each tile consumes one unit of black paint. If at any moment the robot hits a wall of the kitchen he changes his direction according the reflection rules. Note that a tile gets painted when the robot enters the tile from another tile, in other words changing direction in the same tile doesn't lead to any painting. The first tile the robot is standing on, is also painted.

The robot stops painting the first moment the floor is checkered. Given the dimensions of the kitchen and the position of the robot, find out the amount of paint the robot consumes before it stops painting the floor.

Let's consider an examples depicted below.

If the robot starts at tile number 1 (the tile (1, 1)) of the left grid heading to down-right it'll pass tiles 1354236 and consumes 7 units of black paint on his way until he stops at tile number 6. But if it starts at tile number 1 in the right grid heading to down-right it will get stuck in a loop painting tiles 1, 2, and 3.

沙阿斯觉得全白的厨房地砖太过单调。他的厨房地面由 n⋅mn \cdot m 块正方形瓷砖构成一个 n×mn \times m 的矩形。因此,他决定将其中一些瓷砖涂成黑色,使地面呈现棋盘格样式——即任意两个共享一条边的相邻瓷砖颜色均不相同。

沙阿斯打算使用一台自动涂漆机器人来为瓷砖上色。初始时,机器人位于某块边界瓷砖 (xs, ys)(x_s,\,y_s) 上,并朝向某个对角线方向(即左上、右上、左下或右下)。当机器人在厨房中移动时,它会将其经过的每一块瓷砖都涂成黑色(即使该瓷砖此前已被涂过)。每涂一块瓷砖消耗一单位黑色颜料。若机器人在行进过程中碰到厨房墙壁,则根据反射规律改变其行进方向。注意:只有当机器人从另一块瓷砖进入当前瓷砖时,该瓷砖才会被涂色;即在同一块瓷砖内转向不会导致任何涂色操作。机器人起始所站的那块瓷砖也会被涂色。

机器人会在地板首次变为棋盘格样式时立即停止涂色。已知厨房的尺寸及机器人的初始位置,请计算机器人停止涂色前总共消耗的黑色颜料量。

我们考虑如下示例图:

若机器人从左侧网格中标号为 1 的瓷砖(即坐标为 (1, 1)(1,\,1) 的瓷砖)出发,并朝右下方向行进,则它将依次经过编号为 1、3、5、4、2、3、6 的瓷砖,共消耗 7 单位黑色颜料,最终在编号为 6 的瓷砖处停止。但若机器人从右侧网格中标号为 1 的瓷砖出发并朝右下方向行进,则它将陷入循环,反复涂刷编号为 1、2 和 3 的瓷砖而无法终止。

输入格式

The first line of the input contains two integers n and m, (2 ≤ n, m ≤ 105). The second line contains two integers x__s and y__s (1 ≤ x__s ≤ n, 1 ≤ y__s ≤ m) and the direction robot is facing initially. Direction is one of the strings: "UL" (upper-left direction), "UR" (upper-right), "DL" (down-left) or "DR" (down-right).

Note, that record (x__s, y__s) denotes the tile that is located at the x__s-th row from the top and at the y__s-th column from the left of the kitchen.

It's guaranteed that the starting position will be a border tile (a tile with less than four side-adjacent tiles).

输入的第一行包含两个整数 nn 和 mm(2≤n,m≤1052 \leq n, m \leq 10^5)。第二行包含两个整数 xsx_s 和 ysy_s(1≤xs≤n1 \leq x_s \leq n,1≤ys≤m1 \leq y_s \leq m)以及机器人初始朝向。朝向为以下字符串之一:“UL”(左上方向)、“UR”(右上方向)、“DL”(左下方向)或“DR”(右下方向)。

注意,坐标记录 (xs,ys)(x_s, y_s) 表示厨房中从上往下数第 xsx_s 行、从左往右数第 ysy_s 列的方格。

保证起始位置为边界方格(即邻接的四连通方格数量少于 4 个的方格)。

输出格式

Print the amount of paint the robot consumes to obtain a checkered kitchen floor. Or print -1 if it never happens.

Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specificator.

打印机器人为了获得棋盘格图案的厨房地板所消耗的油漆量;如果这种情况永远不会发生,则打印 -1。

请注意:在 C++ 中,请勿使用 %lld 格式说明符来读取或写入 64 位整数。推荐使用 cin、cout 流,或使用 %I64d 格式说明符。

输入输出样例

  • 输入#1

    3 4
    1 1 DR

    输出#1

    7
  • 输入#2

    3 4
    3 3 DR

    输出#2

    11
  • 输入#3

    3 3
    1 1 DR

    输出#3

    -1
  • 输入#4

    3 3
    1 2 DL

    输出#4

    4

输入解题思路,AI测评打分。不知道怎么写?

首页