Childhood dream 搜索(广搜)
Childhood dream
搜索
1,“天赋异禀”, “足够机智” 又是一道劝退题。。。
2,A 前面的数字说明与答案相比,有多少个位置上的数字是相同的。
B 前面的数字说明与答案相比,有多 少个数字是相同的,但是位置不一样。
观察这句话发现,毫无逻辑,想到的只有暴力。如果存在A前面的数大于3的话,直接跑暴力应该能过,但都小于3的话,没招了,B前面的数字想不起来有什么意义。
3,刚说的暴力就是 枚举答案,每一个答案都要判断是否满足m个条件,这仅仅判断答案是否合法就花费 O(m * 10) = O(1000),也就是最多只能检查1e5个答案,故考虑换一下思路
4,考虑广搜,按位搜索,从后往前依次确立每个答案是否合法,及时减去不合法的字符串 ,估计应该能过,毕竟答案就一种,让他们造个卡这个算法的数据估计都不太好造。莽有莽,单车变摩托。
5,莽了一发,完美 1ms 直接过了。
#include
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 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> 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;
}