1. 最短路径+判断无解:
例题: ybt 1251:仙岛求药
BFS:
#include <bits/stdc++.h>
using namespace std;
const int N = 105;
constexpr int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1};
struct node { int x, y, step; } S, E;
int m, n;
char a[N][N];
bool vis[N][N];
inline bool input() {
scanf("%d%d", &m, &n);
if (m == 0 && n == 0) return false;
for (int i = 1; i <= m; i++) {
scanf("%s", a[i] + 1);
for (int j = 1; j <= n; j++) {
if(a[i][j] == '@') S = {i, j};
if(a[i][j] == '*') E = {i, j};
}
}
memset(vis, false, sizeof(vis));
return true;
}
inline void bfs() {
auto isValid = [](int x, int y) {
return (1 <= x && x <= m && 1 <= y && y <= n && a[x][y] != '#' && !vis[x][y]);
};
queue<node> q; q.push(S);
while (!q.empty()) {
node u = q.front(); q.pop();
for(int i = 0; i < 4; i++) {
int _x = u.x + dx[i], _y = u.y + dy[i];
if (!isValid(_x, _y)) continue;
if (_x == E.x && _y == E.y) {
printf("%d\n", u.step + 1);
return;
}
q.push({_x, _y, u.step + 1});
vis[_x][_y] = true;
}
}
printf("-1\n");
}
signed main() {
while (input()) bfs();
return 0;
}
DFS:
#include <bits/stdc++.h>
using namespace std;
const int N = 105;
constexpr int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1};
int m, n, step[N][N];
char a[N][N];
bool hasAns;
int st_x, st_y, ed_x, ed_y;
inline bool input() {
scanf("%d%d", &m, &n);
if (m == 0 && n == 0) return false;
for (int i = 1; i <= m; i++) {
scanf("%s", a[i] + 1);
for (int j = 1; j <= n; j++) {
if(a[i][j] == '@') st_x = i, st_y = j;
if(a[i][j] == '*') ed_x = i, ed_y = j;
}
}
memset(step, 0x3f, sizeof(step)); step[st_x][st_y] = 0;
return true;
}
inline bool isValid(int x, int y) {
return (1 <= x && x <= m && 1 <= y && y <= n && a[x][y] != '#');
};
inline void dfs(int x, int y) {
if (a[x][y] == '*') {
hasAns = true;
return;
}
for(int i = 0; i < 4; i++) {
int _x = x + dx[i], _y = y + dy[i];
if (!isValid(_x, _y)) continue;
if (step[_x][_y] <= step[x][y] + 1) continue;
step[_x][_y] = step[x][y] + 1;
dfs(_x, _y);
}
}
signed main() {
while (input()) {
hasAns = false;
dfs(st_x, st_y);
if (hasAns) printf("%d\n", step[ed_x][ed_y]);
else printf("-1\n");
}
return 0;
}
2. 连通块个数+最大面积:
例题: 计蒜客 T1405 最大岛屿
BFS:
#include <bits/stdc++.h>
using namespace std;
const int N = 505;
constexpr int dx[]= {0, 0, 1, -1, 1, -1, 1, -1};
constexpr int dy[]= {1, -1, 0, 0, 1, -1, -1, 1};
struct node { int x, y; };
int m, n, t;
char a[N][N];
bool vis[N][N];
int cnt, ans, now;
inline void bfs(int x,int y) {
auto isValid = [](int x, int y) {
return (1 <= x && x <= m && 1 <= y && y <= n && a[x][y] != '0' && !vis[x][y]);
};
queue<node> q; q.push({x, y});
while (!q.empty()) {
node u = q.front(); q.pop();
for (int i = 0; i < 8; i++) {
int _x = u.x + dx[i], _y = u.y + dy[i];
if (!isValid(_x, _y)) continue;
vis[_x][_y] = true;
now++;
q.push({_x, _y});
}
}
}
inline void input() {
scanf("%d%d%d", &m, &n, &t);
for (int i = 1; i <= m; i++)
for (int j = 1; j <= n; j++) {
do {
a[i][j] = getchar();
} while (a[i][j] != '0' && a[i][j] != '1');
}
}
inline void solve() {
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
if (a[i][j] == '0' || vis[i][j]) continue;
vis[i][j] = true;
now = 1;
bfs(i, j);
ans = max(ans, now);
cnt++;
}
}
printf("%d %d\n", cnt, ans * t);
}
signed main() {
input();
solve();
return 0;
}
DFS:
#include <bits/stdc++.h>
using namespace std;
const int N = 505;
constexpr int dx[]= {0, 0, 1, -1, 1, -1, 1, -1};
constexpr int dy[]= {1, -1, 0, 0, 1, -1, -1, 1};
int m, n, t;
char a[N][N];
bool vis[N][N];
int cnt, ans, now;
inline void input() {
scanf("%d%d%d", &m, &n, &t);
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
do {
a[i][j] = getchar();
} while (a[i][j] != '0' && a[i][j] != '1');
}
}
}
inline bool isValid (int x, int y) {
return (1 <= x && x <= m && 1 <= y && y <= n && a[x][y] != '0' && !vis[x][y]);
}
inline void dfs(int x,int y) {
for (int i = 0; i < 8; i++) {
int _x = x + dx[i], _y = y + dy[i];
if (!isValid(_x, _y)) continue;
vis[_x][_y] = true;
now++;
dfs(_x,_y);
}
}
inline void solve() {
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
if (a[i][j] == '0' || vis[i][j]) continue;
vis[i][j] = true;
now = 1;
dfs(i, j);
ans = max(ans, now);
cnt++;
}
}
printf("%d %d\n", cnt, ans * t);
}
signed main() {
input();
solve();
return 0;
}