#!/usr/bin/python # -*- coding: utf-8 -*- # NOI '12 - Day 2, Problem 3 # Triple Town (三重镇) # # Solution Checker for Experimentation # By Alex Li import collections import os import sys tileScore = [0, 4, 20, 100, 500, 1500, 5000, 20000, 100000, 500000] # Returns: the score of the put command def put(grid, n, m, putX, putY, tileLevel): if grid[putX][putY] != '.': sys.exit('Output Invalid!\nMust PUT on space.\n') score = tileScore[tileLevel] grid[putX][putY] = tileLevel while tileLevel < 9: visit = set() queue = collections.deque([(putX, putY)]) while queue: x, y = queue.popleft() if ((not 0 <= x < n) or (not 0 <= y < m) or (grid[x][y] == '.') or ((x, y) in visit) or (int(grid[x][y]) != tileLevel)): continue visit.add((x, y)) queue.append((x - 1, y)) queue.append((x + 1, y)) queue.append((x, y - 1)) queue.append((x, y + 1)) if len(visit) < 3: break for (x, y) in visit: grid[x][y] = '.' tileLevel += 1 grid[putX][putY] = tileLevel score += tileScore[tileLevel] return score def main(): if len(sys.argv) < 2: # sys.argv = ['', 'tritown0.in', 'tritown0.out'] sys.exit("Usage: tritown_check.py ") if (not os.path.isfile(sys.argv[1])) or (not os.path.isfile(sys.argv[2])): sys.exit('Input/Output File Does Not Exist!\n') fi = open(sys.argv[1], "r") fi.readline() # discard test case number n, m = map(int, fi.readline().strip().split()) p, q = map(int, fi.readline().strip().split()) grid = [[ ' ' ]*m for _ in xrange(n)] for i in range(n): line = fi.readline().strip() for j in range(m): grid[i][j] = line[j] k = int(fi.readline().strip()) seq = collections.deque(map(int, fi.readline().strip().split())) fi.close() score = 0 fo = open(sys.argv[2], "r") for line in fo: if not line: sys.exit('Output Invalid!\nNo END command.\n') line = line.strip() if line == 'END': break command = line[:line.index(' ')] x, y = map(int, line[line.index(' ') + 1:].split()) x -= 1 y -= 1 if (not 0 <= x < n) or (not 0 <= y < m): sys.exit('Output Invalid!\nCoordinates out of bounds.\n') if command == 'PUT': if len(seq) == 0: sys.exit('Output Invalid!\n.No more tiles in sequence.\n') score += put(grid, n, m, x, y, seq.popleft()) elif command == 'STAR': if p <= 0: sys.exit('Output Invalid!\n.Not enough stars.\n') p -= 1 maxLevel = 1 for (xx, yy) in [(x-1,y), (x,y-1), (x+1,y), (x,y+1)]: if ((not 0 <= xx < n) or (not 0 <= yy < m) or (grid[xx][yy] == '.')): continue maxLevel = max(maxLevel, int(grid[xx][yy])) score += put(grid, n, m, x, y, maxLevel) elif command == 'BOMBER': if q <= 0: sys.exit('Output Invalid!\n.Not enough bombs.\n') q -= 1 if grid[x][y] == '.': sys.exit('Output Invalid!\nMust bomb a tile.\n') score -= (tileScore[int(grid[x][y])] / 2) grid[x][y] = '.' else: sys.exit('Output Invalid!\nUnrecognized command.\n') fo.close() print 'Correct! Your score is %d' % score if __name__ == '__main__': main()