fork download
  1. process.stdin.resume();
  2. process.stdin.setEncoding('utf8');
  3.  
  4. // your code goes here
  5. (function() {
  6. 'use strict';
  7.  
  8. var Node = function(map_val, visit_flg, cost_val, map_x, map_y, pre_x, pre_y){
  9. this.map_val = map_val;
  10. this.visit_flg = visit_flg;
  11. this.cost_val = cost_val;
  12. this.map_x = map_x;
  13. this.map_y = map_y;
  14. this.pre_x = pre_x;
  15. this.pre_y = pre_y;
  16. };
  17. var matrix_size_str = '';
  18. var matrix = [];
  19. var input_line_check = 0;
  20. require('readline').createInterface({
  21. input: process.stdin,
  22. output: process.stdout
  23. }).on('line', function(line) {
  24. if (input_line_check === 0){
  25. matrix_size_str = line;
  26. input_line_check = 1;
  27. } else {
  28. matrix.push(line);
  29. }
  30. });
  31. process.stdin.on('end', function() {
  32. const ok_area = '.';
  33. const ng_area = '#';
  34. var matrix_size = matrix_size_str.split(' ').map(Number);
  35. const start = [1, 1];
  36. const goal = [matrix_size[0] + 1, matrix_size[1] + 1];
  37. const up = [1, 0];
  38. const right = [0, 1];
  39. const down = [-1, 0];
  40. const left = [0, -1];
  41. const max_cost = 999;
  42. console.log(matrix_size[0] + ' ' + matrix_size[1]);
  43. let horizontal_line = '';
  44. var map = [];
  45. for(let i = 0; i < matrix_size[1]+2; i++){
  46. horizontal_line = horizontal_line + ng_area;
  47. }
  48. map.push(horizontal_line);
  49. for (let matrix_line of matrix){
  50. map.push(ng_area + matrix_line + ng_area);
  51. }
  52. var nodes = [];
  53. map.push(horizontal_line);
  54. for (let i = 0; i < map.length; i++){
  55. let node_line = [];
  56. let map_lines = map[i].split('');
  57. for (let j = 0; j < map_lines.length; j++){
  58. let node = new Node(map_lines[j], false, max_cost, i,j,0,0 );
  59. node_line.push(node);
  60. }
  61. nodes.push(node_line);
  62. }
  63. console.log(nodes.length);
  64. console.log(nodes[0].length);
  65. console.log(nodes[0][0].map_val);
  66. var now_move = [1, 1];
  67. var unsearched_nodes = [];
  68. unsearched_nodes.push(nodes[start[0]][start[1]]);
  69. unsearched_nodes[0].cost_val = 0;
  70. while(true){
  71. let now_cost = max_cost;
  72. let min_cost_index = 0;
  73. // 最小コストのノードを探す
  74. for (let i = 0; i < unsearched_nodes.length; i++){
  75. if(unsearched_nodes[i].cost_val < now_cost){
  76. now_cost = unsearched_nodes[i].cost_val;
  77. min_cost_index = i;
  78. }
  79. }
  80. // 最小コストのノードを取り出し
  81. let node = unsearched_nodes[min_cost_index];
  82. unsearched_nodes.splice(min_cost_index, 1);
  83.  
  84. node.visit_flg = true;
  85.  
  86. // 終了判定
  87. if (node.map_x === goal[0] && node.map_y === goal[y]){
  88. console.log(node.cost_val);
  89. break;
  90. }
  91.  
  92. // up
  93. let next_node = nodes[node.map_x + up[0]][node.map_y + up[1]];
  94. if(next_node.map_val == ok_area || !next_node.visit_flg){
  95. let cost = 0
  96. if((now_move[0] * up[0] + now_move[1] * up[1]) != 0){
  97. cost = node.cost_val;
  98. } else{
  99. cost = node.cost_val + 1;
  100. }
  101. if(cost < next_node.cost_val){
  102. next_node.cost_val = cost;
  103. }
  104. unsearched_nodes.push(next_node);
  105. }
  106.  
  107. // right
  108. next_node = nodes[node.map_x + right[0]][node.map_y + right[1]];
  109. if(next_node.map_val == ok_area || !next_node.visit_flg){
  110. let cost = 0
  111. if((now_move[0] * right[0] + now_move[1] * right[1]) != 0){
  112. cost = node.cost_val;
  113. } else{
  114. cost = node.cost_val + 1;
  115. }
  116. if(cost < next_node.cost_val){
  117. next_node.cost_val = cost;
  118. }
  119. unsearched_nodes.push(next_node);
  120. }
  121.  
  122. // down
  123. next_node = nodes[node.map_x + down[0]][node.map_y + down[1]];
  124. if(next_node.map_val == ok_area || !next_node.visit_flg){
  125. let cost = 0
  126. if((now_move[0] * down[0] + now_move[1] * down[1]) != 0){
  127. cost = node.cost_val;
  128. } else{
  129. cost = node.cost_val + 1;
  130. }
  131. if(cost < next_node.cost_val){
  132. next_node.cost_val = cost;
  133. }
  134. unsearched_nodes.push(next_node);
  135. }
  136.  
  137. // left
  138. next_node = nodes[node.map_x + left[0]][node.map_y + left[1]];
  139. if(next_node.map_val == ok_area || !next_node.visit_flg){
  140. let cost = 0
  141. if((now_move[0] * left[0] + now_move[1] * left[1]) != 0){
  142. cost = node.cost_val;
  143. } else{
  144. cost = node.cost_val + 1;
  145. }
  146. if(cost < next_node.cost_val){
  147. next_node.cost_val = cost;
  148. }
  149. unsearched_nodes.push(next_node);
  150. }
  151. }
  152. });
  153. })();
Runtime error #stdin #stdout #stderr 0.07s 598528KB
stdin
2 3
...
...
stdout
2 3
4
5
#
stderr
/home/2XjzGw/prog.js:123
      next_node = nodes[node.map_x + down[0]][node.map_y + down[1]];
                                             ^

TypeError: Cannot read property '1' of undefined
    at ReadStream.<anonymous> (/home/2XjzGw/prog.js:123:46)
    at emitNone (events.js:91:20)
    at ReadStream.emit (events.js:185:7)
    at endReadableNT (_stream_readable.js:974:12)
    at _combinedTickCallback (internal/process/next_tick.js:74:11)
    at process._tickCallback (internal/process/next_tick.js:98:9)