Problem:


Mysterious Gems

宇宙暦 1603〜1867 年，人々はその時代のことを EDO 時代と呼ぶ．EDO とは当時最先端の宇宙航行技術，Enhanced Driving Operation のことであり、1603 年に Dr.Izy によって開発された．

あなたは宇宙冒険家であり，宇宙を飛び回って様々な惑星を冒険していた．その冒険の途中で，あなたはとても不思議な惑星を発見した．その惑星には，至るところに七色に輝く不思議な宝石が落ちていた．あなたはその惑星への降下を試みようと考えたが，重大な問題のためにそれは不可能であることがわかった．その惑星の空気には，人間が触れただけで即死してしまうような猛毒の成分が含まれていたのだ．

そこで，あなたはロボットを使って宝石を回収することを考えついた．あなたはその惑星の周回軌道にて待機する．そして，降下させたロボットを遠隔操作することによって宝石を回収するのだ．あなたは，ロボットに「移動する方向」と「移動する距離」の組からなる命令の列によってロボットを遠隔操作する．ロボットは移動経路上（到達点を含む）に宝石を見つけると，それらを全て回収する．

あなたの仕事は，ロボットが与えられた全ての命令の実行を終えたときに，全ての宝石を回収することができたかどうかを判定するプログラムを書くことである．

なお，ロボットが不思議な宝石を回収する範囲はそれほど広くない．そのため，ロボットが移動する範囲は全て 2 次元の平面で表すことができる．そして，ロボットが移動する範囲は (0,0) および (20,20) をそれぞれ左下隅および右上隅とする正方形の内部（境界線を含む）である．ロボットは常に範囲の中央，すなわち (10,10) の座標に降下する．また，全ての宝石は中央以外の格子点上にあることが保証されている．

Input

入力は複数のデータセットにより構成される．

それぞれのデータセットの先頭行には，単一の正の整数 N (1 <= N <= 20) が含まれる。これはロボットが移動可能な範囲内にある不思議な宝石の個数を表す．次の N 行には，それぞれ xi および yi (0 <= xi , yi <= 20) が含まれ，これは i 番目の不思議な宝石の落ちている座標を表す．なお，1 つの場所に複数の宝石が落ちていることはない．

次の行には単一の整数 M (1 <= M <= 30) が含まれ，これはロボットに与えた命令の個数を表す．この後の M 行にはそれぞれ dj とひとつの正の整数 lj が含まれる．これは j 番目の命令における方向および移動量を表す．ただし，方向は N，E，S，W のいずれかの文字であり，順に北，東，南，西を表す（北は y 軸の正方向，東は x 軸の正方向）．なお，ロボットが移動可能な範囲を超えるような命令は与えられないことが保証されている．

入力は N = 0 のときに終了し，これはデータセットに含まれない．

Output

それぞれのデータセットについて，ロボットが全ての宝石を収集できるときは「Yes」と，そうでないときは「No」と 1 行に出力しなさい．

Sample Input

2
10 11
11 12
2
N 2
E 1
2
10 11
11 12
2
N 2
W 1
3
0 15
5 10
5 15
5
W 10
S 10
N 20
E 10
S 10
0

Output for the Sample Input

Yes
No
No



Sample input:
2
10 11
11 12
2
N 2
E 1
2
10 11
11 12
2
N 2
W 1
3
0 15
5 10
5 15
5
W 10
S 10
N 20
E 10
S 10
0


Sample output:
Yes
No
No

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
#aoj a

