我正在嘗試解決代碼出現時的以下代碼挑戰:問題描述
本質上講,我在表示坐標上的網格線,即線的形式接收輸入:x1,y1 -> x2,y2。對于問題的第一部分,我要將這些線繪制成一個二維陣列,然后計算網格中線至少重疊一次的坐標數。到目前為止,我已經成功地隔離了哪些行是有效的(水平或垂直),并試圖簡單地將它們繪制到二維陣列上。由于一些我無法確定的奇怪原因,我的方法僅將一些線繪制到陣列上,即使這些線都被識別為有效的,應該并且被傳遞到應該繪制的同一個代碼塊中行到陣列中。請參閱以下代碼:
import { stdout } from 'process';
function getPuzzleInput (fileName : string) : [any[], number, number] {
const fs = require('fs')
let coordLines = [];
let maxX = 0;
let maxY = 0;
try {
const data : string = fs.readFileSync(fileName, 'utf8')
const lines = data.split("\n");
let insertIndex = 0;
for (let lineNum=0; lineNum < lines.length; lineNum ) {
let coords : string[] = lines[lineNum].split(" -> ");
let x1 : number = parseInt(coords[0].split(",")[0].trim());
let y1 : number = parseInt(coords[0].split(",")[1].trim());
let x2 : number = parseInt(coords[1].split(",")[0].trim());
let y2 : number = parseInt(coords[1].split(",")[1].trim());
if (x1 == x2 || y1 == y2) {
coordLines.push({x1: x1, y1: y1, x2: x2, y2: y2});
maxX = Math.max(maxX, x1, x2);
maxY = Math.max(maxY, y1, y2);
}
}
} catch (err) {
console.error(err)
}
return [coordLines, maxX, maxY];
}
function getSolutionOne(coordLines, maxX : number, maxY : number) : void {
let grid = [];
for (let y = 0; y <= maxY; y ) {
grid[y] = [];
for (let x = 0; x <= maxX; x ) {
grid[y][x] = 0;
}
}
for (let line of coordLines) {
if (line.x1 == line.x2) {
console.log("Write into grid: ", line);
// Write line into grid
for (let y = line.y1; y <= line.y2; y ) {
grid[y][line.x1] ;
}
} else if (line.y1 == line.y2) {
console.log("Write into grid: ", line);
// Write line into grid
for (let x = line.x1; x <= line.x2; x ) {
grid[line.y1][x] ;
}
}
}
printGridMap(grid, maxX, maxY);
}
function printGridMap(grid: any[], maxX : number, maxY : number) {
stdout.write("x 0 1 2 3 4 5 6 7 8 9\n");
stdout.write("----------------------\n");
for (let y = 0; y <= maxY; y ) {
stdout.write(y.toString() "| ");
for (let x = 0; x <= maxX; x ) {
stdout.write(grid[y][x].toString() " ");
}
stdout.write("\n");
}
}
const main = () => {
const [coordLines, maxX, maxY] = getPuzzleInput('./day5EX.txt');
getSolutionOne(coordLines, maxX, maxY);
}
main();
此代碼導致以下輸出:
Write into grid: { x1: 0, y1: 9, x2: 5, y2: 9 }
Write into grid: { x1: 9, y1: 4, x2: 3, y2: 4 }
Write into grid: { x1: 2, y1: 2, x2: 2, y2: 1 }
Write into grid: { x1: 7, y1: 0, x2: 7, y2: 4 }
Write into grid: { x1: 0, y1: 9, x2: 2, y2: 9 }
Write into grid: { x1: 3, y1: 4, x2: 1, y2: 4 }
x 0 1 2 3 4 5 6 7 8 9
----------------------
0| 0 0 0 0 0 0 0 1 0 0
1| 0 0 0 0 0 0 0 1 0 0
2| 0 0 0 0 0 0 0 1 0 0
3| 0 0 0 0 0 0 0 1 0 0
4| 0 0 0 0 0 0 0 1 0 0
5| 0 0 0 0 0 0 0 0 0 0
6| 0 0 0 0 0 0 0 0 0 0
7| 0 0 0 0 0 0 0 0 0 0
8| 0 0 0 0 0 0 0 0 0 0
9| 2 2 2 1 1 1 0 0 0 0
而我們可以看到真正的解決方案,基于向我們顯示寫入網格的行應該是什么的輸出,看起來像這樣:
x 0 1 2 3 4 5 6 7 8 9
----------------------
0| 0 0 0 0 0 0 0 1 0 0
1| 0 0 1 0 0 0 0 1 0 0
2| 0 0 1 0 0 0 0 1 0 0
3| 0 0 0 0 0 0 0 1 0 0
4| 0 1 1 2 1 1 1 2 1 1
5| 0 0 0 0 0 0 0 0 0 0
6| 0 0 0 0 0 0 0 0 0 0
7| 0 0 0 0 0 0 0 0 0 0
8| 0 0 0 0 0 0 0 0 0 0
9| 2 2 2 1 1 1 0 0 0 0
很明顯,即使我的代碼正在確定哪些行應該添加到網格中,但出于某種原因,我無法確定某些行沒有被添加。關于這里出了什么問題的任何想法?
uj5u.com熱心網友回復:
有時 x2 或 y2 小于 x1 或 y1,并且您的代碼沒有考慮到這一點 - 您的回圈當前正在執行for (let y = line.y1; y <= line.y2; y ) {,如果 2 開始時小于 1,則根本不會迭代。
首先確定較小或較大的元素,或者確定需要迭代的方向 - 使用 1 或 -1。例如
const increment = x2 > x1 ? 1 : -1;
for (let x = line.x1; x !== line.x2; x = increment) {
grid[line.y1][x] ;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/380283.html
上一篇:一個關于連續順序的演算法題
下一篇:Bloxorza-Star搜索
