Help us understand the problem. What is going on with this article?

AtCoder供養

JSのES6でAtCoderをやろうとしたら、「letもconstも使えません 分割代入でなんかエラー出てるぞ」と言われて萎えたのでコードを供養します

はじめてのBFS...

迷路をツリー構造に格納しーの、キューを使って探索してます
やり方がまったくわかりませんでした

const maze = '######## #.#....# #.###..# #......# ########'.split(' ')
const [r, c] = [maze.length, maze[0].length]
const [sr, sc] = [1,1]
const [gr, gc] = [1,3]

// SAMPLE DATA STRUCTURE
// const root = {
//   r: 1,
//   c: 1,
//   children: [
//     {
//       r: 2,
//       c: 1,
//       children: [
//         ...
//       ]
//     }
//   ]
// }

const printNode = n => {
  console.log('['+n.r+','+n.c+']' + 'depth: ' + n.d)
}



var visited = [[1,1]]; // root node is already visited

const addChild = p => {
  const range = [[-1, 0], [1, 0], [0, -1], [0, 1]]

  for (d of range) {
    const [dr, dc] = d

    let child = {}
    child.r = p.r + dr
    child.c = p.c + dc
    child.d = p.d + 1
    child.children = []
    if (child.r >= 0 && child.r < r && child.c >= 0 && child.c < c 
        && maze[child.r][child.c] == '.'
        && !visited.some(e => e[0] == child.r && e[1] == child.c)) {
      visited.push([child.r, child.c])
      p.children.push(child)
      addChild(child)
    }

  }
}


const start = {
  r: 1,
  c: 1,
  d: 0, 
  children: []
}
addChild(start)

var res;
const q = [start]
while (q.length) {
  const row = q.shift()
  if (row.children) {
    row.children.forEach(child => {
      if (child.r == gr && child.c == gc) {
        res = child.d
        return
      }
      q.push(child)
    })
  }
}


console.log(res)

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away