|
如何随机生成哈密顿路?
import graph;
size(200);
// Define the SVG path as a string
string svgPath = "50,50 L 50,150 L 150,150 L 150,100 L 100,100 L 100,50 L 200,50 L 200,150 L 250,150 L 250,250 L 200,250 L 200,200 L 50,200 L 50,350 L 100,350 L 100,250 L 150,250 L 150,300 L 350,300 L 350,250 L 300,250 L 300,100 L 250,100 L 250,50 L 450,50 L 450,100 L 350,100 L 350,200 L 400,200 L 400,150 L 500,150 L 500,50 L 550,50 L 550,300 L 500,300 L 500,200 L 450,200 L 450,250 L 400,250 L 400,300 L 450,300 L 450,400 L 400,400 L 400,350 L 150,350 L 150,400 L 50,400 L 50,550 L 150,550 L 150,600 L 50,600 L 50,750 L 200,750 L 200,700 L 100,700 L 100,650 L 250,650 L 250,750 L 300,750 L 300,600 L 200,600 L 200,500 L 100,500 L 100,450 L 200,450 L 200,400 L 350,400 L 350,450 L 500,450 L 500,350 L 600,350 L 600,200 L 700,200 L 700,100 L 650,100 L 650,150 L 600,150 L 600,50 L 750,50 L 750,250 L 650,250 L 650,350 L 700,350 L 700,300 L 750,300 L 750,450 L 700,450 L 700,400 L 550,400 L 550,550 L 500,550 L 500,500 L 300,500 L 300,450 L 250,450 L 250,550 L 450,550 L 450,600 L 350,600 L 350,750 L 400,750 L 400,650 L 450,650 L 450,750 L 500,750 L 500,600 L 550,600 L 550,750 L 700,750 L 700,700 L 600,700 L 600,450 L 650,450 L 650,500 L 750,500 L 750,550 L 650,550 L 650,650 L 700,650 L 700,600 L 750,600 L 750,750";
// Split the SVG path into individual commands
string[] commands = split(svgPath, " L ");
// Create a list of points to store the coordinates
pair[] points;
// Process each command in the SVG path
for (string command : commands) {
string[] coordinates = split(command, ",");
real x = (int)coordinates[0];
real y = (int)coordinates[1];
points.push((x/10,y/10));
}
draw(graph(points), linewidth(2));
for(int i=1; i < 16; ++i) {
for(int j=1; j < 16; ++j) {
dot((5i,5j),red);
}
}
stackoverflow.com/questions/7371227/
sfu.ca/~eemberly/phys847/assignments/hamiltonian_paths.html
clisby.net/projects/hamiltonian_path/ |
|