Find the answer to the question of whether the horse is stepping on the chessboard or not.

address

Puzzle Game Network - Horse Stepping Board (yzyx.info)

plan

1. Create a new folder Horse

2. Open Horse and create a new script file horse.js with the following content:

"use strict";

let map = [];
let over = false;
let direct = [
	[1, 2],
	[-1, 2],
	[1, -2],
	[-1, -2],
	[2, 1],
	[-2, 1],
	[2, -1],
	[-2, -1]
];

function initMap() {
	map = [];
	for (let i = 0; i < 8; i++) {
		map.push([]);
		for (let j = 0; j < 8; j++) {
			map[i].push(0);
		}
	}
}

function nextCount(i, j) {
	let c = 0;
	for (let k = 0; k < 8; k++) {
		let x = i + direct[k][0];
		let y = j + direct[k][1];
		if (x < 0 || y < 0 || x >= 8 || y >= 8) {
			continue;
		}

		if (map[i][j] == 0) {
			c++;
		}
	}
	return c;
}

function bfs(i, j, num) {

	if (map[i][j] != 0) {
		return;
	}

	map[i][j] = num;

	if (num >= 64) {
		over = true;
		return;
	}

	let nextPos = [];

	for (let k = 0; k < 8; k++) {
		let x = i + direct[k][0];
		let y = j + direct[k][1];
		if (x < 0 || y < 0 || x >= 8 || y >= 8) {
			continue;
		}
		if (map[x][y] != 0) {
			continue;
		}
		let c = nextCount(x, y);

		nextPos.push({
			pos: [x, y],
			c: c
		});
	}

	nextPos.sort((p1, p2) => {
		return p1.c - p2.c
	});

	for(let k=0;k<nextPos.length;k++){
		let x=nextPos[k].pos[0];
		let y=nextPos[k].pos[1];
		
		if (x < 0 || y < 0 || x >= 8 || y >= 8) {
			continue;
		}
		bfs(x,y,num+1);
		if(over){
			break;
		}else{
			map[x][y]=0;
		}
	}
}

document.oncontextmenu=(e)=>{
	e.preventDefault();
	
	over=false;
	initMap();
	bfs(0,0,1);
	
	
	let cells=[];
	for(let i=0;i<64;i++){
		cells.push(document.querySelector("#cell"+i));
	}
	
	for(let i=1;i<=64;i++){
		let flag=false;
		for(let x=0;x<8;x++){
			for(let y=0;y<8;y++){
				if(map[x][y]==i){
					let index=x*8+y;
					cells[index].click();
					flag=true;
					break;
				}
			}
			if(flag){
				break;
			}
		}
	}
	
};

 3. Create a new configuration file manifest.json in the Horse folder with the following content

{
	"name": "horse",
	"version": "1.0",
	"description": "单击鼠标右键求解马踏棋盘",
	"manifest_version": 2,
	"content_scripts": [{
		"matches": [
			"https://yzyx.info/mtqp.html"
		],
		"js": [
			"horse.js"
		]
	}]
}

4. If your operation is correct, the Horse folder should now look like this

5. Drag our Horse folder into the extension of Edge (this is my browser)

At this point, the plug-in installation is complete

test

Open the game website, wait until the game is loaded, and right-click the mouse to automatically solve the problem.

initial

After right-clicking the mouse

 

Guess you like

Origin blog.csdn.net/qq_36694133/article/details/133161957