URAL 1224. Spiral (规律)

Posted clnchanpin

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了URAL 1224. Spiral (规律)相关的知识,希望对你有一定的参考价值。

1224. Spiral

Time limit: 1.0 second
Memory limit: 64 MB
技术分享
A brand new sapper robot is able to neutralize mines in a rectangular region having integer height and width (N and M respectively). Before the robot begins its work it is placed near the top leftmost cell of the rectangle heading right. Then the robot starts moving and neutralizing mines making a clockwise spiral way (see picture). The spiral twists towards the inside of the region, covering all the cells. The region is considered safe when all the cells are visited and checked by the robot.
Your task is to determine the number of the turns the robot has to make during its work.

Input

The input contains two integers in the following order: N, M (1 ≤ N, M ≤ 231 ? 1).

Output

The output consists of a single integer value — the number of the turns.

Sample

input output
3 5
4
Problem Source: 2002-2003 ACM Central Region of Russia Quarterfinal Programming Contest, Rybinsk, October 2002



解析:找规律。一定要注意n和m的大小关系,由于出发地点是固定的。



AC代码:

#include <bits/stdc++.h>
using namespace std;

int main(){
    long long n, m;
    while(~scanf("%lld%lld", &n, &m)){
        long long ans;
        if(n <= m) ans = 2 * (n - 1);
        else ans = 2 * m - 1;
        printf("%lld\n", ans);
    }
    return 0;
}














以上是关于URAL 1224. Spiral (规律)的主要内容,如果未能解决你的问题,请参考以下文章

Spiral and Zigzag

URAL 2080 Wallet

ural 1149. Sinus Dances

URAL 1513. Lemon Tale(简单的递推)

URAL 1933 Guns for Battle!

URAL 2011. Long Statement题解