TopCoder SRM 146 DIV 1 Medium 練習
解
全部の桁は1〜7で、桁は4桁。
74 = 2401なので総当りする。
using System; using System.Text; using System.Text.RegularExpressions; using System.Collections; using System.Collections.Generic; public class Masterbrain { string[] G, R; public int possibleSecrets(string[] guesses, string[] results) { G = guesses; R = results; int res = 0; for (int i = 1; i <= 7; ++i) for (int j = 1; j <= 7; ++j) for (int k = 1; k <= 7; ++k) for (int l = 1; l <= 7; ++l) if (check((i * 1000 + j * 100 + k * 10 + l).ToString())) res++; return res; } private bool check(string p) { bool lie = false; for (int i = 0; i < G.Length; ++i) { if (check2(G[i], p) != R[i]) if (lie) return false; else lie = true; } return lie; } private string check2(string a, string b) { int B = 0, W = 0; bool[] va = new bool[4]; bool[] vb = new bool[4]; for (int i = 0; i < 4; ++i) if (a[i] == b[i]) { va[i] = vb[i] = true; B++; } for(int i = 0; i < 4; ++i) for(int j = 0; j < 4; ++j) if (!vb[i] && !va[j] && a[j] == b[i]) { va[j] = vb[i] = true; W++; } return string.Format("{0}b {1}w", B, W); } }