【Codeforces 63C】Bulls and Cows
阿新 • • 發佈:2019-01-27
continue int() ble 如果 你是 所有 反饋 com 出現
【鏈接】 我是鏈接,點我呀:)
【題意】
給你一個長度為4的數字序列(每個數字都在0~9之間,且不重復出現)
現在讓你猜這個長度為4的序列是什麽.
猜了之後對方會告訴有幾個數字是位置和數字都正確的(猜的數字序列有順序)
以及有幾個數字是數字出現了但是位置不正確.
即給你兩個反饋。
現在給你n個猜的過程(猜的4個數字以及對應的兩個反饋)
問你是否能唯一的確定一個4位數字的答案
【題解】
列舉出來最後的4位數的所有可能。
對於每一種可能 將其對這n個猜的過程驗證一遍》
如果驗證通過,那麽遞增sum
最後如果sum==1則說明存在唯一的一個可能.
sum==0說明沒有任何一個可能,則數據有錯
sum>1則說明需要更多的數據才能確定是哪一個.
【代碼】
import java.io.*; import java.util.*; public class Main { static int N = (int)10; static String s[]; static int b[],c[],n,ans[],sum,final_ans[]; static boolean bo[]; static boolean flag[]; public static boolean check(){ for (int i = 1;i <= n;i++){ for (int j = 0;j <= 9;j++) flag[j] = false; int cnt1 = 0,cnt2 = 0; for (int j = 0;j < 4;j++) if ( (s[i].charAt(j)-'0') == ans[j+1]){ cnt1++; }else{ flag[s[i].charAt(j)-'0'] = true; } for (int j = 1;j <= 4;j++) if (flag[ans[j]]==true){ cnt2++; } if(cnt1==b[i] && cnt2==c[i]){ continue; }else return false; } return true; } public static void dfs(int dep){ if (sum>=2) return; if (dep==5){ if (check()==true){ for (int i = 1;i <= 4;i++) final_ans[i] = ans[i]; sum++; } return; } for (int i = 0;i <= 9;i++) if (bo[i]==false){ bo[i] = true; ans[dep] = i; dfs(dep+1); bo[i] = false; } } public static void main(String args[]){ Scanner in = new Scanner(System.in); bo = new boolean[N+10]; s = new String[N+10]; b = new int[N+10];c = new int[N+10]; ans = new int[N+10]; flag = new boolean[N+10]; final_ans = new int[N+10]; sum = 0; n = in.nextInt(); for (int i = 1;i <= n;i++){ s[i] = in.next();b[i] = in.nextInt();c[i] = in.nextInt(); } dfs(1); if (sum==0){ System.out.println("Incorrect data"); }else if (sum==1){ for (int j = 1;j <= 4;j++) System.out.print(final_ans[j]); }else{ System.out.print("Need more data"); } } }
【Codeforces 63C】Bulls and Cows