题解 P2327 [SCOI2005]扫雷

题解 P2327 [SCOI2005]扫雷

题目链接

普及组水题非常适合新手练搜索

看题解里各路神仙都用各种简单的方法,我来讲讲暴力搜索做法....

对于我这样的萌新我觉得这样的做法更容易让你理解搜索

首先搜索要定义状态,我们定义dfs(x, p)是当第x位是p,就有了状态

然后我们讨论第x + 1位的情况只有两种情况,然后就有了转移

其次当x是n时,如果成立,则++ans,于是有了边界条件

最后你把这段代码复制粘贴得了100分,发现原来这有点像动态规划

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define ll long long
#define N 100005
int a[N], b[N], n;//a是左边那排,b是每一个格子是否放雷
ll ans;
void dfs(int x, int p) //第x个格子放 p个雷
{
    b[x] = p;
    if (x == n) //边界
    {
        if (b[x] + b[x - 1] == a[x]) //第n个格子放 p个雷 成立
            ans++;
        return;
    }
    for (int i = 0; i <= 1; i++) //讨论第x + 1个格子放不放雷
        if (b[x] + b[x - 1] + i == a[x] && b[x] + i <= a[x + 1])
            dfs(x + 1, i); //转移(滑稽
    b[x] = 0;
}
int main()
{
    cin >> n;
    for (int i = 1; i <= n; i++)
        cin >> a[i];
    dfs(1, 1);
    dfs(1, 0);
    cout << ans;
    return 0;
}

19.08.18

猜你喜欢

转载自www.cnblogs.com/YuanqiQHFZ/p/11622376.html