#P3071. 猜数字(100分)
          
                        
                                    
                      
        
              - 
          
          
                      1000ms
            
          
                      Tried: 148
            Accepted: 37
            Difficulty: 4
            
          
          
          
                       所属公司 : 
                              华为od
                                
            
                      
          
 
- 
                        算法标签>暴力枚举          
 
猜数字(100分)
模拟+暴力模拟
由于答案只有10000个数,我们可以去暴力枚举每一个数去验证所有猜测要求是否正确,假设我们现在枚举1111,去跟猜测结果比较,是否等于提示结果。如果n组都等于那么这是一个可能的答案,枚举全部后只有一个可能的答案,那这个答案就是真正的答案。具体还要实现一个fun(string,string)用来得到提示结果的详细请看代码
代码如下
cpp
#include <bits/stdc++.h>
using namespace std;
#define N 100005
#define int long long
string a[N],b[N];
string fun(string a,string b){
	int res1=0,res2=0;
	int vis[5]={0};
	for(int i=0;i<4;i++){
		if(a[i]==b[i]){
			res1++;
			vis[i]=1;
		}
	}
	for(int i=0;i<4;i++){
		if(a[i]!=b[i]){
			for(int j=0;j<4;j++){
				if(a[i]==b[j]&&vis[j]==0){
					res2++;
					vis[j]=1;
					break;
				}
			}
		}
	}
	return to_string(res1)+'A'+to_string(res2)+'B';
}
signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	int ans=0;
	string ans1;
	int n;
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>a[i]>>b[i];
	}
	for(int i1=0;i1<=9;i1++){
		for(int i2=0;i2<=9;i2++){
			for(int i3=0;i3<=9;i3++){
				for(int i4=0;i4<=9;i4++){
					string s=to_string(i1)+to_string(i2)+to_string(i3)+to_string(i4);
					int res=0;
						for(int i=1;i<=n;i++){	
							string to=fun(a[i],s);
							if(to==b[i]) res++;
						}
						
						if(res==n){
							ans++;
							ans1=s;
						}
				}
			}
		}
	}
	if(ans==1){
		cout<<ans1<<'\n';
	}
	else{
		cout<<"NA"<<'\n';
	}
	return 0;
}
import java.util.Scanner;
public class Main {
    public static String calculateHint(String secret, String guess) {
        int correctPosition = 0, correctNumber = 0;
        int[] visited = new int[4];
        for (int i = 0; i < 4; i++) {
            if (secret.charAt(i) == guess.charAt(i)) {
                correctPosition++;
                visited[i] = 1;
            }
        }
        for (int i = 0; i < 4; i++) {
            if (secret.charAt(i) != guess.charAt(i)) {
                for (int j = 0; j < 4; j++) {
                    if (secret.charAt(i) == guess.charAt(j) && visited[j] == 0) {
                        correctNumber++;
                        visited[j] = 1;
                        break;
                    }
                }
            }
        }
        return correctPosition + "A" + correctNumber + "B";
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        String[] guesses = new String[n];
        String[] hints = new String[n];
        for (int i = 0; i < n; i++) {
            guesses[i] = sc.next();
            hints[i] = sc.next();
        }
        int answerCount = 0;
        String finalAnswer = "";
        for (int i1 = 0; i1 <= 9; i1++) {
            for (int i2 = 0; i2 <= 9; i2++) {
                for (int i3 = 0; i3 <= 9; i3++) {
                    for (int i4 = 0; i4 <= 9; i4++) {
                        String currentGuess = "" + i1 + i2 + i3 + i4;
                        int matches = 0;
                        for (int i = 0; i < n; i++) {
                            String hint = calculateHint(guesses[i], currentGuess);
                            if (hint.equals(hints[i])) {
                                matches++;
                            }
                        }
                        if (matches == n) {
                            answerCount++;
                            finalAnswer = currentGuess;
                        }
                    }
                }
            }
        }
        if (answerCount == 1) {
            System.out.println(finalAnswer);
        } else {
            System.out.println("NA");
        }
        sc.close();
    }
}
def calculate_hint(secret, guess):
    correct_position = 0
    correct_number = 0
    visited = [0] * 4
    for i in range(4):
        if secret[i] == guess[i]:
            correct_position += 1
            visited[i] = 1
    for i in range(4):
        if secret[i] != guess[i]:
            for j in range(4):
                if secret[i] == guess[j] and visited[j] == 0:
                    correct_number += 1
                    visited[j] = 1
                    break
    return f"{correct_position}A{correct_number}B"
def main():
    n = int(input())
    guesses = []
    hints = []
    for _ in range(n):
        guess, hint = input().split()
        guesses.append(guess)
        hints.append(hint)
    answer_count = 0
    final_answer = ""
    for i1 in range(10):
        for i2 in range(10):
            for i3 in range(10):
                for i4 in range(10):
                    current_guess = f"{i1}{i2}{i3}{i4}"
                    matches = 0
                    for i in range(n):
                        if calculate_hint(guesses[i], current_guess) == hints[i]:
                            matches += 1
                    if matches == n:
                        answer_count += 1
                        final_answer = current_guess
    if answer_count == 1:
        print(final_answer)
    else:
        print("NA")
if __name__ == "__main__":
    main()
        题目内容
一个人设定一组四码的数字作为谜底,另一方猜。
每猜一个数,出数者就要根据这个数字给出提示,提示以XAYB形式呈现,直到猜中位置。
其中X表示位置正确的数的个数(数字正确且位置正确),而Y表示数字正确而位置不对的数的个数。
例如,当谜底为8123,而猜谜者猜1052时,出题者必须提示0A2B。
例如,当谜底为5637,而猜谜者才4931时,出题者必须提示1A0B。
当前已知N组猜谜者猜的数字与提示,如果答案确定,请输出答案,不确定则输出NA。
输入描述
第一行输入一个正整数,0<N<100。
接下来N行,每一行包含一个猜测的数字与提示结果。
输出描述
输出最后的答案,答案不确定则输出NA。
样例1
输入
6
4815 1A1B
5716 0A1B
7842 0A1B
4901 0A0B
8585 3A0B
8555 2A1B
输出
3585
说明