数独を解く(6日目)

実行ボタンのClickイベントハンドラを記述する。

        private void zikko_Click(object sender, EventArgs e)
        {
            Stopwatch sw = new Stopwatch();
            sw.Start();
            ////////////////////////////////////////////////////////////////////////////////////////////////////////////
            string[] _kugiri = { "\r\n" };
            string[] all_data = Properties.Resources.resource.Split(_kugiri, StringSplitOptions.RemoveEmptyEntries);
            string[] num_data = Enumerable.Repeat("999999999", 9).ToArray();
            int[] data_num = new int[9];
            ////////////////////////////////////////////////////////////////////////////////////////////////////////////
            for (int i = 0; i < 9; i++)
            {
                for (int u = 0; u < 81; u++)
                {
                    if (kazu[start_position[u / 9] + next_position[u % 9]] == i + 1)
                    {
                        num_data[i] = num_data[i].Remove(u / 9, 1);
                        num_data[i] = num_data[i].Insert(u / 9, (u % 9).ToString());
                    }
                }
            }
            for (int i = 0; i < 9; i++)
            {
                List<string> possible_array = new List<string>();
                foreach (string aa in all_data)
                {
                    bool hantei = true;
                    for (int char_no = 0; char_no < 9; char_no++)
                    {
                        if (num_data[i][char_no] == '9')
                        {
                            if (kazu[start_position[char_no] + next_position[(int)(aa[char_no] - '0')]] != 0)
                            {
                                hantei = false;
                                break;
                            }
                        }
                        else
                        {
                            if (num_data[i][char_no] != aa[char_no])
                            {
                                hantei = false;
                                break;
                            }
                        }
                    }
                    if (hantei)
                    {
                        possible_array.Add(aa);
                    }
                }
                data_num[i] = possible_array.Count();
                each_data[i] = possible_array.ToArray();
            }
            for (int i = 0; i < 9; i++) sort_index[i] = i;
            Array.Sort(data_num, sort_index);
            for (each_data_para[0] = 0; each_data_para[0] < data_num[0]; each_data_para[0]++)
            {
                for (each_data_para[1] = 0; each_data_para[1] < data_num[1]; each_data_para[1]++)
                {
                    if (!hantei(1)) continue;
                    for (each_data_para[2] = 0; each_data_para[2] < data_num[2]; each_data_para[2]++)
                    {
                        if (!hantei(2)) continue;
                        for (each_data_para[3] = 0; each_data_para[3] < data_num[3]; each_data_para[3]++)
                        {
                            if (!hantei(3)) continue;
                            for (each_data_para[4] = 0; each_data_para[4] < data_num[4]; each_data_para[4]++)
                            {
                                if (!hantei(4)) continue;
                                for (each_data_para[5] = 0; each_data_para[5] < data_num[5]; each_data_para[5]++)
                                {
                                    if (!hantei(5)) continue;
                                    for (each_data_para[6] = 0; each_data_para[6] < data_num[6]; each_data_para[6]++)
                                    {
                                        if (!hantei(6)) continue;
                                        for (each_data_para[7] = 0; each_data_para[7] < data_num[7]; each_data_para[7]++)
                                        {
                                            if (!hantei(7)) continue;
                                            for (each_data_para[8] = 0; each_data_para[8] < data_num[8]; each_data_para[8]++)
                                            {
                                                if (hantei(8))
                                                {
                                                    goto finish;
                                                }
                                                    
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        finish:
            for (int i = 0; i < 9; i++)
            {
                for (int n = 0; n < 9; n++)
                {
                    IDbutton[start_position[n] + next_position[(int)(each_data[sort_index[i]][each_data_para[i]][n] - '0')]].Text = (sort_index[i] + 1).ToString();
                }
            }
            sw.Stop(); 
            MessageBox.Show(sw.Elapsed.ToString());
            foreach (Button a in IDbutton)
            {
                a.Enabled = true;
            }
            kensyo.Enabled = true;
            zikko.Enabled = false;
        }
        private bool hantei(int para)
        {
            for (int i = 0; i < para; i++)
            {
                for (int ii = 0; ii < 9; ii++)
                {
                    if (each_data[sort_index[i]][each_data_para[i]][ii] == each_data[sort_index[para]][each_data_para[para]][ii]) return false;
                }
            }
            return true;
        }