T642548 引爆炸弹
错误代码
#include<bits/stdc++.h>
using namespace std;
int n, m, a[1005][1005], ans;
char x;
void dfs(int x, int y){
for(int i = 1; i <= n; i++){
if(a[i][y]){
a[i][y] = 0;
dfs(i, y);
}
}
for(int i = 1; i <= m; i++){
if(a[x][i]){
a[x][i] = 0;
dfs(x, i);
}
}
}
int main(){
cin >> n >> m;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
cin >> x;
a[i][j] = x - '0';
}
}
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
if(a[i][j]){
a[i][j] = 0;
dfs(i, j);
ans++;
}
}
}
cout << ans;
return 0;
}错误原因:手残写错了。
正确代码
#include<bits/stdc++.h>
using namespace std;
int n, m, a[1005][1005], ans;
char x;
void dfs(int x, int y){
a[x][y] = 0;
for(int i = 1; i <= n; i++){
if(a[i][y]){//如果本列有炸弹,则引爆并继续往下找。
dfs(i, y);
}
}
for(int i = 1; i <= m; i++){
if(a[x][i]){//如果本行有炸弹,则引爆并继续往下找。
dfs(x, i);
}
}
}
int main(){
cin >> n >> m;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
cin >> x;
a[i][j] = x - '0';
}
}
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
if(a[i][j]){
dfs(i, j);
ans++;
}
}
}
cout << ans;
return 0;
}思路:有点像连通块,每次都搜索一遍所在的行和列并标记为0。
T643724 落落的去去扫雷
错误代码
#include<bits/stdc++.h>
using namespace std;
int n, a[10005], vis[10005], ans;
void dfs(int t){
if(t == n + 1){
for(int i = 1; i <= n; i++){
int cnt = vis[i] + vis[i - 1] + vis[i + 1];
if(cnt != a[i]){
return;
}
}
ans++;
return;
}
vis[t] = 1;
dfs(t + 1);
vis[t] = 0;
dfs(t + 1);
}
int main(){
cin >> n;
for(int i = 1; i <= n; i++){
cin >> a[i];
}
dfs(1);
cout << ans;
return 0;
}错误原因:暴力骗分。
正确代码
#include<bits/stdc++.h>
using namespace std;
int n, a[10005], f[10005], ans;
bool check(){
for(int i = 1; i <= n; i++){
f[i + 1] = a[i] - f[i] - f[i - 1];
if(f[i + 1] < 0 || f[i + 1] > 1){
return 0;
}
}
if(f[n + 1] != 0){
return 0;
}
return 1;
}
int main(){
cin >> n;
for(int i = 1; i <= n; i++){
cin >> a[i];
}
if(a[1] == 0){//a[1] == 0的话第一列第一个只可能是0。
f[1] = 0;
ans += check();
}
else if(a[1] == 1){//a[1] == 1的话第一列第一个只可能是0, 也可能是1。
f[1] = 1;
ans += check();
memset(f, 0, sizeof(f));
f[1] = 0;
f[2] = 1;
ans += check();
}
else if(a[1] == 2){//a[1] == 0的话第一列第一个和第二个只可能是1。
f[1] = 1;
f[2] = 1;
ans += check();
}
cout << ans;
return 0;
}思路:因为我们只需要知道第一个填了什么,就可以推断出是否合理,所以答案只有0,1,2 三种。