using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;namespace fromnasa3
{
public partial class Form1 : Form
{
Graphics gdi;
Pen p = new Pen(Color.Black, 1);
SolidBrush b = new SolidBrush(Color.White); static Random rad = new Random(); public Form1()
{
InitializeComponent();
} private void panel1_Paint(object sender, PaintEventArgs e)
{ gdi = panel1.CreateGraphics();
gdi.FillRectangle(b, 0, 0, 320, 320);
Point pstart=new Point(), pend=new Point();
for (int i = 1; i <= 7; i++)
{
pstart.X = 0; pstart.Y = i*40;
pend.X = 320; pend.Y = pstart.Y;
gdi.DrawLine(p, pstart, pend);
pstart.X = i * 40; pstart.Y = 0;
pend.X = pstart.X; pend.Y = 320;
gdi.DrawLine(p, pstart, pend);
}
}
private void button初始化_Click(object sender, EventArgs e)
{
//gdi = panel1.CreateGraphics();
b.Color = Color.White;
gdi.FillRectangle(b, 0, 0, 320, 320);
Point pstart = new Point(), pend = new Point();
for (int i = 1; i <= 7; i++)
{
pstart.X = 0; pstart.Y = i * 40;
pend.X = 320; pend.Y = pstart.Y;
gdi.DrawLine(p, pstart, pend);
pstart.X = i * 40; pstart.Y = 0;
pend.X = pstart.X; pend.Y = 320;
gdi.DrawLine(p, pstart, pend);
}
pstart.X = rad.Next(1, 8); pstart.Y = rad.Next(1, 8);
drawPoint(pstart); bool[,] chess = new bool[8, 8];
Point[] gogo = new Point[64];
move(pstart.X,pstart.Y,1,chess,gogo);
}
private void drawPoint(Point p)
{
Rectangle rect = new Rectangle((p.X-1)*40,(p.Y-1)*40,40,40);
b.Color = Color.Red;
gdi.FillRectangle(b,rect);
}
private void move(int x, int y, int count, bool[,] map, Point[] text)
{
map[x - 1, y - 1] = true;
text[count - 1].X = x; text[count - 1].Y = y;
//Console.WriteLine(text +"---------");
if (count==64&&lookmap(map))
{ //MessageBox.Show("找到了解决方案");
foreach (Point v in text)
{
drawPoint(v);
Thread.Sleep(20); }
} int xnext, ynext; count++;
//出现4个可能
xnext = x; ynext = y - 1;
if (xnext >= 1 && xnext <= 8 && ynext >= 1 && ynext <= 8 && map[xnext - 1, ynext - 1] == false)//过界判断
move(xnext, ynext, count, map, text);
xnext = x - 1; ynext = y;
if (xnext >= 1 && xnext <= 8 && ynext >= 1 && ynext <= 8 && map[xnext - 1, ynext - 1] == false)//过界判断
move(xnext, ynext, count, map, text);
xnext = x + 1; ynext = y;
if (xnext >= 1 && xnext <= 8 && ynext >= 1 && ynext <= 8 && map[xnext - 1, ynext - 1] == false)//过界判断
move(xnext, ynext, count, map, text);
xnext = x; ynext = y + 1;
if (xnext >= 1 && xnext <= 8 && ynext >= 1 && ynext <= 8 && map[xnext - 1, ynext - 1] == false)//过界判断
move(xnext, ynext, count, map, text);
}
private bool lookmap(bool[,] xmap)
{
foreach (bool v in xmap)
{
if (v == false)
return false;
}
return true; } }
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货