题目大意:给你三个数n
,n1
,n2
,n3
。你可以对n
做下面三种操作之一:
最多做100次操作,且n
不能等于n1
,n2
,n3
中的任意一个,问你是否可以将n
变成0。
因为这题只有三种转移状态,我第一反应想到的是bfs搜索,很好实现,代码如下:
//https://blog.csdn.net/hesorchen
// #include <bits/stdc++.h>
#include <iostream>
#include <algorithm>
#include <string>
#include <queue>
#include <cstring>
using namespace std;
#define ll long long
#define endl "\n"
#define mod 1000000007
int n, n1, n2, n3;
int t[308];
struct node
{
int zhi, step;
} a, b;
queue<node> q;
int bfs()
{
a.zhi = n;
a.step = 0;
q.push(a);
while (!q.empty())
{
a = q.front();
q.pop();
if (a.step > 100 || a.zhi == n1 || a.zhi == n2 || a.zhi == n3||t[a.zhi])
continue;
t[a.zhi]=1;
if (a.zhi == 0 && a.step <= 100)
return 1;
if (a.zhi - 1 >= 0)
{
b.zhi = a.zhi - 1;
b.step = a.step + 1;
q.push(b);
}
if (a.zhi - 2 >= 0)
{
b.zhi = a.zhi - 2;
b.step = a.step + 1;
q.push(b);
}
if (a.zhi - 3 >= 0)
{
b.zhi = a.zhi - 3;
b.step = a.step + 1;
q.push(b);
}
}
return 0;
}
int main()
{
cin >> n >> n1 >> n2 >> n3;
if (bfs())
printf("YES\n");
else
printf("NO\n");
return 0;
}
赛后有人贴出代码之后发现这其实是贪心+模拟,因为有次数限制,所以每一次都尽量多减一点。
AC代码:
//https://blog.csdn.net/hesorchen
// #include <bits/stdc++.h>
#include <iostream>
using namespace std;
#define ll long long
#define endl "\n"
#define mod 1000000007
int n, a, b, c;
int check(int x)
{
if (x != a && x != b && x != c)
return 1;
return 0;
}
int main()
{
cin >> n >> a >> b >> c;
if (!a || !b || !c || !check(n))
return !printf("NO\n");
int flag = 1;
int step = 0;
while (n > 0)
{
step++;
if (check(n - 3))
n -= 3;
else if (check(n - 2))
n -= 2;
else if (check(n - 1))
n -= 1;
else
{
flag = 0;
break;
}
}
if (flag && step <= 100)
return !printf("YES\n");
printf("NO\n");
return 0;
}