跳到正文

剪贴板

剪贴板 sh5n7wrl

原文
剪贴板信息

剪贴板信息

收录
更新
HTML
<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8" />
  <title>T2模拟</title>
  <style>
    body {
      margin: 0;
      font-family: Arial, Helvetica, sans-serif;
      display: flex;
      height: 100vh;
    }
    #left {
      flex: 1;
      display: flex;
      align-items: center;
      justify-content: center;
    }
    #right {
      width: 280px;
      border-left: 1px solid #ccc;
      padding: 10px;
      box-sizing: border-box;
      overflow-y: auto;
      white-space: pre;
      font-size: 14px;
    }
    #controls {
      position: fixed;
      top: 10px;
      left: 10px;
      background: #fff;
      padding: 8px 12px;
      border: 1px solid #ccc;
      border-radius: 4px;
    }
    #board {
      display: grid;
      border: 2px solid #333;
    }
    .cell {
      width: 40px;
      height: 40px;
      display: flex;
      align-items: center;
      justify-content: center;
      font-size: 26px;
      cursor: pointer;
      user-select: none;
      box-sizing: border-box;
      border: 1px solid #999;
    }
    .cell.placeable {
      background: #e6ffe6; /* light green */
    }
    .cell.blocked {
      background: #ffe6e6; /* light red */
    }
  </style>
</head>
<body>
  <div id="controls">
    n = <input id="nInput" type="number" min="1" value="8" style="width:60px" />
    <button id="resetBtn">Reset</button>
    <div style="font-size:12px;margin-top:4px;">
      Ctrl+Z: Undo<br />
      Ctrl+R: Reset
    </div>
  </div>
  <div id="left">
    <div id="board"></div>
  </div>
  <div id="right" id="log"></div>

<script>
  const boardDiv = document.getElementById('board');
  const rightDiv = document.getElementById('right');
  const nInput = document.getElementById('nInput');
  const resetBtn = document.getElementById('resetBtn');

  let n = 8;
  let queens = []; // list of {x, y}
  let attackCount = []; // attackCount[x][y]

  function initBoard() {
    n = parseInt(nInput.value);
    queens = [];
    attackCount = Array.from({ length: n }, () => Array(n).fill(0));

    boardDiv.innerHTML = '';
    boardDiv.style.gridTemplateColumns = `repeat(${n}, 40px)`;
    boardDiv.style.gridTemplateRows = `repeat(${n}, 40px)`;

    for (let i = 0; i < n; i++) {
      for (let j = 0; j < n; j++) {
        const cell = document.createElement('div');
        cell.className = 'cell';
        cell.dataset.x = i;
        cell.dataset.y = j;
        cell.addEventListener('click', () => onCellClick(i, j));
        boardDiv.appendChild(cell);
      }
    }
    updateBoard();
    updateLog();
  }

  function inBoard(x, y) {
    return x >= 0 && x < n && y >= 0 && y < n;
  }

  function updateAttacks(x, y, delta) {
    const dirs = [
      [1, 0], [-1, 0], [0, 1], [0, -1],
      [1, 1], [1, -1], [-1, 1], [-1, -1]
    ];
    for (const [dx, dy] of dirs) {
      let nx = x + dx, ny = y + dy;
      while (inBoard(nx, ny)) {
        attackCount[nx][ny] += delta;
        nx += dx;
        ny += dy;
      }
    }
  }

  function onCellClick(x, y) {
    // already has queen
    if (queens.some(q => q.x === x && q.y === y)) return;
    // check parity
    if (attackCount[x][y] % 2 !== 0) return;

    // place queen
    queens.push({ x, y });
    updateAttacks(x, y, 1);
    updateBoard();
    updateLog();
  }

  function undo() {
    if (queens.length === 0) return;
    const q = queens.pop();
    updateAttacks(q.x, q.y, -1);
    updateBoard();
    updateLog();
  }

  function updateBoard() {
    const cells = document.querySelectorAll('.cell');
    cells.forEach(cell => {
      const x = parseInt(cell.dataset.x);
      const y = parseInt(cell.dataset.y);
      cell.textContent = '';
      cell.classList.remove('placeable', 'blocked');

      const hasQueen = queens.some(q => q.x === x && q.y === y);
      if (hasQueen) {
        cell.textContent = '♕';
        cell.classList.add('blocked');
      } else {
        if (attackCount[x][y] % 2 === 0) {
          cell.classList.add('placeable');
        } else {
          cell.classList.add('blocked');
        }
      }
    });
  }

  function updateLog() {
    let text = '';
    text += queens.length + '\n';
    queens.forEach(q => {
      // output as 1-based coordinates
      text += (q.x + 1) + ' ' + (q.y + 1) + '\n';
    });
    rightDiv.textContent = text;
  }

  // keyboard shortcuts
  document.addEventListener('keydown', (e) => {
    if (e.ctrlKey && e.key.toLowerCase() === 'z') {
      e.preventDefault();
      undo();
    }
    if (e.ctrlKey && e.key.toLowerCase() === 'r') {
      e.preventDefault();
      initBoard();
    }
  });

  resetBtn.addEventListener('click', initBoard);

  initBoard();
</script>
</body>
</html>