# ACM BFS，DFS入门题（附代码解释）

HDU 1312 Red and Black（BFS）

HDU1372 Knight Moves（BFS）

HDU2717 Catch That Cow（BFS）

HDU 1241 Oil Deposits（DFS）

HDU - 1181 变形课（DFS）

## HDU 1312 Red and Black（BFS）

#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>

using namespace std;
struct node {
int x, y;
};
bool vis[21][21];
int fx[4][2] = {0, 1, 0, -1, 1, 0, -1, 0};
char str[21][21];
int m, n;

int check(int x, int y) {
if (x < 0 || y < 0 || x >= n || y >= m || vis[x][y] || str[x][y] == '#')
return 0;
return 1;
}

int bfs(int x, int y) {
int sum = 1;
node now, next;
now.x = x,now.y = y;
queue<node> q;
q.push(now);
vis[now.x][now.y] = 1;
while (!q.empty()) {
now = q.front();
q.pop();
for (int i = 0; i < 4; i++) {
next.x = now.x + fx[i][0];
next.y = now.y + fx[i][1];
if (check(next.x, next.y)) {
vis[next.x][next.y] = 1;
sum++;
q.push(next);
}
}
}
return sum;
}

int main() {
while (scanf("%d%d", &m, &n) && m + n) {
int x, y;
memset(vis, 0, sizeof(vis));
for (int i = 0; i < n; i++){
scanf("%s", str[i]);
for (int j = 0; j < m; j++) {
if (str[i][j] == '@') {
x = i,y = j;
break;
}
}
}
printf("%d\n",bfs(x, y));
}
return 0;
}

## HDU1372 Knight Moves（BFS）

#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>

using namespace std;
int fx[8][2] = {1, 2, 2, 1, 2, -1, 1, -2, -1, -2, -2, -1, -1, 2, -2, 1};
bool vis[10][10];
char st[5], ed[5];
int x2, y2;
struct node {
int x, y, t;
};

int check(int x, int y) {
if (x < 0 || y <= 0 || x >= 8 || y > 8 || vis[x][y])
return 0;
return 1;
}

int bfs(int x, int y) {
queue<node> q;
node now, next;
now.x = x, now.y = y, now.t = 0;
vis[now.x][now.y] = 1;
q.push(now);
while (!q.empty()) {
now = q.front();
q.pop();
if (now.x == x2 && now.y == y2)
return now.t;
for (int i = 0; i < 8; i++) {
next.x = now.x + fx[i][0];
next.y = now.y + fx[i][1];
if (check(next.x, next.y)) {
next.t = now.t + 1;
vis[next.x][next.y] = 1;
q.push(next);
}
}
}
}

int main() {
while (scanf("%s%s", st, ed) != EOF) {
memset(vis, 0, sizeof(vis));
int x1 = st[0] - 'a', y1 = st[1] - '0';
x2 = ed[0] - 'a', y2 = ed[1] - '0';
int sum = bfs(x1, y1);
printf("To get from %s to %s takes %d knight moves.\n", st, ed, sum);
}
return 0;
}

## HDU2717 Catch That Cow（BFS）

#include<iostream>
#include<stdio.h>
#include<cstring>
#include<algorithm>
#include<queue>

using namespace std;
bool maps[200001];
int x, y;
struct node {
int x, t;
};

int bfs() {
queue<node> q;
node now, next;
now.x = x, now.t = 0;
maps[now.x] = 1;
q.push(now);
while (!q.empty()) {
now = q.front();
q.pop();
if (now.x == y)
return now.t;
next.x = now.x - 1;
if (now.x >= 1 && !maps[next.x]) {
next.t = now.t + 1;
maps[next.x] = 1;
q.push(next);
}
next.x = now.x + 1;
if (now.x <= y && !maps[next.x]) {
next.t = now.t + 1;
maps[next.x] = 1;
q.push(next);
}
next.x = now.x * 2;
if (now.x <= y && !maps[next.x]) {
next.t = now.t + 1;
maps[next.x] = 1;
q.push(next);
}
}
}

int main() {
while (scanf("%d%d", &x, &y) != EOF) {
memset(maps, 0, sizeof(maps));
if (x >= y)///如果牛在左边直接每次走一步去抓牛
printf("%d\n", x - y);
else
printf("%d\n", bfs());
}
return 0;
}

## HDU 1241 Oil Deposits（DFS）

#include<iostream>
#include<algorithm>
#include<cstring>

using namespace std;
char str[101][101];
int fx[8][2] = {0, 1, 0, -1, 1, 0, -1, 0, 1, -1, 1, 1, -1, -1, -1, 1};
int n, m;

int check(int x, int y) {
if (x >= 0 && x < m && y >= 0 && y < n && str[x][y] == '@')
return 1;
return 0;
}

void dfs(int x, int y) {
int x1, y1;
for (int i = 0; i < 8; i++) {
x1 = x + fx[i][0];
y1 = y + fx[i][1];
if (check(x1, y1)) {
str[x1][y1] = '*';
dfs(x1, y1);
}
}
}

int main() {
while (scanf("%d%d", &m, &n) && m) {
int sum = 0;
for (int i = 0; i < m; i++)
scanf("%s", str[i]);
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
if (str[i][j] == '@') {
dfs(i, j);
sum++;
}
printf("%d\n", sum);
}
return 0;
}

## HDU - 1181 变形课（DFS）

PS：简单的DFS，只是注意输入单词要是首尾字母不能够一样，其次不能做首尾单词相同的转化。

#include<stdio.h>
#include<cstring>

using namespace std;
char str2[10000][5];
int a[10000], flag, t;

void dfs(int s) {
for (int i = 0; i < t; i++) {
if (str2[i][0] == str2[s][1] && str2[i][1] == 'm') {
flag = 1;
return;
}
if (!a[i] && str2[i][0] == str2[s][1]) {
a[i] = 1;
dfs(i);
}
}
}

int main() {
char temp[1000];
while (scanf("%s", temp) != EOF) {
int s = 0, l = strlen(temp);
if (temp[0] != temp[l - 1]) {
str2[s][0] = temp[0];
str2[s][1] = temp[l - 1];
s++;
}
while (scanf("%s", temp) && temp[0] != '0') {
int l = strlen(temp);
if (temp[0] != temp[l - 1]) {
str2[s][0] = temp[0];
str2[s][1] = temp[l - 1];
s++;
}
}
t = s,flag = 0;
memset(a, 0, sizeof(a));
for (int i = 0; i < s; i++) {
if (str2[i][0] == 'b')
a[i] = 1;
dfs(i);
}
if (flag)
printf("Yes.\n");
else
printf("No.\n");
}
return 0;
}


08-11 6471

02-04 2753

06-02 193

05-09 202

08-11 104

11-26 1858

03-09 554

02-16 416

05-27 2197

#### 杭电1253HDU acm---胜利大逃亡----BFS广度优先算法实现

©️2020 CSDN 皮肤主题: 创作都市 设计师: CSDN官方博客

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。