while 1:
    N = int(input())
    if N == 0:
        break
    A = [list(map(int,input().split())) for _ in range(N)]
    M = int(input())
    B = [list(input().split()) for _ in range(M)]
    D = []
    nx,ny = 10,10
    for i in range(M):
        if B[i][0] == "N":
            for j in range(1,int(B[i][1])+1):
                D.append([nx,ny+j])
            ny += int(B[i][1])
        elif B[i][0] == "E":
            for j in range(1,int(B[i][1])+1):
                D.append([nx+j,ny])
            nx += int(B[i][1])
        elif B[i][0] == "S":
            for j in range(1,int(B[i][1])+1):
                D.append([nx,ny-j])
            ny -= int(B[i][1])
        elif B[i][0] == "W":
            for j in range(1,int(B[i][1])+1):
                D.append([nx-j,ny])
            nx -= int(B[i][1])
    f = 1
    for i in A:
        if i not in D:
            f = 0
            break
    if f:
        print('Yes')
    else:
        print('No')

Submission status: Accepted

Solution code:
from collections import defaultdict

def check(D, STEP,  x, y):
    global moves_x, moves_y, oks

    if D == 'N':
        y += STEP
        for _y, i in moves_x.get(x, []):
            if y-STEP <= _y and _y <= y:
                oks[i] = 1
            else:
                continue
        return x, y
    elif D == 'S':
        y -= STEP
        for _y, i in moves_x.get(x, []):
            if y+STEP >= _y and _y >= y:
                oks[i] = 1
            else:
                continue
        return x, y
    elif D == 'E':
        x += STEP
        for _x, i in moves_y.get(y, []):
            if x-STEP <= _x and _x <= x:
                oks[i] = 1
            else:
                continue
        return x, y
    elif D == 'W':
        x -= STEP
        for _x, i in moves_y.get(y, []):
            if x+STEP >= _x and _x >= x:
                oks[i] = 1
            else:
                continue
        return x, y

ans = []
while 1:
    N = int(input())
    if N == 0: break
    moves_x = defaultdict(list)
    moves_y = defaultdict(list)
    oks = [0]*N

    for i in range(N):
        _x, _y = map(int, input().split())
        moves_x[_x] += [[_y, i]]
        moves_y[_y] += [[_x, i]]
    moves_x = dict(moves_x)
    moves_y = dict(moves_y)

    M = int(input())
    x, y = 10, 10
    for i in range(M):
        D, STEP = input().split()
        STEP = int(STEP)
        x, y = check(D, STEP, x, y)
    if 0 in oks:
        ans += ['No']
    else:
        ans += ['Yes']

print('\n'.join(ans))

Submission status: Accepted

Solution code:
def solve(N, xy, M, diredist):
    now = (10, 10)
    field = [[0]*21 for i in range(21)]
    field[now[0]][now[1]] = 1
    direction = {"N":(1, 0), "W":(0,-1), "S":(-1, 0), "E":(0, 1)}
    for i in range(M):
        dire = direction[diredist[i][0]]
        dist = int(diredist[i][1])
        for j in range(dist):
            field[now[0]+dire[0]][now[1]+dire[1]] = 1
            now = (now[0]+dire[0], now[1]+dire[1])
        
    for xi, yi in xy:
        if field[yi][xi]:
            continue
        else:
            return False
    
    return True
    

def main():
    ans = []
    while True:
        N = int(input())
        if (not N):
            break
        xy = [list(map(int, input().split())) for i in range(N)]
        M = int(input())
        diredist = [input().split() for i in range(M)]
        ans.append(solve(N, xy, M, diredist))
    
    for i in ans:
        print("Yes" if i else "No")


main()

Submission status: Accepted

Solution code:
while True :
    x=10
    y=10
    n=int(input())
    if n==0:break
    l=[input() for _ in range(n)]
    l2=["10 10"]
    n2=int(input())
    for i in range(n2):
        a,b=map(str,input().split())
        b=int(b)
        if a=="N":
            for i in range(b):
                y+=1
                l2.append("{} {}".format(x,y))
        elif a=="W":
            for j in range(b):
                x-=1
                l2.append("{} {}".format(x,y))
        elif a=="S":
            for m in range(b):
                y-=1
                l2.append("{} {}".format(x,y))
        else:
            for n in range(b):
                x+=1
                l2.append("{} {}".format(x,y))
    s=0
    for l3 in l:
        if l2.count(l3)==0:
            print("No")
            s=1
            break
    if s==0:
        print("Yes")

