考试内容: 模拟
比赛情况
错题总结
T2 - [SCOI2005] 扫雷
题意概括
给定一个 的扫雷棋盘。其中,第二列(右列)是已知的数字(表示其周围相邻的雷的数量),第一列(左列)是未知的雷区。需要计算第一列可能的雷的摆放方案数。
正确思路
首先,我们发现,第一行的棋子如何摆放其实已经决定了接下来的棋子如何摆放,也就是说,我们只需判断在 1 号格子放雷是,和 1 号格子不放雷 这些情况的可行性即可得到答案,如果可行将答案加 ,否则不变。
所以,我们可以在这里使用 dfs 来解决,我们可以暴力枚举所有可能性(可行性剪枝),如果全部填完且全部合法就将答案加 。
最后输出答案即可。
时空复杂度
- 时间复杂度:大约
- 空间复杂度:
参考代码
#include <iostream>
using namespace std;
const int N = 1e4 + 5;
int ans = 0;
int a[N],b[N];
bool check(int x){
return b[x - 1] + b[x] + b[x + 1] == a[x];
}
void dfs(int step,int n){
if(step == n + 1){
if(check(n)) ans++;
return;
}
b[step] = true;
if(step == 1 || check(step - 1)) dfs(step + 1,n);
b[step] = false;
if(step == 1 || check(step - 1)) dfs(step + 1,n);
}
int main(){
int n;
cin >> n;
for(int i = 1;i <= n;i++){
cin >> a[i];
}
dfs(1,n);
cout << ans;
return 0;
}