day5-0730
今天偶然看到一个编程题:主旨是迷宫渲染
之前刷题都是C++
,Python
,Java
索性今天就用 JS
浪一把
/**
* @author: henryhe
* @description: 迷宫渲染
* @param { row1 } - RoadGrid
* @param { connection } - 连通数组
* @license: under MIT
*/
const readLine = require('readline')
const fs = require('fs')
const os = require('os')
const commandReadline = readLine.createInterface({
input: process.stdin,
output: process.stdout
})
var commandContent = {
count: 0,
roadGrid: [],
renderGrid: []
}
var maze = []
var row, column = null
commandReadline.on('line', (content) => {
if (content === 'X') {
renderMaze()
process.exit(0)
}
content.split(' ').forEach((element, index) => {
if (element === 'X') {
renderMaze()
process.exit(0)
}
if (commandContent.count === 0) {
commandContent.roadGrid.push(element)
}
if (commandContent.count === 1) {
commandContent.renderGrid.push(element)
}
})
commandContent.count++
if (commandContent.count === 2) {
renderMaze()
process.exit(0)
}
})
commandReadline.on('close', () => process.exit(0))
// 处理输入
function handleCommandInput() {
console.log('输入 X 结束输入')
}
/**
*
* @param {string} head 前一个cell
* @param {string} tail 后一个cell
*/
function connectPosByCell(head, tail) {
var h = getRealPosMaze(head)
var t = getRealPosMaze(tail)
if (h[0] == t[0]) {
for (let index = h[1]; index <= t[1]; index++) {
maze[h[0]][index] = '[R]'
}
for (let index = t[1]; index <= h[1]; index++) {
maze[h[0]][index] = '[R]'
}
} else {
for (let index = h[0]; index <= t[0]; index++) {
maze[index][t[1]] = '[R]'
}
for (let index = t[0]; index <= h[0]; index++) {
maze[index][t[1]] = '[R]'
}
}
}
function getRealPosMaze(cell) {
var x = parseInt(cell.split(',')[0])
var y = parseInt(cell.split(',')[1])
var times = x * row + y
var ct = 0
for (let index = 1; index < maze.length; index += 2) {
e_row = maze[index];
for (let j = 1; j < e_row.length; j += 2) {
if (ct === times) {
return [index, j]
}
ct++
}
}
}
// 渲染
function renderMaze() {
console.log(commandContent)
row = parseInt(commandContent.roadGrid[0])
column = parseInt(commandContent.roadGrid[1])
for (let index = 0; index < column * 3 - (column - 1); index++) {
maze.push(new Array(row * 3 - (row - 1)).fill('[W]'))
}
initMaze()
console.log(printMaze())
var index = 0
while (true) {
if (commandContent.renderGrid[index] === '') {
console.log('输出:')
console.log(printMaze())
return
}
var head = commandContent.renderGrid[index]
var tail = commandContent.renderGrid[index + 1]
if (tail === undefined || tail === '') {
console.log('输出:')
console.log(printMaze())
return
}
if (head.split(';').length > 1) {
head = head.split(';')[1]
} else {
head = head.split(';')[0]
}
connectPosByCell(head, tail.split(';')[0])
// console.log(printMaze())
index++
}
}
function initMaze() {
for (let index = 1; index < maze.length; index += 2) {
e_row = maze[index];
for (let j = 1; j < e_row.length; j += 2) {
maze[index][j] = '[R]'
}
}
}
// 输入迷宫
function printMaze() {
var result = ''
for (let index = 0; index < maze.length; index++) {
const element = maze[index];
for (let j = 0; j < element.length; j++) {
const e = element[j];
result += e + ' '
}
result += '\n'
}
return result
}
/**
* main
*/
handleCommandInput()
输入 X 结束输入
{ count: 2,
roadGrid: [ '3', '3' ],
renderGrid: [ '0,1', '0,2;1,1', '1,2' ] }
[W] [W] [W] [W] [W] [W] [W]
[W] [R] [W] [R] [W] [R] [W]
[W] [W] [W] [W] [W] [W] [W]
[W] [R] [W] [R] [W] [R] [W]
[W] [W] [W] [W] [W] [W] [W]
[W] [R] [W] [R] [W] [R] [W]
[W] [W] [W] [W] [W] [W] [W]
输出:
[W] [W] [W] [W] [W] [W] [W]
[W] [R] [W] [R] [R] [R] [W]
[W] [W] [W] [W] [W] [W] [W]
[W] [R] [W] [R] [R] [R] [W]
[W] [W] [W] [W] [W] [W] [W]
[W] [R] [W] [R] [W] [R] [W]
[W] [W] [W] [W] [W] [W] [W]
比较酷的的一件事是:使用js来获取标准输入,之前都没有仔细思考过
好久没有做类似的编程题,思维已经有点迟钝了,大局观几乎丧失。
还需要多多练习呀!