Submission status: Accepted

Solution code:
def set(Map, x, y) :
	for i in range(N) :
		Map[y[i]][x[i]] = 1
	return Map

def search(Map, dir, dis) :
	X = 10
	Y = 10
	for i in range(M) :
		if(dir[i] == "S") :
			if(Y + dis[i] <= 20) :
				for j in range(dis[i]+ 1) :
					Map[Y + j][X] = 0
				Y += dis[i]
			else :
				for j in range(20 - Y) :
					Map[Y + j][X] = 0
				Y = 20
		elif(dir[i] == "E") :
			if(X + dis[i] <= 20) :
				for j in range(dis[i]+ 1) :
					Map[Y][X + j] = 0
				X += dis[i]
			else :
				for j in range(20 - X) :
					Map[Y][X + j] = 0
				X = 20
		elif(dir[i] == "N") :
			if(Y - dis[i] >= 0) :
				for j in range(dis[i]+ 1) :
					Map[Y - j][X] = 0
				Y -= dis[i]
			else :
				for j in range(Y + 1) :
					Map[Y - j][X] = 0
				Y = 0
		else :
			if(X - dis[i] >= 0) :
				for j in range(dis[i]+ 1) :
					Map[Y][X - j] = 0
				X -= dis[i]
			else :
				for j in range(X + 1) :
					Map[Y][X - j] = 0
				X = 0
	return Map
			
while True :
	N = int(input())
	if(N == 0) :
		break
	else :
		Map = [[0 for i in range(21)] for j in range(21)]
		
		jewelryX = list()
		jewelryY = list()
		for i in range(N) :
			X, Y = map(int, input().split())
			jewelryX.append(X)
			jewelryY.append(20 - Y)
		
		M = int(input())
		order_dir = list()
		order_dis = list()
		for i in range(M) :
			dir, dis = map(str, input().split())
			# 方角 direction
			# 距離 distance
			order_dir.append(dir)
			order_dis.append(int(dis))
			
		set(Map, jewelryX, jewelryY)
		
		search(Map, order_dir, order_dis)
		
		S = 0
		for i in range(21) :
			S += sum(Map[i])
		
		if(S == 0) :
			print("Yes")
		else :
			print("No")

Submission status: Accepted

Solution code:
while True:
  n = int(input())
  if n==0:
    break

  grid = [[0 for i in range(21)] for i in range(21)]
  for i in range(n):
    x, y = list(map(int, input().split()))
    grid[x][y] = 1

  x, y = 10, 10
  grid[x][y] = 0
  m = int(input())
  for i in range(m):
    d, l = input().split()
    l = int(l)
    nx, ny = x, y
    if d=="N":
      ny += l
      for j in range(y, ny+1):
        grid[x][j] = 0
    elif d=="S":
      ny -= l
      for j in range(y, ny-1, -1):
        grid[x][j] = 0
    elif d=="E":
      nx += l
      for j in range(x, nx+1):
        grid[j][y] = 0
    else:
      nx -= l
      for j in range(x, nx-1, -1):
        grid[j][y] = 0
    x, y = nx, ny

  flag = True
  for i in range(21):
    for j in range(21):
      if grid[i][j] == 1:
        flag = False
  if flag:
    print("Yes")
  else:
    print("No")

Submission status: Accepted

Solution code:
dir_dict = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)}

while True:
    jewel_num = int(input())
    
    if jewel_num == 0:
        break
    
    jewel_coor_set = set()
    for i in range(jewel_num):
        jewel_coor_set.add(tuple(map(int, input().split(' '))))
    
    
    cmd_num = int(input())
    curr_coor = [10, 10]
    
    jewel_coor_set -= {(10, 10)}
    for i in range(cmd_num):
        move_cmd = input().split(' ')
        move_vec = dir_dict[move_cmd[0]]
        move_dist = int(move_cmd[1])
        
        for i in range(move_dist):
            curr_coor[0] += move_vec[0]
            curr_coor[1] += move_vec[1]       
            jewel_coor_set -= {tuple(curr_coor)}
            
    if not jewel_coor_set:
        print('Yes')
    else:
        print('No')

