USACO-青铜

USACO的题不会,求解
Farmer John has recently expanded the size of his farm, so from the perspective of his cows it is effectively now infinite in size! The cows think of the grazing area of the farm as an infinite 2D grid of square “cells”, each filled with delicious grass (think of each cell as a square in an infinite chessboard). Each of Farmer John’s N cows (1≤N≤50) starts out in a different cell; some start facing north, and some start facing east.
Every hour, every cow either

Stops if the grass in her current cell was already eaten by another cow.
Eats all the grass in her current cell and moves one cell forward according to the direction she faces.
Over time, each cow therefore leaves a barren “rut” of empty cells behind her.

If two cows move onto the same grassy cell in the same move, they share the cell and continue moving in their respective directions in the next hour.

Please determine the amount of grass eaten by each cow. Some cows never stop, and therefore eat an infinite amount of grass.

INPUT FORMAT (input arrives from the terminal / stdin):
The first line of input contains N. Each of the next N lines describes the starting location of a cow, in terms of a character that is either N (for north-facing) or E (for east-facing) and two nonnegative integers x and y (0≤x≤109, 0≤y≤109) giving the coordinates of a cell. All x-coordinates are distinct from each-other, and similarly for the y-coordinates.
To be as clear as possible regarding directions and coordinates, if a cow is in cell (x,y) and moves north, she ends up in cell (x,y+1). If she instead had moved east, she would end up in cell (x+1,y).

OUTPUT FORMAT (print output to the terminal / stdout):
Print N lines of output. Line i in the output should describe the number of cells worth of grass that the ith cow in the input eats. If a cow eats an infinite amount of grass, output “Infinity” for that cow.
SAMPLE INPUT:
6
E 3 5
N 5 3
E 4 6
E 10 4
N 11 2
N 8 1
SAMPLE OUTPUT:
5
3
Infinity
Infinity
2
5
SCORING:
In test cases 2-5, all coordinates are at most 100.
In test cases 6-10, there are no additional constraints.
Problem credits: Brian Dean

Farmer John 最近扩大了他的农场,从奶牛们的角度看来这个农场相当于是无限大了!奶牛们将农场上放牧的区域想作是一个由正方形方格组成的无限大二维方阵,每个方格中均有美味的草(将每个方格看作是棋盘上的一个方格)。Farmer John 的 N 头奶牛(1≤N≤50)初始时位于不同的方格中,一部分朝向北面,一部分朝向东面。
每一小时,每头奶牛会执行以下二者之一:

如果她当前所在的方格里的草已经被其他奶牛吃掉了,则她会停下。
吃完她当前所在的方格中的所有草,并向她朝向的方向移动一个方格。
经过一段时间,每头奶牛的身后会留下一条被啃秃了的轨迹。

如果两头奶牛在一次移动中移动到了同一个有草的方格,她们会分享这个方格中的草,并在下一个小时继续沿她们朝向的方向移动。

请求出每头奶牛吃到的草的数量。有些奶牛永远不会停下,从而吃到无限多的草。

输入格式(从终端/标准输入读入):
输入的第一行包含 N。以下 N 行,每行描述一头奶牛的起始位置,包含一个字符 N(表示朝向北面) 或 E(表示朝向东面),以及两个非负整数 x 和 y(0≤x≤109,0≤y≤109)表示方格的坐标。所有 x 坐标各不相同,所有 y 坐标各不相同。
为了使方向和坐标尽可能明确,如果一头奶牛位于方格 (x,y) 并向北移动,她会到达方格 (x,y+1)。如果她向东移动,她会到达方格 (x+1,y)。

输出格式(输出至终端/标准输出):
输出 N 行。输出的第 i 行包含输入中的第 i 头奶牛吃到草的方格的数量。如果一头奶牛可以吃到无限多的草,为这头奶牛输出 “Infinity”。
输入样例:
6
E 3 5
N 5 3
E 4 6
E 10 4
N 11 2
N 8 1
输出样例:
5
3
Infinity
Infinity
2
5
测试点性质:
测试点 2-5 中,所有坐标不超过 100。
测试点 6-10 没有额外限制。
供题:Brian Dean

我的代码如下(python)

#coding=utf-8
#author:Lishuyu time:2020年12月20日22:40:41
location = {
     }
max_x, max_y = 0,0
class cow():
    def __init__(self,direction,x,y):
        self.direction = direction
        self.x = x
        self.y = y
        self.grass = 0
        self.B = False
        self.locationstate = 0
        self.x_2 = 0
        self.y_2 = 0
    
    def move(self):
        if location.get(str(self.x)+' '+str(self.y)) == None:
            self.x_2, self.y_2 = self.x, self.y
            if self.direction == 'N':
                self.y += 1
            else:
                self.x += 1
            self.grass += 1
        self.locationstate = 1
        
            
    def eat(self):
        if self.locationstate == 1:
            location[str(self.x_2)+' '+str(self.y_2)] = 1
        else:
            self.B = True
        
        
                
N = int(input())
lis_name=[]
for i in range(N):
    name = 'cow'+str(i)
    lis_name.append(name)
for i in range(N):
    direction, x_axise, y_axise = map(str, input().split())
    x_axise, y_axise = int(x_axise), int(y_axise)
    lis_name[i] = cow(direction, x_axise, y_axise)
    if max_x < x_axise:
        max_x = x_axise
    if max_y < y_axise:
        max_y = y_axise
loop_r = max_x + max_y 
for i in range(loop_r):
    for i in range(N):
        lis_name[i].move()
    for i in range(N):
        lis_name[i].eat()
for i in range(N):
    k = lis_name[i].grass
    if k+1>loop_r :
        print('Infinity')
    else:
        print(k)

提示超时(6/10)

你可能感兴趣的:(python,usaco)