Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

186
Views
¿Cómo encontrar la siguiente posición en una matriz 2D según la posición actual?

Supongamos que tengo una matriz con 3 filas y 4 columnas const arr = [[1,2,3,4],[5,6,7,8],[9,10,11,12]] y doy una entrada como ["straight", "right", "left"] y la posición inicial es arr[0][0] y la dirección inicial es "east" .

 [ [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12] ]

Desde la posición inicial, ir "straight" debería dar 2 . Y luego, desde aquí, ir a "right" debería dar 6 y, finalmente, "left" desde aquí debería dar 7 .

¿Cómo puedo lograr esto en JavaScript?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

  • Cree un mapa que proporcione la siguiente dirección en función de la dirección y el movimiento actuales.
  • Ahora, para cada movimiento, calcule la siguiente dirección y verifique si es un movimiento válido, si lo es, devuelva el siguiente valor, posición y dirección y repita esto para cada movimiento.
  • Si el movimiento no es válido en algún momento, esta solución arroja un error, puede personalizar el manejo de errores según sus necesidades.

 const nextDirMap = { north: { left: "west", right: "east", straight: "north" }, south: { left: "east", right: "west", straight: "south" }, east: { left: "north", right: "south", straight: "east" }, west: { left: "south", right: "north", straight: "west" }, }; function getNextPos(grid, currPos, currDir, move) { const nextDir = nextDirMap[currDir][move]; const [r, c] = currPos; const maxRowLength = grid.length; const maxColLength = grid[0].length; switch (nextDir) { case "north": { if (r <= 0) { throw "Unable to move"; } return { val: grid[r - 1][c], pos: [r - 1, c], dir: "north" }; } case "south": { if (r >= maxRowLength) { throw "Unable to move"; } return { val: grid[r + 1][c], pos: [r + 1, c], dir: "south" }; } case "east": { if (c >= maxColLength) { throw "Unable to move"; } return { val: grid[r][c + 1], pos: [r, c + 1], dir: "east" }; } case "west": { if (c <= 0) { throw "Unable to move"; } return { val: grid[r][c - 1], pos: [r, c - 1], dir: "west" }; } } } function solution(grid, initPos, initDir, moves) { let currPos = initPos; let currDir = initDir; let currVal; moves.forEach((move) => { let { val, pos, dir } = getNextPos(grid, currPos, currDir, move); currDir = dir; currPos = pos; currVal = val; }); return currVal; } const res = solution( [ [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], ], [0, 0], "east", ["straight", "right", "left"] ); console.log(res); // 7

Tenga en cuenta que la solución asume que tiene una cuadrícula válida (mismo número de columnas para todas las filas) y tiene al menos una fila.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!