Submission status: Accepted

Solution code:
while 1:
    n = int(input())
    if n == 0:
        break

    jewel = []
    for _ in range(n):
        x, y = map(int, input().split())
        jewel.append([x, y])

    m = int(input())
    pos = [10, 10]
    for _ in range(m):
        news, dis = input().split()
        dis = int(dis)

        for i in range(dis):
            if news == "N":
                pos[1] += 1
            elif news == "E":
                pos[0] += 1
            elif news == "W":
                pos[0] -= 1
            else:
                pos[1] -= 1

            if pos in jewel:
                jewel.remove(pos)

    if jewel == []:
        print("Yes")
    else:
        print("No")

Submission status: Accepted

Solution code:
def check(rx, ry, gem):
    for k in range(len(gem)):
        if gem[k][0] == rx and gem[k][1] == ry:
            return k
        elif k == len(gem) - 1:
            return -1


while True:
    x = 10
    y = 10
    N = int(input())
    if N == 0:
        break
    Gem = []
    for i in range(N):
        X, Y = list(map(int, input().split()))
        Gem.append([X, Y])
    M = int(input())
    GemCount = [0]*N
    for i in range(M):
        C, m = input().split()
        if C == "N":
            for j in range(int(m)):
                y += 1
                reg = check(x, y, Gem)
                if reg != -1:
                    GemCount[reg] = 1
        elif C == "E":
            for j in range(int(m)):
                x += 1
                reg = check(x, y, Gem)
                if reg != -1:
                    GemCount[reg] = 1
        elif C == "S":
            for j in range(int(m)):
                y -= 1
                reg = check(x, y, Gem)
                if reg != -1:
                    GemCount[reg] = 1
        else:
            for j in range(int(m)):
                x -= 1
                reg = check(x, y, Gem)
                if reg != -1:
                    GemCount[reg] = 1
    if GemCount.count(0) == 0:
        print("Yes")
    else:
        print("No")

Submission status: Accepted

Solution code:
 
if __name__ == '__main__':
    while True:
        n = int(input())
        if n == 0:
            break
        flag = []
        for i in range(21):
            flag.append([0 for j in range(21)])
        for i in range(n):
            x,y = map(int,input().split())
            flag[x][y] = 1;
        m = int(input())
        nx,ny = 10,10
        for i in range(m):
            dre,num = input().split()
            num = int(num)
            if dre == 'N':
                for i in range(num):
                    ny += 1
                    if flag[nx][ny] == 1:
                        flag[nx][ny] = 0
            elif dre == 'W':
                for i in range(num):
                    nx -= 1
                    if flag[nx][ny] == 1:
                        flag[nx][ny] = 0
            elif dre == 'E':
                for i in range(num):
                    nx += 1
                    if flag[nx][ny] == 1:
                        flag[nx][ny] = 0
            else:
                for i in range(num):
                    ny -= 1
                    if flag[nx][ny] == 1:
                        flag[nx][ny] = 0
        ans = True
        for i in range(21):
            for j in range(21):
                if flag[i][j] == 1:
                    ans = False
        if ans:
            print('Yes')
        else:
            print('No')

Submission status: Accepted

Solution code:
# coding: utf-8
dic={'N':(0,1),'E':(1,0),'S':(0,-1),'W':(-1,0)}

def move(v,robot):
    return (robot[0]+dic[v][0],robot[1]+dic[v][1])
