fork download
  1. def parse(inFile):
  2. return [inFile.getInts() for k in xrange(inFile.getInts()[0])]
  3.  
  4. def solve(grid):
  5. [w,h] = [len(grid[0]),len(grid)]
  6. dest = [[(i,j) for j in xrange(w)] for i in xrange(h)]
  7. for i in xrange(h):
  8. for j in xrange(w):
  9. if (i > 0) and grid[i-1][j] < grid[dest[i][j][0]][dest[i][j][1]]:
  10. dest[i][j] = (i - 1, j)
  11. if (j > 0) and grid[i][j-1] < grid[dest[i][j][0]][dest[i][j][1]]:
  12. dest[i][j] = (i, j - 1)
  13. if (j + 1 < w) and grid[i][j+1] < grid[dest[i][j][0]][dest[i][j][1]]:
  14. dest[i][j] = (i, j + 1)
  15. if (i + 1 < h) and grid[i+1][j] < grid[dest[i][j][0]][dest[i][j][1]]:
  16. dest[i][j] = (i + 1, j)
  17. for k in xrange(14):
  18. for i in xrange(h):
  19. for j in xrange(w):
  20. (i0,j0) = dest[i][j]
  21. dest[i][j] = dest[i0][j0]
  22. grid = [["" for g in row] for row in grid]
  23. labels = {}
  24. nextLabel = "a"
  25. for i in xrange(h):
  26. for j in xrange(w):
  27. if labels.has_key(dest[i][j]):
  28. grid[i][j] = labels[dest[i][j]]
  29. else:
  30. labels[dest[i][j]] = nextLabel
  31. grid[i][j] = nextLabel
  32. nextLabel = chr(ord(nextLabel) + 1)
  33. return "\n".join([""]+[" ".join(row) for row in grid])
  34.  
  35. if __name__ == "__main__":
  36. from GCJ import GCJ
  37. GCJ(parse, solve, "/Users/lpebody/gcj/2009_q/", "b").run()
  38.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty