HIHOCODER-WEEK135 九宫
DFS
其实一共就八种填法……
而且似乎没优化的裸DFS也没问题……干脆就偷懒没写优化= =
======================================================
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace HIHOWEED135 { class Program { static void Main(string[] args) { int[] S = new int[9]; string[] inp = Console.ReadLine().Trim().Split(' '); S[0] = Convert.ToInt32(inp[0]); S[1] = Convert.ToInt32(inp[1]); S[2] = Convert.ToInt32(inp[2]); inp = Console.ReadLine().Trim().Split(' '); S[3] = Convert.ToInt32(inp[0]); S[4] = Convert.ToInt32(inp[1]); S[5] = Convert.ToInt32(inp[2]); inp = Console.ReadLine().Trim().Split(' '); S[6] = Convert.ToInt32(inp[0]); S[7] = Convert.ToInt32(inp[1]); S[8] = Convert.ToInt32(inp[2]); bool[] C = new bool[9] { true, true, true, true, true, true, true, true, true }; foreach (var i in S) { if (i == 0) continue; C[i - 1] = false; } DFS(S, C, 0); if (ans == 1) { for (int i = 0; i < 9; i++) { Console.Write(AnsS[i]); if (((i + 1) % 3 == 0)) Console.WriteLine(); else Console.Write(' '); } } else Console.WriteLine("Too Many"); } static int ans = 0; static int[] AnsS = new int[9]; static void DFS(int[] S, bool[] Ava, int Level) { if (Level > 8) { if (Check(S)) { ans++; S.CopyTo(AnsS, 0); } return; } if (S[Level] != 0) { DFS(S, Ava, Level + 1); return; } for (int i = 0; i < 9; i++) { if (Ava[i] == false) continue; Ava[i] = false; S[Level] = i + 1; DFS(S, Ava, Level + 1); Ava[i] = true; S[Level] = 0; } return; } static bool Check(int[] S) { if ((S[0] + S[1] + S[2]) == 15) if ((S[3] + S[4] + S[5]) == 15) if ((S[6] + S[7] + S[8]) == 15) if ((S[0] + S[3] + S[6]) == 15) if ((S[1] + S[4] + S[7]) == 15) if ((S[2] + S[5] + S[8]) == 15) if ((S[0] + S[4] + S[8]) == 15) if ((S[2] + S[4] + S[6]) == 15) return true; return false; } } }