while True:
    n=int(input())
    if n==0:
        break
    field=[[False for i in range(21)] for i in range(21)]
    for i in range(n):
        x,y=map(int,input().split())
        field[y][x]=True
    m=int(input())
    robot=(10,10)
    if field[10][10]:
        n-=1
        field[10][10]=False
    for i in range(m):
        v,f=input().split()
        for j in range(int(f)):
            robot=move(v,robot)
            if field[robot[1]][robot[0]]:
                n-=1
                field[robot[1]][robot[0]]=False
    print('Yes' if n==0 else 'No')

Submission status: Accepted

Solution code:
while True:

    N = int(raw_input())
    if N == 0:
        break

    gems = []
    x, y = 10, 10
    direc = {
        "N": (0, 1),
        "E": (1, 0),
        "S": (0, -1),
        "W": (-1, 0)
    }

    for _ in range(N):
        xi, yi = map(int, raw_input().split())
        gems.append((xi, yi))
    M = int(raw_input())
    for _ in range(M):
        dat = raw_input().split()
        d, l = dat[0], int(dat[1])
        for _ in range(l):
            x += direc[d][0]
            y += direc[d][1]
            #print "check", x, y
            if (x, y) in gems:
                gems.remove((x, y))
    if gems == []:
        print "Yes"
    else:
        print "No"

Submission status: Runtime Error

Solution code:
while True:
&#160;&#160;&#160;&#160;num_hoseki=input()
&#160;&#160;&#160;&#160;if num_hoseki==0:
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;break
&#160;&#160;&#160;&#160;&#160;
&#160;&#160;&#160;&#160;hoseki_list=[]
&#160;&#160;&#160;&#160;for i in range(num_hoseki):
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;hoseki_list.append(map(int,raw_input().split()))
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;
&#160;&#160;&#160;&#160;num_meirei=input()
&#160;&#160;&#160;&#160;&#160;
&#160;&#160;&#160;&#160;keiro=[]
&#160;&#160;&#160;&#160;x,y=[10,10]
&#160;&#160;&#160;&#160;for i in range(num_meirei):
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;meirei=raw_input()
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;d, l=meirei.split()
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;l=int(l)
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;if d=='N':
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;dx,dy=0,1
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;elif d=='S':
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;dx,dy=0,-1
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;elif d=='W':
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;dx,dy=-1,0
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;elif d=='E':
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;dx,dy=1,0
&#160;
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;for j in range(l):
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;keiro.append([x+(j+1)*dx,y+(j+1)*dy])
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;x,y=keiro[-1]
&#160;&#160;&#160;&#160;flag=True
&#160;&#160;&#160;&#160;for hoseki in hoseki_list:
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;if not hoseki in keiro:
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;flag=False
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;break
    if flag:
        print 'Yes'
    else:
        print 'No'

Submission status: Runtime Error

Solution code:
while(1):
    N=int(raw_input())
    if N==0:
        break
    else:
        gems=[]
        okflag=0
        loc=[10,10]
        for i in range(N):
            [gemx,gemy]=[int(x) for x in raw_input().split()]
            gems.append([gemx,gemy])
        M=int(raw_input())
        for i in range(M):
            com=raw_input().split()
            com[1]=int(com[1])
            if com[0]=="N":
                for j in range(com[1]):
                    loc[1]=loc[1]+1
                    if loc in gems:
                        gems.remove(loc)
                #loc[1]=loc[1]+com[1]
            elif com[0]=="S":
                for j in range(com[1]):
                    loc[1]=loc[1]-1
                    if loc in gems:
                        gems.remove(loc)
                #loc[1]=loc[1]-com[1]
            elif com[0]=="E":
                for j in range(com[1]):
                    loc[0]=loc[0]+1
                    if loc in gems:
                        gems.remove(loc)

                #loc[0]=loc[0]+com[1]
            else:
                for j in range(com[1]):
                    loc[0]=loc[0]-1
                    if loc in gems:
                        gems.remove(loc)
                #loc[0]=loc[0]-com[1]
            if len(gems)==0:
                okflag=1
                break
        if okflag:
            print "Yes"
        else:
            print "No"

