运行平台
Algorithm Visualizer
神奇的幻方
[NOIP2015 提高组] 神奇的幻方
// import visualization libraries {
const { Tracer, Array2DTracer, Array1DTracer, LogTracer, Randomize, Layout, VerticalLayout } = require('algorithm-visualizer');
// }
function filledArray(length, value) {return Array(...Array(length)).map(Number.prototype.valueOf, value);
}const N = 5; //配置幻方大小
const board = (function createArray(N) {const result = [];for (let i = 0; i < N; i++) {result[i] = filledArray(N, 0);}return result;
}(N));// define tracer variables {
const boardTracer = new Array2DTracer('幻方');
const logger = new LogTracer('算法过程');
Layout.setRoot(new VerticalLayout([boardTracer, logger]));
boardTracer.set(board);
logger.println(`${N}阶幻方`);
Tracer.delay();
// }// logger {
logger.println('开始执行');
// }
let x = 0, y = (N - 1) / 2for(let i = 1; i <= N * N; i ++)
{board[x][y] = i;// logger {logger.println(`在${x}行${y}列填入${i}`);// }// visualizer {boardTracer.patch(x, y, i); Tracer.delay();// }if(x === 0 && y != N - 1) x = N - 1, y += 1;else if(y == N - 1 && x !== 0) x -= 1, y = 0;else if(x === 0 && y == N - 1) x += 1;else if(board[x - 1][y + 1] !== 0) x += 1;else x -= 1, y += 1;
}
// logger {
logger.println(`完成`);
// }
杨辉三角
杨辉三角
// import visualization libraries {
const { Tracer, Array2DTracer, Layout, VerticalLayout } = require('algorithm-visualizer');
// }const N = 9;
const A = new Array(N);
for (let i = N - 1; i >= 0; i--) {A[i] = new Array(i + 1);
}// define tracer variables {
const tracer = new Array2DTracer('杨辉三角');
Layout.setRoot(new VerticalLayout([tracer]));
tracer.set(A);
Tracer.delay();
// }for (let i = 0; i < N; i++) {for (let j = 0; j <= i; j++) {if (j === i || j === 0) { // 第一列和最后一列的值为1A[i][j] = 1;// visualize {tracer.patch(i, j, A[i][j]);Tracer.delay();tracer.depatch(i, j);// }} else { // 其它列的值为上一行对应列的和// visualize {tracer.select(i - 1, j - 1);Tracer.delay();tracer.select(i - 1, j);Tracer.delay();// }A[i][j] = A[i - 1][j - 1] + A[i - 1][j];// visualize {tracer.patch(i, j, A[i][j]);Tracer.delay();tracer.depatch(i, j);tracer.deselect(i - 1, j - 1);tracer.deselect(i - 1, j);// }}}
}
赦免战俘
赦免战俘
// import visualization libraries {
const { Tracer, Array2DTracer, Array1DTracer, LogTracer, Randomize, Layout, VerticalLayout } = require('algorithm-visualizer');
// }
function filledArray(length, value) {return Array(...Array(length)).map(Number.prototype.valueOf, value);
}const N = 1 << 3; //配置棋盘大小和初始状态
const board = (function createArray(N) {const result = [];for (let i = 0; i < N; i++) {result[i] = filledArray(N, 0);}return result;
}(N));// define tracer variables {
const boardTracer = new Array2DTracer('正方形方阵');
const logger = new LogTracer('算法过程');
Layout.setRoot(new VerticalLayout([boardTracer, logger]));boardTracer.set(board);
logger.println(`赦免战俘: ${N} X ${N}的方阵`);
Tracer.delay();
// }let ans = 0
function work(n, x, y) {// logger {logger.println(`赦免${x}行${y}列开始、大小为${n} X ${n}的方阵`); // }if (n == 2) {board[x + 1][y] = board[x][y + 1] = board[x + 1][y + 1] = 1;// 可视化处理 {boardTracer.select(x + 1, y);Tracer.delay();boardTracer.patch(x + 1, y, 1);Tracer.delay();boardTracer.select(x, y + 1);Tracer.delay();boardTracer.patch(x, y + 1, 1);Tracer.delay();boardTracer.select(x + 1, y + 1);Tracer.delay();boardTracer.patch(x + 1, y + 1, 1);Tracer.delay();// }return;}n = n / 2;work(n, x + n, y);work(n, x, y + n);work(n, x + n, y + n);
}// logger {
logger.println('开始执行');
// }
work(N, 0, 0);
// logger {
logger.println(`完成`);
// }