y, x = self._i2c(i) if cell.pheromones: for vec in neighbors: yd, xd = vec i_n = self._c2i(y + yd, x + xd) if 0 <= i_n < self.height * self.width and self.cells[ i_n].pheromones: self.connect[i_n] = self.connect[i] def connected(self, y, x): return self.connect[self._c2i(y - 1, x - 1)] if __name__ == '__main__': H, W = map(int, input().split()) cells = [] for y in range(H): line = list(input()) for x in range(W): pheromones = line[x] == '#' cells.append(Cell(y, x, pheromones)) cage = Cage(H, W, cells) cage.connect_pheromones() N = int(input()) food_pos = [] for _ in range(N): y, x = map(int, input().split())
def move(self): self.y += self.direction[self.ref_direction][0] # 向いてる方角に対応した増分を足す self.x += self.direction[self.ref_direction][1] def turn_right(self): self.ref_direction = (self.ref_direction + 1) % 4 # 右を向いたら右にインデックスをずらす def turn_left(self): self.ref_direction = (self.ref_direction - 1) % 4 # 左を向いたら左にインデックスをずらす def get_pos(self): return self.y, self.x ## main H, W = map(int, input().split()) h0, w0 = map(int, input().split()) edo_map = [list(input()) for _ in range(H)] edo = City(edo_map, H, W) mouse_kid = Walker(h0 - 1, w0 - 1) # 内部では0開始 while True: y, x = mouse_kid.get_pos() if not edo.in_city(y, x): break # 町の外に出たら終了 if edo.resident(y, x) == '.': mouse_kid.turn_right() # 庶民の家なら右に回る else: mouse_kid.turn_left() # 富豪の家なら左に回る
# coding: utf-8 from my_input import input n = int(input()) dic = {} for _ in range(n): color, pair = input().split() sox = list(dic.get(color, (0, 0))) if pair == 'L': sox[0] += 1 elif pair == 'R': sox[1] += 1 dic[color] = tuple(sox) count = 0 for sox in dic.values(): count += min(sox) print(count)
# coding: utf-8 from my_input import input H, W = map(int, input().split()) garden = [] for _ in range(H): garden.append(list(input())) fence_count = 0 for i in range(H): for j in range(W): if garden[i][j] == '#': fence_count += 4 if i-1 >=0 and garden[i-1][j] == '#': fence_count -= 1 if i+1 < H and garden[i+1][j] == '#': fence_count -= 1 if j-1 >=0 and garden[i][j-1] == '#': fence_count -= 1 if j+1 < W and garden[i][j+1] == '#': fence_count -= 1 print(fence_count)
def next_position(pos, ring): return pos + 1 if pos + 1 < len(ring) else 0 def next_vacant(pos, ring): tmp_position = next_position(pos, ring) while ring[tmp_position] != 0: tmp_position = next_position(tmp_position, ring) return tmp_position ## main brush_count, rabbits, turns = map(int, input().split()) brush = [0] * brush_count for i in range(1, rabbits + 1): brush[int(input()) - 1] = i for _ in range(turns): for i in range(1, rabbits + 1): current_pos = brush.index(i) next_pos = next_vacant(current_pos, brush) brush[current_pos] = 0 brush[next_pos] = i for i in range(1, rabbits + 1): print(brush.index(i) + 1)
# coding: utf-8 from my_input import input n, m, k = map(int, input().split()) masu = [[0, 0]] for _ in range(1, n - 1): masu.append(list(map(int, input().split()))) masu += [[0, 0]] player = [[0, 0, 0] for _ in range(m)] winner = [] winner_coin = 0 award = 3 for _ in range(k): roll = list(map(int, input().split())) for i in range(m): pos = player[i][0] + roll[i] player[i][1] += player[i][2] * roll[i] if pos >= n: player[i][2] += award award = max(award, 0) pos = n - 1 player[i][1] = max(player[i][1] + masu[pos][1], 0) pos = pos + masu[pos][0] if pos >= n: player[i][2] += award award = max(award, 0) pos = n - 1 elif pos <= 0: pos == 0 player[i][0] = pos
if 0 <= y + dy < self.height and 0 <= x + dx < self.width: next_cell = self.cells[self.c2i(y + dy, x + dx)] return next_cell == '.' else: return False def move(self): y, x = self.pos self.stamp(y, x) # 今いる位置に踏破済みマークを入れる for i in range(3): for _ in range(i): # 0回右ターン:正面、1回右ターン:右向き、更に2回右ターン:左向き self.robot.turn_right() dy, dx = self.robot.step() if self.can_move(y + dy, x + dx): # 行けるマスなら self.pos = (y + dy, x + dx) # 移動 return # 行けるマスがなければ位置は不変(その場に留まる) if __name__ == "__main__": H, W = map(int, input().split()) cells = [t for _ in range(H) for t in list(input())] board = Board(H, W, cells, Robot()) current_pos = (-1, -1) while (current_pos != board.pos): current_pos = board.pos board.move() board.show_pos()
class Task: def __init__(self, priority, duration, start, end): self.priority = priority self.remain = duration self.start = start self.end = end def can_do(self, current): return self.start <= current <= self.end def complete(self): return self.remain == 0 N = int(input()) input_line = [list(map(int, input().split())) for _ in range(N)] tasks = [Task(p, d, s, e) for p, (d, s, e) in enumerate(input_line)] dummy = Task(1000, 1000, 0, 1000) current = dummy for time in range(1000): for task in tasks: if task.can_do(time) and task.priority < current.priority: current = task if current.can_do(time): current.remain -= 1 if current.complete(): tasks.remove(current) current = dummy if not tasks:
# coding: utf-8 from my_input import input def show_output_list(output_list): if output_list: for num in sorted(output_list): print(num) else: print('None') strnum = input() move_inner = {'0':['6', '9'], '1':[], '2':['3'], '3':['2', '5'], '4':[], '5':['3', '5'], '6':['0', '9'], '7':[], '8':[], '9':['0', '6']} can_add = {'0':['8'], '1':['7'], '2':[], '3':['9'], '4':[], '5':['6', '9'], '6':['8'], '7':[], '8':[], '9':[]} can_sub = {'0':[], '1':[], '2':[], '3':[], '4':[], '5':[], '6':['5'], '7':['1'], '8':['0', '6'], '9':['3', '5']} output_list = [] for i in range(len(strnum)): changeable = move_inner[strnum[i]] for c in changeable: newstr = strnum[:i] + c + strnum[i+1:] output_list.append(newstr) def addable(c): return len(can_add[c]) != 0 def subable(c): return len(can_sub[c]) != 0 a = []
def have_intersection(self, other): x = [self.x0, self.x1, other.x0, other.x1] y = [self.y0, self.y1, other.y0, other.y1] if self.width + other.width >= max(x) - min(x) and \ self.height + other.height >= max(y) - min(y): return True return False def connect(self, other): self.connected |= other.connected other.connected |= self.connected pass if __name__ == "__main__": N = int(input()) idx0 = int(input()) - 1 ponds = [] for i in range(N): pond = Pond(i, *map(int, input().split())) ponds.append(pond) for _ in range(50): comb = itertools.combinations(ponds, 2) for (c1, c2) in comb: if c1.have_intersection(c2): c1.connect(c2) for id in ponds[idx0].connected: print(id + 1)
neighbors_height = self.get_height(y + yd, x + xd) if neighbors_height < current_height: self.drop(y + yd, x + xd) break else: self.stack(y, x) def stack(self, y, x): position = self.coordinate2index(y, x) if position >= 0: self.cells[position].z += 1 def get_height(self, y, x): position = self.coordinate2index(y, x) if position >= 0: return self.cells[position].z return 500000 if __name__ == '__main__': H, W, N = map(int, input().split()) cells = [Cell(y, x, 0) for y in range(H) for x in range(W)] diorama = Diorama(H, W, cells) for _ in range(N): x, y = map(int, input().split()) diorama.drop(y - 1, x - 1) diorama.draw()
# coding: utf-8 from my_input import input n, m = map(int, input().split()) count = 0 max_count = 0 incombo = True for _ in range(m): score, play = input().split() if score == play: count += 1 elif '#' in score: plain = score.replace('#', '+') if plain == play and incombo: count += 1 else: incombo = False count = 0 else: incombo = True count = 0 max_count = max(max_count, count) print(max_count)
class Square: def __init__(self, extend=0, coin=0): self.extend = extend self.coin = coin class Sugoroku: def __init__(self, road): self.road = road self.start = 0 self.goal = len(road) n, m, k = map(int, input().split()) road_map = [Square()] # start for _ in range(n - 2): extend, coin = map(int, input().split()) road_map.append(Square(extend, coin)) road_map.append(Square()) # goal player = [Player(road_map) for _ in range(m)] for _ in range(k): roll = list(map(int, input().split())) for i in range(m): p, r = player[i], roll[i] p.get_award(r) p.move(r) # ダイスの目分進む
from my_input import input class Robot: def __init__(self): self.step_dic = [[0,1], [1,0], [0,-1], [-1,0]] self.dic_idx = 0 class Map: def __init__(self, squares): self.pos = [0,0] self.squares = squares class Square: def __init__(self, type, unexplored=True): self.type = type H, W = map(int, input().split()) lines = [] for _ in range(H): line = list(input()) for s in line:
# coding: utf-8 from my_input import input import pprint import re def parse_log(log): address = '[0-9]{,3}.[0-9]{,3}.[0-9]{,3}.[0-9]{,3}' postCode = re.match(address, log) print(postCode.group()) segment = map(int, input().split()) n = int(input()) log = [] for _ in range(n): input_line = input() parse_log(input_line) log.append(input_line)ire
# coding: utf-8 from my_input import input def connect(pos1, pos2, pond_list): x11, y11, x12, y12 = pond_list[pos1] x21, y21, x22, y22 = pond_list[pos2] if x11 < x12 < x21 < x22 or y11 < y12 < y21 < y22 or \ x21 < x22 < x11 < x12 or y21 < y22 < y11 < y12: return False else: return True pond = int(input()) position = int(input()) pond_list = [] for idx in range(pond): pond_list.append(list(map(int, input().split()))) root = [position - 1] c_root = [] while root != c_root: c_root = set(root) for pos in root: for i in range(pond): if connect(pos, i, pond_list) and i not in root: root.append(i) root = set(root) for pos in root:
def __init__(self, price, stock=0): self.price = price self.stock = stock def can_buy(self, amount): return self.stock >= amount def buy(self, amount): self.stock -= amount return self.price * amount def bake(self, amount): self.stock += amount n, q = map(int, input().split()) # initialize bread lists bread = [] for _ in range(n): price, stock = map(int, input().split()) bread.append(Bread(price, stock)) # process queries for _ in range(q): query, *amount = input().split() amount = [int(i) for i in amount] if query == 'bake': for i in range(n): bread[i].bake(amount[i]) elif query == 'buy': if False in [bread[i].can_buy(amount[i]) for i in range(n)]:
def sidechange(self): tmp = self.offence self.offence = list(map(lambda x:-x, self.diffence)) self.diffence = list(map(lambda x:-x, self.tmp)) def getoffsideline(self): return self.diffence.getlastline() def offsideplayers(self, passer): passer_x = self.offence.getposition(passer) offside_x = self.getoffsideline() offside = [] for idx, player in enumerate(self.offence.players): if passer_x < offside_x < player.x: offside.append(idx + 1) if not offside: offside,append('None') return offside # main offence, passer = input().split() teams = [] for _ in range(2): p_list = list(map(int, input().split())) teams.append(Team(list(map(Player, p_list)))) game = Game(teams) if offence == 'B': game.sidechange() for p in game.offsideplayers(int(passer)): print(p)
from my_input import input def have2holidays(start, end, calendar): s = max(start, 0) e = min(end, len(calendar)) return calendar[s:e].count(0) >= 2 N = int(input()) days = list(map(int, input().split())) count = 0 for i in range(N): for j in range(i - 6, i + 1): if have2holidays(j, j + 7, days): count += 1 break print(count)
# coding: utf-8 from my_input import input import math oy, speed, angle_d = map(int, input().split()) d_x, d_y, r = map(int, input().split()) G = 9.8 angle_r = math.radians(angle_d) vx = speed * math.cos(angle_r) vy = speed * math.sin(angle_r) t = d_x / vx y = oy + vy * t - G * t**2 / 2 diff = y - d_y if -r <= diff <= r: print('Hit', round(diff, 1)) else: print('Miss')
def is_offside(x_player, x_passer, x_offside_line): return x_passer < x_offside_line < x_player def get_offside_line(diffence_team): return sorted(diffence_team, reverse=True)[1] def get_opponent(name): return 'A' if name == 'B' else 'B' # main offence_name, passer = input().split() team = {} team['A'] = list(map(int, input().split())) team['B'] = list(map(int, input().split())) if offence_name == 'B': for t in team: t = list(map(lambda x: -x, t)) diffence_name = get_opponent(offence_name) offence_team = team[offence_name] diffence_team = team[diffence_name] x_offside_line = get_offside_line(diffence_team) x_passer = int(offence_team[int(passer) - 1])
from my_input import input class Program: def __init__(self, start, duration): self.start = start self.duration = duration self.playing = range(start, start + duration + 1) class TimeTable: def __init__(self, program_list): self.program_list = program_list self.make_time_table() def make_time_table(self): self.program_list = sorted(self.program_list, key=lambda x: x.start) N = int(input()) program_list = [ Program(s, d) for s, d in [map(int, input().split()) for _ in range(N)] ] table = TimeTable(program_list) print(table)
# coding: utf-8 from my_input import input def sort_record(record): record = dict(sorted(record.items(), key=lambda x: x[0])) record = dict(sorted(record.items(), key=lambda x: x[1], reverse=True)) return record n, run_count, rank = map(int, input().split()) prev_record = {} last_record = {} for _ in range(n): name, mileage = input().split() prev_record[name] = int(mileage) last_record[name] = 0 prev_record = sort_record(prev_record) for _ in range(run_count): date, name, mileage = input().split() last_record[name] = last_record.get(name, 0) + int(mileage) last_record = sort_record(last_record) for idx, item in enumerate(last_record.items()): name, length = item[0], item[1] prev_rank = list(prev_record.keys()).index(name) comment = 'same' if idx < prev_rank: comment = 'up'
from my_input import input class Mount: def __init__(self, width, points): self.width = width self.points = points def c2i(self, y, x): # coodinate to index return y * self.width + x def height(self, y, x): if y < 0 or x < 0 or y >= self.width or x >= self.width: return 0 return self.points[self.c2i(y, x)] N = int(input()) points = [h for _ in range(N) for h in map(int, input().split())] mount = Mount(N, points) peaks = [] for y in range(N): for x in range(N): around = [mount.height(y-1,x), mount.height(y+1,x), mount.height(y,x-1), mount.height(y,x+1)] if mount.height(y,x) > max(around): peaks.append(mount.height(y,x)) for h in sorted(peaks, reverse=True): print(h)
from my_input import input if __name__ == "__main__": N, M = map(int, input().split()) items = [list(map(int, input().split())) for _ in range(N)] score_list = [0] * N for j in range(M): # 指標ごとに処理 eval_list = sorted([[items[i][j], i] for i in range(N)], reverse=True) idx = 0 eval_tmp = 0 for i, r in enumerate(eval_list): if r[0] != eval_tmp: # 一つ前と評価が異なれば idx = i + 1 # 順位を振り直す score_list[r[1]] += N - idx eval_tmp = r[0] # score順、item順に並べる evaluation = sorted(list(zip(score_list, list(range(N)))), key=lambda x: (-x[0], x[1])) for score, item in evaluation: print(item + 1)
from my_input import input h, w = map(int, input().split()) result = [r for _ in range(h) for r in input()] score = [s for _ in range(h) for s in map(int, input().split())] print(sum([s for r, s in zip(result, score) if r == 'o']))
def set_by_decimal(self, decimal): self.decimal = decimal str_deci = str(self.decimal).zfill(self.digit) t_pattern = [Soroban.dic_deci[int(c)] for c in str_deci] self.pattern = self.transepose(t_pattern) def show(self): for line in self.pattern: print(''.join(line)) def make_matrix(self, x, y, init='?'): return [[init] * x for _ in range(y)] def transepose(self, matrix): x, y = len(matrix[0]), len(matrix) t_matrix = self.make_matrix(y, x) for i in range(y): for j in range(x): t_matrix[j][i] = matrix[i][j] return t_matrix digit = int(input()) soroban_a = Soroban(digit, [input() for _ in range(Soroban.size)]) soroban_b = Soroban(digit, [input() for _ in range(Soroban.size)]) total = soroban_a + soroban_b total.show()
def reflect(self): self.swap_lit(1, 5) self.swap_lit(2, 4) return self def show(self): print(' ' + '_' if self.lit[0] == '1' else ' ' + ' ') print(('|' if self.lit[5] == '1' else ' ') + ('_' if self.lit[6] == '1' else ' ') + ('|' if self.lit[1] == '1' else ' ')) print(('|' if self.lit[4] == '1' else ' ') + ('_' if self.lit[3] == '1' else ' ') + ('|' if self.lit[2] == '1' else ' ')) print() def judge_correct_numbers(num1, num2): print('Yes' if num1.is_correct_number() and num2.is_correct_number() else 'No') ## main digit1 = list(input().split()) stay, ref, rot = Segments(digit1), Segments(digit1), Segments(digit1) print(id(stay), id(ref), id(rot)) stay.show() ref.reflect().show() rot.rotate().show() print(id(stay), id(ref), id(rot))
for i in range(len(query)): stock_list[i] += int(query[i]) def buy_bread(query): for i in range(len(query)): if stock_list[i] < int(query[i]): return -1 sum = 0 for i in range(len(query)): stock_list[i] -= int(query[i]) sum += price_list[i] * int(query[i]) return sum N, Q = map(int, input().split()) # N: variety of bread, Q: number of queries price_list = [] # price of bread stock_list = [] # stock of bread # initialize lists for i in range(N): price, stock = map(int, input().split()) price_list.append(price) stock_list.append(stock) # process queries for i in range(Q): query = input().split() command = query.pop(0) if command == "bake":
from my_input import input def coin_count(pay): coins = [500, 100, 50, 10, 5, 1] coin_count = 0 while pay: for coin in coins: coin_count += pay // coin pay -= pay // coin * coin return coin_count price = int(input()) min_pay = 21 for y500 in range(3): for y100 in range(5): for y50 in range(2): for y10 in range(5): for y5 in range(2): for y1 in range(5): pay = y500 * 500 + y100 * 100 + y50 * 50 + y10 * 10 + y5 * 5 + y1 * 1 if pay >= price: pay_count = y500 + y100 + y50 + y10 + y5 + y1 charge_count = coin_count(pay - price) min_pay = min(pay_count + charge_count, min_pay) print(min_pay)