Submission status: Runtime Error

Solution code:
while 1:
    n=int(input())
    if n==0:break
    x=y=10
    b=[list(map(int,input().split())) for _ in range(n)]
    print(b)
    for i,j in [input().split() for _ in range(int(input()))]:
        for _ in range(int(j)):
            if 'N'<=i<='S': y+=1 if i=='N' else -1
            else: x+=1 if i=='E' else -1
            if [x,y] in b:del b[b.index([x,y])]
    print(['Yes','No'][len(b)])

Submission status: Runtime Error

Solution code:
while 1:
    n=int(input())
    if n==0:break
    x=y=10
    b=[list(map(int,input().split())) for _ in range(n)]
    for i,j in [input().split() for _ in range(int(input()))]:
        for _ in range(int(j)):
            if 'N'<=i<='S': y+=1 if i=='N' else -1
            else: x+=1 if i=='E' else -1
            if [x,y] in b:del b[b.index([x,y])]
    print(['Yes','No'][len(b)])

Submission status: Runtime Error

Solution code:
while (True):
    pos = [10, 10]
    result = 'No'
    N = int(input()) # Total number of jewels
    if N == 0:
        break
    jewelPositions = list()
    for i in range(N):
        jewelPos = list(map(int, input().split()))
        jewelPositions.append(jewelPos)

    M = int(input()) # Number of instructions
    # instructions = list()
    for i in range(M):
        # print('==== Currently at ', pos)
        instruction = input().split()
        instruction[1] = int(instruction[1])
        if (instruction[0] == 'E'):
            move = ('x', instruction[1])
        elif (instruction[0] == 'W'):
            move = ('x', -1*instruction[1])
        elif (instruction[0] == 'N'):
            move = ('y', instruction[1])
        elif (instruction[0] == 'S'):
            move = ('y', -1*instruction[1])
        else:
            raise NotImplementedError

        for jewel in jewelPositions:
            if ((move[0] == 'x') and (pos[1] == jewel[1]) and (pos[0] <= jewel[0] <= pos[0]+move[1])) \
                    or ((move[0] == 'y') and (pos[0] == jewel[0]) and (pos[1] <= jewel[1] <= pos[1]+move[1])):
                # print('---- Collecting a jewel at ', jewel)
                jewelPositions.remove(jewel)

        if (move[0] == 'x'):
            pos[0] += move[1]
        elif (move[0] == 'y'):
            pos[1] += move[1]

        if not jewelPositions:
            """
            if empty,
            all jewels are collected
            """
            result = 'Yes'
            break
        else:
            pass
        # print('jewels are left at ', jewelPositions)
    print(result)

Submission status: Runtime Error

Solution code:
while True:
    n = int(input())
    if n == 0:
        break
    p = [10, 10]
    l = [list(map(int,input().split())) for _ in range(n)]
    for d,s in [input.split() for _ in range(int(input()))]:
        for _ in range(s):
            if d == 'N':
                p[1] += 1
            elif d == 'E':
                p[0] += 1
            elif d == 'W':
                p[0] -= 1
            elif d == 'S':
                p[1] -= 1
            if p in l:
                l.remove(p)
    if len(l) == 0:
        print('Yes')
    else:
        print('No')

Submission status: Runtime Error

Solution code:
while True:
    n = int(input())
    if n == 0:
        break
    p = [10, 10]
    l = [list(map(int,input().split())) for _ in range(n)]
    for d,s in [input().split() for _ in range(int(input()))]:
        for _ in range(s):
            if d == 'N':
                p[1] += 1
            elif d == 'E':
                p[0] += 1
            elif d == 'W':
                p[0] -= 1
            elif d == 'S':
                p[1] -= 1
            if p in l:
                l.remove(p)
    if len(l) == 0:
        print('Yes')
    else:
        print('No')