- Translate each rule into one explicit state update.
- Maintain the invariant after every processed item.
- Return the accumulated state once all relevant input has been handled.
Code notes
- 82 lines of Python from the credited upstream file ccc11j4.py.
- The implementation keeps its working state in language-native values and containers.
- No explicit loop blocks detected.
Complexity
Count the number and nesting of passes over the input, then include the maintained containers in the memory estimate.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
1234 567 89wellPlan = {}10for row in range(-1,-201,-1):11 for col in range (-200,201):12 wellPlan[(row,col)] = False 13wellPlan[(-1,0)] = True14wellPlan[(-2,0)] = True15wellPlan[(-3,0)] = True16wellPlan[(-3,1)] = True17wellPlan[(-3,2)] = True18wellPlan[(-3,3)] = True19wellPlan[(-4,3)] = True20wellPlan[(-5,3)] = True21wellPlan[(-5,4)] = True22wellPlan[(-5,5)] = True23wellPlan[(-4,5)] = True24wellPlan[(-3,5)] = True25wellPlan[(-3,6)] = True26wellPlan[(-3,7)] = True27wellPlan[(-4,7)] = True28wellPlan[(-5,7)] = True29wellPlan[(-6,7)] = True30wellPlan[(-7,7)] = True31wellPlan[(-7,6)] = True32wellPlan[(-7,5)] = True33wellPlan[(-7,4)] = True34wellPlan[(-7,3)] = True35wellPlan[(-7,2)] = True36wellPlan[(-7,1)] = True37wellPlan[(-7,0)] = True38wellPlan[(-7,-1)] = True39wellPlan[(-6,-1)] = True40wellPlan[(-5,-1)] = True41 4243line = raw_input()44direction = line[0:1]45distance = eval(line[2:])46 47ok = True48row = -549col = -150 5152while ok and direction != "q":53 dr = 054 dc = 055 if direction == "l":56 dc = -1 57 elif direction == "u":58 dr = 159 elif direction == "r":60 dc = 161 elif direction == "d":62 dr = -163 64 while distance > 0:65 row = row + dr66 col = col + dc67 if wellPlan[(row,col)]:68 ok = False69 else:70 wellPlan[(row,col)] = True71 distance = distance - 172 73 if ok:74 print col, row, "safe"75 else:76 print col, row, "DANGER"77 78 line = raw_input()79 direction = line[0:1]80 distance = eval(line[2:])81 82