7月5日深度优先搜索1--路径总结
课堂练习
P1331 海战
题目背景
在峰会期间,武装部队得处于高度戒备。警察将监视每一条大街,军队将保卫建筑物,领空将布满了
此外,巡洋船只和舰队将被派去保护海岸线。不幸的是,因为种种原因,国防海军部仅有很少的几位军官能指挥大型海战。因此,他们培养了一些新海军指挥官。军官们选择了“海战”游戏来帮助他们学习。
题目描述
在一个方形的盘上,放置了固定数量和形状的船只,每只船却不能碰到其它的船。在本题中,我们认为船是方形的,所有的船只都是由图形组成的方形。
求出该棋盘上放置的船只的总数。
输入格式
第一行为两个整数
接下来 # 或 .。# 表示船只的一部分,. 表示水。
输出格式
一行一个字符串,如果船的位置放得正确(即棋盘上只存在相互之间不能接触的方形,如果两个 # 号上下相邻或左右相邻却分属两艘不同的船只,则称这两艘船相互接触了)。就输出 There are S ships.,Bad placement.。
错误代码
无
错误原因
无
正确思路
- 这道题目本质上就是连通块题目。
- 双重循环枚举每个位置的符号,如果是
#号并且没被vis数组标记过, 就dfs把#号属于的连通块都用vis数组标记,完成dfs后ans++。 - 注意!!!这个题目与其他连通块题目不一样的是:每个连通块必须都是矩形,否则输出
Bad placement.。对于这个问题可以在dfs的时候记录连通块的左上角与右下角,和#号的数量,最后再看看数量是否匹配就可以了。
正确代码
#include <bits/stdc++.h>
using namespace std;
int n, m, ans, sum, min_x, min_y, max_x, max_y;
const int N=1007;
char a[1007][1007];
bool vis[1007][1007];
int dx[] = {0, 0, -1, 1};
int dy[] = {-1, 1, 0, 0};
void dfs(int x, int y) {
vis[x][y] = true;
min_x = min(min_x, x);
min_y = min(min_y, y);
max_x = max(max_x, x);
max_y = max(max_y, y);
sum++;
for (int i = 0; i < 4; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if (nx >= 1 && nx <= n && ny >= 1 && ny <= m && vis[nx][ny] == false && a[nx][ny] == '#') {
dfs(nx, ny);
}
}
}
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> a[i][j];
}
}
for (int i = 1; i <=n; i++) {
for (int j = 1; j <= m; j++) {
if (a[i][j] == '#' && vis[i][j] == false) {
sum = 0;
min_x = 0x3f3f3f3f; min_y = 0x3f3f3f3f;
max_x = 0; max_y = 0;
dfs(i, j);
if ((max_x - min_x + 1) * (max_y - min_y + 1) != sum) {
cout << "Bad placement.";
return 0;
}
ans++;
}
}
}
cout << "There are " << ans << " ships.";
return 0;
}
P1958 上学路线
题目描述
你所在城市的街道好像一个棋盘,有
你住在
现在有
问你上学一共有多少走法?
输入格式
第一行包含两个整数
第二行包含一个整数
接下来
输出格式
输出一个整数表示从
错误代码
无
错误原因
无
正确思路
- dfs从
(1,1) 开始搜索如果到了终点(a,b) ,就ans++,回溯。 - 如果是障碍的话,也只需要特判一下。
正确代码
#include <bits/stdc++.h>
using namespace std;
int n, m, ans;
const int N=1007;
int a[20][20];
bool vis[1007][1007];
int dx[] = {0, 1};
int dy[] = {1, 0};
void dfs(int x, int y) {
if (x == n && y == m) {
ans++; return ;
}
for (int i = 0; i < 2; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if (nx >= 1 && nx <= n && ny >= 1 && ny <= m && vis[nx][ny] == false && a[nx][ny] != 1) {
vis[nx][ny] = 1;
dfs(nx, ny);
vis[nx][ny] = 0;
}
}
}
int main() {
cin >> n >> m;
int t;
cin >> t;
for (int i = 1; i <= t; i++) {
int l, r;
cin >> l >> r;
a[l][r] = 1;
}
vis[1][1] = 1;
dfs(1, 1);
cout << ans;
return 0;
}
D3432 【基础】迷宫的第一条出路
题目描述
已知一N×N的迷宫,允许往上、下、左、右四个方向行走,现请你按照左、上、右、下顺序进行搜索,找出第一条从左上角到右下角的路径。
输入
输入数据有若干行,第一行有一个自然数
输出
输出数据仅一行,为按照要求的搜索顺序找到的从入口到出口的第一条路径(搜索顺序:左、上、右、下)。
错误代码
无
错误原因
无
正确思路
-
用一个数组
pri 记录当前的这个位置是由哪个位置走来的。 -
如果到了总点就递归输出
pri 的内容就好了。代码如下:
void print(int x, int y) {
if (x == 1 && y == 1) {
cout << "(1,1)";
return;
}
else {
print(pre[x][y].x, pre[x][y].y);
cout << "->(" << x << "," << y << ")";
}
}
- 由于题目要求搜索顺序为左、上、右、下,因此方向数组得进行改变。
正确代码
#include <bits/stdc++.h>
using namespace std;
int n, m, ans;
const int N=1007;
char a[25][25];
bool vis[1007][1007];
struct node {
int x, y;
}pre[25][25];
int dx[] = {0, -1, 0, 1};
int dy[] = {-1, 0, 1, 0};
void print(int x, int y) {
if (x == 1 && y == 1) {
cout << "(1,1)";
return;
}
else {
print(pre[x][y].x, pre[x][y].y);
cout << "->(" << x << "," << y << ")";
}
}
void dfs(int x, int y) {
if (x == n && y == n) {
print(x, y);
}
for (int i = 0; i < 4; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if (nx >= 1 && nx <= n && ny >= 1 && ny <= n && vis[nx][ny] == false && a[nx][ny] == '0') {
pre[nx][ny] = {x, y};
vis[nx][ny] = 1;
dfs(nx, ny);
}
}
}
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
cin >> a[i][j];
}
}
vis[1][1] = 1;
dfs(1, 1);
return 0;
}
/*
4
0001
0100
0010
0110
*/
D3361 【基础】卒的遍历
题目描述
在一张
输入
两个整数
输出
卒的行走路线
错误代码
无
错误原因
无
正确思路
1.和前一题很像,这里不多讲。
正确代码
#include <bits/stdc++.h>
using namespace std;
int n, m, ans;
const int N=1007;
char a[25][25];
bool vis[1007][1007];
struct node {
int x, y;
}pre[25][25];
int dx[] = {1, 0};
int dy[] = {0, 1};
void print(int x, int y) {
if (x == 1 && y == 1) {
cout << "1,1";
return;
}
else {
print(pre[x][y].x, pre[x][y].y);
cout << "->" << x << "," << y;
}
}
void dfs(int x, int y) {
if (x == n && y == m) {
ans++; cout << ans << ":";
print(x, y); cout << endl;
}
for (int i = 0; i < 2; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if (nx >= 1 && nx <= n && ny >= 1 && ny <= m) {
pre[nx][ny] = {x, y};
dfs(nx, ny);
}
}
}
int main() {
cin >> n >> m;
dfs(1, 1);
return 0;
}
/*
4
0001
0100
0010
0110
*/
P1238 走迷宫
题目描述
有一个
优先顺序:左上右下。
输入格式
第一行是两个数
输出格式
所有可行的路径,描述一个点时用 -> 表示方向。
如果没有一条可行的路则输出
错误代码
无
错误原因
无
正确思路
- 于前两题几乎一样。只需要小小改一下就好了。
正确代码
#include <bits/stdc++.h>
using namespace std;
int n, m, ans;
const int N=1007;
int a[25][25];
bool vis[1007][1007];
int sx, sy, tx, ty;
struct node {
int x, y;
}pre[25][25];
int dx[] = {0, -1, 0, 1};
int dy[] = {-1, 0, 1, 0};
void print(int x, int y) {
if (x == sx && y == sy) {
cout << "(" << sx << "," << sy << ")";
return;
}
else {
print(pre[x][y].x, pre[x][y].y);
cout << "->(" << x << "," << y << ")";
}
}
void dfs(int x, int y) {
if (x == tx && y == ty) {
ans++;
print(x, y); cout << endl;
return;
}
for (int i = 0; i < 4; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if (nx >= 1 && nx <= n && ny >= 1 && ny <= m && a[nx][ny] == 1 && vis[nx][ny] == false) {
vis[nx][ny] = 1;
pre[nx][ny] = {x, y};
dfs(nx, ny);
vis[nx][ny] = 0;
}
}
}
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> a[i][j];
}
}
cin >> sx >> sy >> tx >> ty;
vis[sx][sy] = 1;
dfs(sx, sy);
if (ans == 0) cout << -1;
return 0;
}
/*
4
0001
0100
0010
0110
*/