-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblemA_edit.py
146 lines (126 loc) · 3.07 KB
/
problemA_edit.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
Route_list=[]
grid_list=[]
dict={}
dict_cell={}
str_1='NSEW'
dict_pos={'A':0,'B':1,'C':2,'D':3,'E':4,'F':5,'G':6,'H':7}
def Grid(pos, cmd):
row=dict_pos[pos[0]]
column=int(pos[1])
ans_row,ans_col=None,None
for x in range(0,8):
for y in range(0,8):
dict={}
for z in str_1:
if row==x and column==y :
if cmd==z:
dict[z]=1
ans_row,ans_col=x,y
#print ans_row,ans_col
elif cmd==None and z=='N':
dict[z]=1
ans_row,ans_col=x,y
else:
dict[z]=0
else:
dict[z]=0
x=repr(x)
y=repr(y)
dict_cell[x+y]=dict
x=int(x)
#import pdb;pdb.set_trace()
grid_list.append(dict_cell)
return ans_row,ans_col
def Initialize(pos,cmd=None):
row,col=Grid(pos,cmd)
row=[k for k, v in dict_pos.iteritems() if v == int(row)][0]
if not isinstance(col,str):
col=repr(col)
print row+col
Route_list.append(row+col)
return row+col
#initialize('A0')
def Row_Column_New(pos,direc):
#import pdb;pdb.set_trace()
if direc=='N':
#import pdb;pdb.set_trace()
column=int(pos[1])
if dict_pos[pos[0]] !=0:
row=dict_pos[pos[0]]-1
else:
row=dict_pos[pos[0]]+7
elif direc=='S':
column=int(pos[1])
if dict_pos[pos[0]]!=7:
row=dict_pos[pos[0]]+1
else:
row=dict_pos[pos[0]]-7
elif direc=='E':
if int(pos[1])!=7:
column=int(pos[1])+1
else:
column=int(pos[1])-7
row=dict_pos[pos[0]]
elif direc=='W':
if int(pos[1])!=0:
column= int(pos[1])-1
else:
column= int(pos[1])+7
row=dict_pos[pos[0]]
return repr(row),repr(column)
def Move_Arrow(pos):
row=repr(dict_pos[pos[0]])
column=pos[1]
for x in grid_list:
data_dict=x[row+column]
#import pdb;pdb.set_trace()
for direc,val in data_dict.iteritems():
if val==1:
row_move,column_move=Row_Column_New(pos,direc)
row_move=[k for k, v in dict_pos.iteritems() if v == int(row_move)][0]
return Initialize(row_move+column_move,direc)
def Left_Right_Arrow(pos,st):
#import pdb;pdb.set_trace()
row=repr(dict_pos[pos[0]])
column=pos[1]
for x in grid_list:
data_dict=x[row+column]
for direc,val in data_dict.iteritems():
if st=='L' and val==1:
if direc =='N':
return Initialize(pos,'W')
elif direc == 'E':
return Initialize(pos,'N')
elif direc == 'S':
return initialize(pos,'E')
elif direc == 'W':
return Initialize(pos,'S')
elif st=='R' and val==1:
if direc =='N':
return Initialize(pos,'E')
elif direc == 'E':
return Initialize(pos,'S')
elif direc == 'S':
return Initialize(pos,'W')
elif direc == 'W':
return Initialize(pos,'N')
def Action_string(pos,string):
pos=Initialize(pos)
for st in string:
#import pdb;pdb.set_trace()
if st =='M':
pos=Move_Arrow(pos)
elif st =='L' or st == 'R':
Left_Right_Arrow(pos,st)
Action_string('B2','MRMLMRMRMRMMMMMMMLRRR')
Coloured_Cell_List=['A0','A1','A2','A3','A4','A5','A6','A7',
'B0','C0','D0','E0','F0','G0','H0','H1',
'H2','H3','H4','H5','H6','H7','B7','C7','D7','E7','G7','H7']
#import pdb;pdb.set_trace()
for route in Route_list:
if route in Coloured_Cell_List:
Lost=True
if Lost:
print "LOST"
else:
print "WON"