Posted on 2006-12-01 13:43
随心所欲 阅读(351)
评论(2) 编辑 收藏 网摘 所属分类:
其他技术
这是一个解决logic9游戏的程序,递归,回溯。给初学者一个例子。
Logic9游戏规则:有9行9列做成的一个网格,初始网格上有一些数字,要求补全剩下的数字。规则:每一直行、每一竖列上数字不能重复;数字只能在1-9中选择。
比方说:
|
|
2 |
3 |
7 |
|
|
|
6 |
8 |
|
|
6 |
1 |
|
5 |
3 |
|
9 |
7 |
|
|
7 |
.. |
|
|
|
|
|
|
|
|
|
.. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
主要的处理:
1:stack 类
堆栈。存储进站的信息。这是回溯的根据。
2:CheckCell递归函数。检查每一个点,直到最后
3:其他:初始化网格;判断是否该数字是否合法
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4
using System.Collections;
5
6
namespace Logic9
7

{
8
/**//// <summary>
9
/// 用于存储每个点
10
/// </summary>
11
public class StackCell
12
{
13
public StackCell(int indexX,int indexY, int value)
14
{
15
this.indexX = indexX;
16
this.indexY = indexY;
17
this.value = value;
18
}
19
public int IndexX
20
{
21
get
{ return indexX; }
22
set
{ indexX = value; }
23
}
24
private int indexX;
25
public int IndexY
26
{
27
get
{ return indexY; }
28
set
{ indexY = value; }
29
}
30
private int indexY;
31
public int Value
32
{
33
get
{ return this.value; }
34
set
{ this.value = value; }
35
}
36
private int value;
37
38
}
39
/**//// <summary>
40
/// 一个简单的堆栈
41
/// </summary>
42
public class Stack
43
{
44
public void Push(StackCell cell)
45
{
46
this.stack.Add(cell);
47
}
48
public StackCell Pop()
49
{
50
if (this.stack.Count > 0)
51
{
52
StackCell cell= this.stack[this.stack.Count - 1] as StackCell;
53
this.stack.RemoveAt(this.stack.Count - 1);
54
return cell;
55
}
56
else
57
return null;
58
}
59
public int Length
60
{
61
get
{ return this.stack.Count; }
62
}
63
private ArrayList stack = new ArrayList();
64
/**//// <summary>
65
/// 可以搜索是否有这个点。
66
/// </summary>
67
/// <param name="indexX"></param>
68
/// <param name="indexY"></param>
69
/// <returns></returns>
70
public int CheckCell(int indexX, int indexY)
71
{
72
for (int i = 0; i < stack.Count; i++)
73
{
74
StackCell cell = stack[i] as StackCell;
75
if (cell.IndexX == indexX && cell.IndexY == indexY)
76
return cell.Value;
77
}
78
return 0;//没找到
79
}
80
}
81
}
82
1
using System;
2
using System.Collections.Generic;
3
using System.ComponentModel;
4
using System.Data;
5
using System.Drawing;
6
using System.Text;
7
using System.Windows.Forms;
8
9
namespace Logic9
10

{
11
public partial class frmMain : Form
12
{
13
14
private StackCell[,] cells = new StackCell[9, 9];//用以存储初始值
15
private Stack stackHolder = new Stack();//用其存储中间结果,回溯,堆栈
16
17
public frmMain()
18
{
19
InitializeComponent();
20
}
21
22
/**//// <summary>
23
/// 把所有的点清零
24
/// </summary>
25
public void InitNumbers()
26
{
27
for (int i = 0; i < 9; i++)
28
{
29
for (int j = 0; j < 9; j++)
30
{
31
cells[i, j] = new StackCell(i, j, 0);
32
}
33
}
34
}
35
/**//// <summary>
36
/// 获取输入的初始值
37
/// </summary>
38
public bool InitInput()
39
{
40
prepare#region prepare
41
cells[0, 0].Value = 1;
42
cells[0, 1].Value = 5;
43
cells[0, 5].Value = 8;
44
cells[0, 6].Value = 3;
45
cells[0, 7].Value = 4;
46
47
cells[1, 0].Value = 4;
48
cells[1, 5].Value = 1;
49
cells[1, 6].Value = 7;
50
51
cells[2, 1].Value = 6;
52
cells[2, 4].Value = 3;
53
cells[2, 5].Value = 2;
54
cells[2, 7].Value = 8;
55
56
cells[3, 2].Value = 5;
57
cells[3, 3].Value = 2;
58
cells[3, 4].Value = 7;
59
cells[3, 8].Value = 4;
60
61
cells[4, 2].Value = 6;
62
cells[4, 3].Value = 1;
63
cells[4, 8].Value = 7