1. 程式人生 > 其它 >Childhood dream 搜尋(廣搜)

Childhood dream 搜尋(廣搜)

Childhood dream

搜尋

1,“天賦異稟”, “足夠機智” 又是一道勸退題。。。
2,A 前面的數字說明與答案相比,有多少個位置上的數字是相同的。
B 前面的數字說明與答案相比,有多 少個數字是相同的,但是位置不一樣。
觀察這句話發現,毫無邏輯,想到的只有暴力。如果存在A前面的數大於3的話,直接跑暴力應該能過,但都小於3的話,沒招了,B前面的數字想不起來有什麼意義。
3,剛說的暴力就是 列舉答案,每一個答案都要判斷是否滿足m個條件,這僅僅判斷答案是否合法就花費 O(m * 10) = O(1000),也就是最多隻能檢查1e5個答案,故考慮換一下思路
4,考慮廣搜,按位搜尋,從後往前依次確立每個答案是否合法,及時減去不合法的字串 ,估計應該能過,畢竟答案就一種,讓他們造個卡這個演算法的資料估計都不太好造。莽有莽,單車變摩托。
5,莽了一發,完美 1ms 直接過了。

#include <bits/stdc++.h>
using namespace std;

#define endl '\n'
#define int long long
#define fi first
#define se second
#define pb push_back

#define foa(x, y, z) for(int x = (y), ooo = (z); x <= z; ++x)
#define fos(x, y, z) for(int x = (y), ooo = (z); x >= z; --x)
#define ckmax(x, y) ((x) < (y) ? (x) = (y), 1 : 0)
#define ckmin(x, y) ((x) > (y) ? (x) = (y), 1 : 0)

typedef pair<int, int> pii;
typedef long long ll;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f3f3f3f3f;
const int N = 1e2 + 10;
int n, m;

struct node
{
	string s;
	int a, b;
	int p[10];
	void init()
	{
		memset(p, -1, sizeof(p));
		foa(i, 0, s.size() - 1) p[s[i] - '0'] = i;
	}
	bool valid(string &str, int x)
	{
		int num = 0, num2 = 0;
		foa(i, 0, x) {
			if(s[i] == str[i]) num++;
			int t = p[str[i] - '0'];
			if(t != -1 && i != t) num2++;
		}
		if(num > a || num2 > b) return false;
		if(x == m - 1 && (num != a || num2 != b)) return false;
		return true;
	}
};
node a[N];
bool check(string &s, int h)
{
	foa(i, 1, n) 
		if(!a[i].valid(s, h))
			return false;
	return true;
}
void solve()
{
	cin >> n >> m;
	foa(i, 1, n) {
		cin >> a[i].s >> a[i].a >> a[i].b;
		a[i].init();
	}
	queue<pair<string, int>> q;
	q.push({string (m, '0'), -1});
	while(q.size()) {
		auto t = q.front();
		q.pop();
		string s = t.fi;
		int h = t.se + 1;
		if(h == m) {
			cout << s << endl;
			return;
		}
		foa(i, '0', '9') {
			s[h] = i;
			if(check(s, h))
				q.push({s, h});
		}
	}
}
signed main()
{
	 ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    
	solve();
    return 0;
}