P1683 入门
错误原因
算法使用错误。
这道题应该使用DFS算法而不是BFS算法
错误代码
#include <bits/stdc++.h>
using namespace std;
char a[25][25];
bool vis[25][25];
int dx[] = {0, 0, -1, 1}, w, h;
int dy[] = {-1, 1, 0, 0};
struct node {
int x, y, step;
};
int bfs(int x, int y) {
memset(vis, false, sizeof vis);
queue <node> q;
q.push({x, y, 1});
vis[x][y];
while(!q.empty()) {
node t = q.front();
q.pop();
bool flag = false;
for (int i = 0; i < 4; i++) {
int nx = t.x + dx[i];
int ny = t.y + dy[i];
if (nx >= 1 && nx <= w && ny >= 1 && ny <= h && vis[nx][ny] == false && a[nx][ny] != '#')
q.push({nx, ny, t.step + 1}), vis[nx][ny] = true, flag = true;
}
if (!flag)
return t.step;
}
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> w >> h;
int x, y;
for (int i = 1; i <= w; i++)
for (int j = 1; j <= h; j++) {
cin >> a[i][j];
if (a[i][j] == '@')
x = i, y = j;
}
cout << bfs(x, y);
return 0;
}
正确思路
使用DFS算法起点开始走,然后一直走到走不动为止
正确代码
#include <bits/stdc++.h>
using namespace std;
char a[25][25];
bool vis[25][25];
int w, h;
int cnt;
void dfs(int x, int y) {
if (x < 1 || x > w || y < 1 || y > h)
return ; //判断是否越界
if (a[x][y] == '#')
return ; //判断是否是障碍物
if (vis[x][y])
return ; //判断是否走过
cnt++; //步数 + 1
vis[x][y] = true; //标记vis数组
//调用四个方向的函数
dfs(x, y - 1);
dfs(x - 1, y);
dfs(x + 1, y);
dfs(x, y + 1);
return ;
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
memset(vis, false, sizeof vis);
cin >> h >> w;
int x, y;
for (int i = 1; i <= w; i++)
for (int j = 1; j <= h; j++) {
cin >> a[i][j];
if (a[i][j] == '@')
x = i, y = j; //记录起点
}
dfs(x, y); //调用函数
cout << cnt;
return 0;
}
P2036 [COCI 2008/2009 #2] PERKET
错误原因
方法错误
错误代码
#include <bits/stdc++.h>
using namespace std;
struct node {
int s, b, ans;
} a[15];
bool cmp(node x, node y) {
return x.ans < y.ans;
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i].s >> a[i].b;
a[i].ans = abs(a[i].s - a[i].b);
}
sort(a + 1, a + 1 + n, cmp);
int s = 1, b = 0, ans = INT_MAX;
for (int i = 1; i <= n; i++) {
int ss = s, bb = b;
s *= a[i].s, b += a[i].b;
if (abs(s - b) <= ans)
ans = abs(s - b);
else
s = ss, b = bb;
}
cout << ans;
return 0;
}
正确思路
这是一道DFS选与不选的题目,看是选这个配料还是不选这个配料
正确代码
#include <bits/stdc++.h>
using namespace std;
struct node {
int s, b;
} a[15];
int n, ans = INT_MAX;
void dfs(int step, int s, int b, int cnt) {
if(cnt >= 1) ans = min(abs(s - b), ans); //只有配料数量>=1才能记录答案
if (step > n) //判断是否越界
return ;
dfs(step + 1, s * a[step].s, b + a[step].b, cnt + 1); //调用选这个配料的DFS
dfs(step + 1, s, b, cnt); //调用不选这个配料的DFS
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i].s >> a[i].b;
dfs(1, 1, 0, 0); //调用DFS函数
cout << ans;
return 0;
}
P10377 [GESP202403 六级] 好斗的牛
正确思路
使用全排列枚举每只牛与其他牛的相对位置来判断要留下多少个牛棚,最后选出需要留下牛棚最少的方案
正确代码
#include <bits/stdc++.h>
using namespace std;
int a[15], b[15], c[15], n;
bool vis[15];
int ans = INT_MAX;
int js() {
int cow = 1;
for (int i = 2; i <= n; i++) {
int dis = max(b[c[i - 1]], a[c[i]]);
cow += dis + 1;
}
return cow;
}
void dfs(int step) {
if (step > n) {
int res = js();
ans = min(res, ans);
return ;
}
for (int i = 1; i <= n; i++)
if (vis[i] == false) {
vis[i] = true;
c[step] = i;
dfs(step + 1);
vis[i] = false;
}
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i];
for (int i = 1; i <= n; i++)
cin >> b[i];
dfs(1);
cout << ans;
return 0;
}
P1506 拯救oibh总部
错误原因
思路错误,原思路:判断每个重要区域是否上下左右都有围墙。原思路会出现的问题:如果遇到重要区域在一块且被围墙围住就会出现问题
错误代码
#include <bits/stdc++.h>
using namespace std;
char a[505][505];
int dx[] = {0, 0, -1, 1};
int dy[] = {-1, 1, 0, 0};
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
int x, y;
cin >> x >> y;
for (int i = 1; i <= x; i++)
for (int j = 1; j <= y; j++)
cin >> a[i][j];
int ans = 0;
for (int i = 1; i <= x; i++)
for (int j = 1; j <= y; j++)
if (a[i][j] == '0') {
bool t = true;
for (int k = 0; k < 4; k++)
if (a[i + dx[k]][j + dy[k]] != '*') {
t = false;
break;
}
if (t)
ans++;
}
cout << ans;
return 0;
}
正确思路
模拟洪水的泛滥,将洪水可以到达的地方标记,最后数有多少给没有被标记的重要区域即可
正确代码(BFS)
#include <bits/stdc++.h>
using namespace std;
char a[505][505];
//方向数组
int dx[] = {0, 0, -1, 1}, x, y;
int dy[] = {-1, 1, 0, 0};
struct node {
int x, y;
};
void bfs(int x1, int y1) {
queue <node> q;
q.push({x1, y1});
a[x][y] = '*';
while (!q.empty()) {
node t = q.front();
q.pop();
for (int i = 0; i < 4; i++) {
int nx = t.x + dx[i];
int ny = t.y + dy[i];
if (nx >= 0 && nx <= x + 1 && ny >= 0 && ny <= y + 1 && a[nx][ny] != '*')
q.push({nx, ny}), a[nx][ny] = '*'; //标记(这里直接将洪水走过的地方标记为障碍了)
}
}
return ;
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> x >> y;
for (int i = 1; i <= x; i++)
for (int j = 1; j <= y; j++)
cin >> a[i][j];
bfs(0, 0); //调用BFS函数
int ans = 0;
//数没有被淹没的重要区域
for (int i = 1; i <= x; i++)
for (int j = 1; j <= y; j++)
if (a[i][j] == '0')
ans++;
cout << ans;
return 0;
}
正确代码(DFS)
#include <bits/stdc++.h>
using namespace std;
int dx[10] = {0, 0, -1, 1};
int dy[10] = {1, -1, 0, 0};
int n, m;
char a[510][510];
void dfs(int x, int y) {
if (x < 0 || y < 0 || x > n + 1 || y > m + 1 || a[x][y] == '*')
return ;
a[x][y] = '*'; //标记(这里直接将洪水走过的地方标记为障碍了)
for (int i = 0; i < 4; i++)
dfs(x + dx[i], y + dy[i]);
return ;
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
cin >> a[i][j];
dfs(0, 0); //调用DFS函数
int ans = 0;
//数没有被淹没的重要区域
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
if (a[i][j] == '0')
ans++;
cout << ans;
return 0;
}