ural 1224. Spiral dfs

来源:互联网 发布:石家庄网络兼职 编辑:程序博客网 时间:2024/06/07 14:03

1224. Spiral

Time limit: 1.0 second
Memory limit: 64 MB
Problem illustration
A brand new sapper robot is able to neutralize mines in a rectangular region having integer height and width (N and Mrespectively). 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: NM (1 ≤ NM ≤ 231 − 1).

Output

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

Sample

inputoutput
3 5
4

Problem Source: 2002-2003 ACM Central Region of Russia Quarterfinal Programming Contest, Rybinsk, October 2002

class InputReader():    def nextInt(self):        return  int(input().strip())    def nextLine(self):        return input()    def nextString(self):        return input().strip()    def nextInts(self):        ints = []        str = input().strip().split()        for s in str:            if (s is None) or (len(s) == 0):                continue            ints.append(int(s))        return ints    def nextFloats(self):        floats = []        str = input().strip().split()        for s in str:            if (s is None) or (len(s) == 0):                continue            floats.append(float(s))        return floatsdef gao(n , m):    if n == 0 or m == 0:        return -1    if n == 1 :        return 0    if m == 1 :        return 1    if n == 2 :        return 2    if m == 2:        return 3    if n == 3:        return 4    if m == 3:        return 5    k = int(min(n , m)/2)-1    return 4 * k + gao(n - 2*k , m - 2*k)if __name__ == '__main__':    reader = InputReader()    nums = reader.nextInts()    n = nums[0]    m = nums[1]    print(gao(n , m))


原创粉丝点击