随心所欲

做个幸福的人
posts - 147, comments - 1402, trackbacks - 28, articles - 0
  博客园 :: 首页 :: 新随笔 ::  :: 订阅 订阅 :: 管理

这是一个解决logic9游戏的程序,递归,回溯。给初学者一个例子。

 

Logic9游戏规则:有99列做成的一个网格,初始网格上有一些数字,要求补全剩下的数字。规则:每一直行、每一竖列上数字不能重复;数字只能在1-9中选择。

比方说:

 

2

3

7

 

 

 

6

8

 

6

1

 

5

3

 

9

7

 

7

..

 

 

 

 

 

 

 

 

..

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

主要的处理:

1stack

   堆栈。存储进站的信息。这是回溯的根据。

2CheckCell递归函数。检查每一个点,直到最后

3:其他:初始化网格;判断是否该数字是否合法

 1using System;
 2using System.Collections.Generic;
 3using System.Text;
 4using System.Collections;
 5
 6namespace 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 - 1as 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

  1using System;
  2using System.Collections.Generic;
  3using System.ComponentModel;
  4using System.Data;
  5using System.Drawing;
  6using System.Text;
  7using System.Windows.Forms;
  8
  9namespace Logic9
 10{
 11    public partial class frmMain : Form
 12    {
 13
 14        private StackCell[,] cells = new StackCell[99];//用以存储初始值
 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