简单dfs寻路
while (line = readline()) {
let nums = line.split(' ');
let col = +nums[0];
let row = +nums[1];
let map = [];
for (let i = 0; i < col; i++) {
map.push([]);
let mapline = readline().split(' ');
for (let j = 0; j < row; j++) {
map[i][j] = +mapline[j];
}
}
let minLine = [];
let nowLine = [];
let dir = [[0, 1], [0, -1], [1, 0], [-1, 0]];
let book = [];
for (let i = 0; i < col; i++) {
book.push([]);
for (let j = 0; j < row; j++) {
book[i][j] = 0;
}
}
let dfs = (x, y) => {
nowLine.push(`(${x},${y})`);
book[x][y] = 1;
if (x == col - 1 && y == row - 1) {
if (minLine.length == 0 || nowLine.length < minLine.length) {
minLine = [];
for (let i = 0; i < nowLine.length; i++) {
minLine[i] = nowLine[i];
}
}
book[x][y] = 0;
nowLine.pop();
return;
}
for (let i = 0; i < 4; i++) {
let tox = x + dir[i][0];
let toy = y + dir[i][1];
if (tox >= 0 && tox < col && toy >= 0 && toy < row && map[tox][toy] == 0 && book[tox][toy] == 0) {
dfs(tox, toy);
}
}
book[x][y] = 0;
nowLine.pop();
return;
}
dfs(0, 0);
for (let i = 0; i < minLine.length; i++) {
console.log(minLine[i]);
}
}