750B. New Year and North Pole

B. New Year and North Pole
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

In this problem we assume the Earth to be a completely round ball and its surface a perfect sphere. The length of the equator and any meridian is considered to be exactly 40 000 kilometers. Thus, travelling from North Pole to South Pole or vice versa takes exactly 20 000 kilometers.

Limak, a polar bear, lives on the North Pole. Close to the New Year, he helps somebody with delivering packages all around the world. Instead of coordinates of places to visit, Limak got a description how he should move, assuming that he starts from the North Pole. The description consists of n parts. In the i-th part of his journey, Limak should move ti kilometers in the direction represented by a string diri that is one of: “North”, “South”, “West”, “East”.

Limak isn’t sure whether the description is valid. You must help him to check the following conditions:

If at any moment of time (before any of the instructions or while performing one of them) Limak is on the North Pole, he can move only to the South.
If at any moment of time (before any of the instructions or while performing one of them) Limak is on the South Pole, he can move only to the North.
The journey must end on the North Pole. 

Check if the above conditions are satisfied and print “YES” or “NO” on a single line.
Input

The first line of the input contains a single integer n (1 ≤ n ≤ 50).

The i-th of next n lines contains an integer ti and a string diri (1 ≤ ti ≤ 106, ) — the length and the direction of the i-th part of the journey, according to the description Limak got.
Output

Print “YES” if the description satisfies the three conditions, otherwise print “NO”, both without the quotes.
Examples
Input

5
7500 South
10000 East
3500 North
4444 West
4000 North

Output

YES

Input

2
15000 South
4000 East

Output

NO

Input

5
20000 South
1000 North
1000000 West
9000 North
10000 North

Output

YES

Input

3
20000 South
10 East
20000 North

Output

NO

Input

2
1000 North
1000 South

Output

NO

Input

4
50 South
50 North
15000 South
15000 North

Output

YES

Note

Drawings below show how Limak’s journey would look like in first two samples. In the second sample the answer is “NO” because he doesn’t end on the North Pole.
这里写图片描述
题意:在南极的话第一步在南极的话就是错误的在北极也是这样,最终的终点一定要在北极。

#include<stdio.h>
int main()
{
    int n,x;
    char str[6];
    while(~scanf("%d",&n))
    {
        int sum=0;
        int judge=0;
        while(n--)
        {
            scanf("%d%s",&x,str);
            if(str[0]=='S')
            {
                if(sum==20000||sum+x>20000)
                {
                    judge=1;
                }
                sum+=x;

            }
            if(str[0]=='N')
            {
                if(sum==0||sum-x<0)
                {
                    judge=1;
                }
                sum-=x;
            }
            if(str[0]=='W')
            {
                if(sum==0||sum==20000)
                    judge=1;
            }
            if(str[0]=='E')
            {
                if(sum==0||sum==20000)
                    judge=1;
            }
        }
        if((sum==0)&&(judge==0))
            printf("YES\n");
        else
            printf("NO\n");

    }
      return 0;

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
请优化下面的代码:import turtle # 控制台显示部分 print("Hanoi Tower Game") # 获取用户输入 n = int(input("请输入盘子的个数:")) # 初始化三个柱子 a = list(range(n, 0, -1)) b, c = [], [] # 定义移动函数 def move(n, source, target, auxiliary): if n > 0: # 移动 n-1 个盘子到辅助柱子 move(n-1, source, auxiliary, target) # 将最大的盘子移动到目标柱子 target.append(source.pop()) # 显示移动过程 print("Move disk", n, "from", source, "to", target) # 移动 n-1 个盘子从辅助柱子到目标柱子 move(n-1, auxiliary, target, source) # 开始移动 move(n, a, c, b) # turtle部分 screen = turtle.Screen() screen.setup(600, 600) screen.bgcolor("white") # 绘制柱子 pole1 = turtle.Turtle() pole1.hideturtle() pole1.speed(0) pole1.penup() pole1.goto(-150, -200) pole1.pendown() pole1.width(5) pole1.color("black") pole1.left(90) pole1.forward(400) pole2 = pole1.clone() pole2.penup() pole2.goto(0, -200) pole2.pendown() pole2.forward(400) pole3 = pole1.clone() pole3.penup() pole3.goto(150, -200) pole3.pendown() pole3.forward(400) # 绘制盘子 colors = ["red", "green", "blue", "yellow", "purple", "orange"] turtles = [] for i in range(n): t = turtle.Turtle() t.hideturtle() t.shape("square") t.color(colors[i%6]) t.shapesize(1, (n-i)*2, 1) t.penup() t.goto(-150, -200+(i+1)*20) t.pendown() turtles.append(t) # 移动盘子 def move_turtle(disk, source, target): disk.penup() disk.goto(source, 200) disk.pendown() disk.goto(target, 200) disk.goto(target, -200+len(target)*20) # 开始移动 for i in range(2**n-1): disk = turtles[a.index(n-i)] move_turtle(disk, disk.xcor(), -150) a.remove(n-i) b.append(n-i) disk_index = a.index(n-i-1) if (n-i-1) in a else b.index(n-i-1) disk = turtles[disk_index] move_turtle(disk, disk.xcor(), pole_positions[disk_index]) if (n-i-1) in a: a.remove(n-i-1) else: b.remove(n-i-1) c.append(n-i-1) disk_index = a.index(n-i) if (n-i) in a else b.index(n-i) disk = turtles[disk_index] move_turtle(disk, disk.xcor(), pole_positions[disk_index]) if (n-i) in a: a.remove(n-i) else: b.remove(n-i) c.append(n-i) # 等待用户关闭窗口 screen.mainloop()
05-31

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值