Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.50 KB | None | 0 0
  1. import java.awt.Point;
  2. import java.io.*;
  3. import java.util.HashSet;
  4. import java.util.LinkedList;
  5. import java.util.Scanner;
  6.  
  7.  
  8. public class F {
  9. public static void main(String args[]) throws IOException {
  10. new F().doIt();
  11. }
  12.  
  13. public void doIt() throws IOException {
  14. BufferedReader in = new BufferedReader(new FileReader("F.in"));
  15. PrintStream out = new PrintStream(new File("F.out"));
  16.  
  17. String input = in.readLine();
  18. while(input != null && !input.equals("")) {
  19. int row, col;
  20. String[] parsed = input.split(" +");
  21. row = Integer.parseInt(parsed[0]);
  22. col = Integer.parseInt(parsed[1]);
  23.  
  24. int[][][] effect = new int[10][row][col];
  25. HashSet<Point>[] lists = new HashSet[10];
  26. for (int x=0; x<10; x++)
  27. lists[x] = new HashSet<Point>();
  28.  
  29. for (int x=0; x<row; x++) {
  30. input = in.readLine();
  31. for (int y=0; y<col; y++) {
  32. lists[input.charAt(y)-'0'].add(new Point(x, y));
  33. }
  34. }
  35.  
  36. int[] dx = {0, 1, -1, 0};
  37. int[] dy = {1, 0, 0, -1};
  38. for (int type=0; type<10; type++) {
  39. LinkedList<Magic> queue = new LinkedList<Magic>();
  40. for (Point p : lists[type]) {
  41. queue.add(new Magic(p, 0, p));
  42. effect[type][p.x][p.y] = -1;
  43. }
  44.  
  45. while(!queue.isEmpty()) {
  46. Magic cur = queue.poll();
  47.  
  48. for (int d=0; d<4; d++) {
  49. if (cur.loc.x + dx[d] >= 0 && cur.loc.x + dx[d] < row &&
  50. cur.loc.y + dy[d] >= 0 && cur.loc.y + dy[d] < col) {
  51. int nextRow = cur.loc.x + dx[d];
  52. int nextCol = cur.loc.y + dy[d];
  53. if (effect[type][nextRow][nextCol] == 0) {
  54. effect[type][nextRow][nextCol] = cur.dist + 1;
  55. queue.add(new Magic(cur.src, cur.dist + 1, new Point(nextRow, nextCol)));
  56. }
  57. else if (effect[type][cur.src.x][cur.src.y] == -1) {
  58. if (lists[type].contains(new Point(nextRow, nextCol)))
  59. effect[type][cur.src.x][cur.src.y] = 1;
  60. else
  61. effect[type][cur.src.x][cur.src.y] = cur.dist + effect[type][nextRow][nextCol]+1;
  62. }
  63.  
  64. }
  65. }
  66. }
  67. }
  68. int ans = 0;
  69. for (int x = 0; x < row; x++) {
  70. for (int y=0; y < col; y++) {
  71. for (int z=0; z<10; z++) {
  72. ans += effect[z][x][y] * 2;
  73.  
  74. }
  75. System.out.print(effect[1][x][y]);
  76. }
  77. System.out.println();
  78. }
  79. out.println(ans);
  80. input = in.readLine();
  81. }
  82. }
  83.  
  84. class Magic {
  85. public Point src;
  86. public int dist;
  87. public Point loc;
  88.  
  89. public Magic(Point s, int d, Point l) {
  90. src = s;
  91. dist = d;
  92. loc = l;
  93. }
  94. }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement