Algorithm Game-Sudoku

Sudoku Solver:

  • Write a program to solve a Sudoku puzzle by filling the empty cells.
  • Empty cells are indicated by the character '.'.
  • You may assume that there will be only one unique solution.

           135744_0kaL_2447575.png

  • Answer :

          135744_cWBa_2447575.png

...and its solution numbers marked in red.

C# Codes:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sudoku
{
    class Suduku
    {
        /// <summary>
        /// Keep all of values related every time after running the programme.
        /// </summary>
        private static string str;

        /// <summary>
        /// Keep all of array's data
        ///
        /// </summary>
        private static string arrayStr;

        static void Main(string[] args)
        {
            string[] a = new string[9];
            string[,] arr = new string[,] {
                {"5","3",".",".","7",".",".",".","." },
                {"6",".",".","1","9","5",".",".","." },
                {".","9","8",".",".",".",".","6","." },
                {"8",".",".",".","6",".",".",".","3" },
                {"4",".",".","8",".","3",".",".","1" },
                {"7",".",".",".","2",".",".",".","6" },
                {".","6",".",".",".",".","2","8","." },
                {".",".",".","4","1","9",".",".","5" },
                {".",".",".",".","8",".",".","7","9" }
            };

            // If key is true, "while" cyscle will continue to run. Otherwise, the process will jump out of cycle.
            bool key = true;

            // This array keep results after running once 'while' cycle.
            string[,] result = new string[8, 8];
            result = arr;

            Console.WriteLine("   -------Sudoku Game Question--------");
            result = arr;
            for (int i = 0; i < 9; i++)
            {
                for (int j = 0; j < 9; j++)
                {
                    Console.Write("   " + result[i, j]);
                }
                Console.Write("\n\n");
            }
            Console.Write("   Let's play this game!\n\n");

            // "while" cycle that it can deal with datas and replace '.';
            while (key)
            {
                for (int i = 0; i < 9; i++)
                {
                    for (int j = 0; j < 9; j++)
                    {
                        if (arr[i, j] == ".")
                        {
                            Array.Clear(a, 0, a.Length);

                            // X stand for rows, Y stand for columns.
                            int X = i / 3;
                            int Y = j / 3;
                            for (int i1 = 0; i1 < 3; i1++)
                            {
                                for (int j1 = 0; j1 < 3; j1++)
                                {
                                    a = isExist(a, arr[X * 3 + i1, Y * 3 + j1]);
                                }
                            }
                            // Check row's values existed
                            for (int m = 0; m < 9; m++)
                            {
                                a = isExist(a, arr[i, m]);
                            }
                            // Check column's values existed
                            for (int n = 0; n < 9; n++)
                            {
                                a = isExist(a, arr[n, j]);
                            }

                            for (int k = 0; k < 9; k++)
                            {
                                if (a[k] != "")
                                {
                                    str += a[k];
                                }
                                else
                                {
                                    str += "";
                                }
                            }

                            //Console.WriteLine("Verify this string:{0}", str);
                            if (str.Length != 8)
                            {
                                str = string.Empty;
                                continue;
                            }
                            else
                            {
                                if (!str.Contains("1"))
                                {
                                    result[i, j] = "1";
                                }
                                if (!str.Contains("2"))
                                {
                                    result[i, j] = "2";
                                }
                                if (!str.Contains("3"))
                                {
                                    result[i, j] = "3";
                                }
                                if (!str.Contains("4"))
                                {
                                    result[i, j] = "4";
                                }
                                if (!str.Contains("5"))
                                {
                                    result[i, j] = "5";
                                }
                                if (!str.Contains("6"))
                                {
                                    result[i, j] = "6";
                                }
                                if (!str.Contains("7"))
                                {
                                    result[i, j] = "7";
                                }
                                if (!str.Contains("8"))
                                {
                                    result[i, j] = "8";
                                }
                                if (!str.Contains("9"))
                                {
                                    result[i, j] = "9";
                                }
                            }
                        }
                        else
                        {
                            continue;
                        }
                    }
                }

                // Clean up this string
                arrayStr = string.Empty;
                for (int i = 0; i < 9; i++)
                {
                    for (int j = 0; j < 9; j++)
                    {
                        arrayStr += result[i, j];
                    }
                }

                // According to key value, whether the cycle continue to run.
                if (arrayStr.Contains("."))
                {
                    key = true;
                }
                else
                {
                    key = false;
                }
            }

            // Print Sudoku Game Result
            Console.WriteLine("   -------Sudoku Game Result--------");
            result = arr;
            for (int i = 0; i < 9; i++)
            {
                for (int j = 0; j < 9; j++)
                {
                    Console.Write("   " + result[i, j]);
                }
                Console.Write("\n\n");
            }
            Console.ReadKey();


        }

        /// <summary>
        /// Create a new array to store row's or column's values.
        /// </summary>
        /// <param name="a1">store row's or column's values</param>
        /// <param name="v">array arr's data</param>
        /// <returns></returns>
        private static string[] isExist(string[] a, string v)
        {
            switch (v)
            {
                case "1": a[0] = v; break;
                case "2": a[1] = v; break;
                case "3": a[2] = v; break;
                case "4": a[3] = v; break;
                case "5": a[4] = v; break;
                case "6": a[5] = v; break;
                case "7": a[6] = v; break;
                case "8": a[7] = v; break;
                case "9": a[8] = v; break;
                default: break;
            }
            return a;
        }
    }
}

Running results:

 

转载于:https://my.oschina.net/u/2447575/blog/1525492

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值