7月5日深度优先搜索1--路径总结

· · 个人记录

课堂练习

P1331 海战

题目背景

在峰会期间,武装部队得处于高度戒备。警察将监视每一条大街,军队将保卫建筑物,领空将布满了 F-2003 飞机。

此外,巡洋船只和舰队将被派去保护海岸线。不幸的是,因为种种原因,国防海军部仅有很少的几位军官能指挥大型海战。因此,他们培养了一些新海军指挥官。军官们选择了“海战”游戏来帮助他们学习。

题目描述

在一个方形的盘上,放置了固定数量和形状的船只,每只船却不能碰到其它的船。在本题中,我们认为船是方形的,所有的船只都是由图形组成的方形。

求出该棋盘上放置的船只的总数。

输入格式

第一行为两个整数 R 和 C,用空格隔开,分别表示游戏棋盘的行数和列数。

接下来 R 行,每行 C 个字符,为 # 或 .。# 表示船只的一部分,. 表示水。

输出格式

一行一个字符串,如果船的位置放得正确(即棋盘上只存在相互之间不能接触的方形,如果两个 # 号上下相邻或左右相邻却分属两艘不同的船只,则称这两艘船相互接触了)。就输出 There are S ships.,S 表示船只的数量。否则输出 Bad placement.。

错误代码

无

错误原因

无

正确思路

  1. 这道题目本质上就是连通块题目。
  2. 双重循环枚举每个位置的符号,如果是 # 号并且没被vis数组标记过, 就dfs把# 号属于的连通块都用vis数组标记,完成dfs后ans++。
  3. 注意!!!这个题目与其他连通块题目不一样的是:每个连通块必须都是矩形,否则输出 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 上学路线

题目描述

你所在城市的街道好像一个棋盘,有 a 条南北方向的街道和 b 条东西方向的街道。南北方向的 a 条街道从西到东依次编号为 1 到 a,而东西方向的 b 条街道从南到北依次编号为 1 到 b,南北方向的街道 i 和东西方向的街道 j 的交点记为 (i,j)。

你住在 (1,1) 处,而学校在 (a,b) 处,你骑自行车去上学,自行车只能沿着街道走,而且为了缩短时间只允许沿着向东和北的方向行驶。

现在有 N 个交叉路口在施工 (X_1,Y_1)、(X_2,Y_2)……,(X_n,Y_n),这些路口是不能通车的。

问你上学一共有多少走法?

输入格式

第一行包含两个整数 a 和 b,并且满足 1 \le a,b \le 16。

第二行包含一个整数 N,表示有 N 个路口在维修 (1\le N\le 40)。

接下来 N 行,每行两个整数X_i,Y_i,描述路口的位置。

输出格式

输出一个整数表示从 (1,1) 到 (a,b) 的行车路线总数。

错误代码

无

错误原因

无

正确思路

  1. dfs从(1,1)开始搜索如果到了终点(a,b),就ans++,回溯。
  2. 如果是障碍的话,也只需要特判一下。

正确代码

#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的迷宫,允许往上、下、左、右四个方向行走,现请你按照左、上、右、下顺序进行搜索,找出第一条从左上角到右下角的路径。

输入

输入数据有若干行,第一行有一个自然数 N(N ≤ 20),表示迷宫的大小,其后有N行数据,每行有 N 个 0 或 1(数字之间没有空格,0表示可以通过,1表示不能通过),用以描述迷宫地图。入口在左上角(1,1)处,出口在右下角(N,N)处。所有迷宫保证存在从入口到出口的可行路径。

输出

输出数据仅一行,为按照要求的搜索顺序找到的从入口到出口的第一条路径(搜索顺序:左、上、右、下)。

错误代码

无

错误原因

无

正确思路

  1. 用一个数组pri记录当前的这个位置是由哪个位置走来的。

  2. 如果到了总点就递归输出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 << ")";
    }
}
  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[] = {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 【基础】卒的遍历

题目描述

在一张n * m的棋盘上(如6行7列)的最左上角(1,1)的位置有一个卒。该卒只能向下或者向右走,请问从(1,1)点走到(n,m)点可以怎样走,输出这些走法。具体方案中,先往下走的步骤优先输出。

输入

两个整数n,m代表棋盘大小(3=<n<=8,3<=m<=8)

输出

卒的行走路线

错误代码

无

错误原因

无

正确思路

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 走迷宫

题目描述

有一个 m\times n 格的迷宫(表示有 m 行、n 列),其中有可走的也有不可走的,如果用 1 表示可以走,0 表示不可以走,文件读入这 m\times n 个数据和起始点、结束点(起始点和结束点都是用两个数据来描述的,分别表示这个点的行号和列号)。现在要你编程找出所有可行的道路,要求所走的路中没有重复的点,走时只能是上下左右四个方向。如果一条路都不可行,则输出相应信息(用 -1 表示无路)。

优先顺序:左上右下。

输入格式

第一行是两个数 m,n(1<m,n<15),接下来是 m 行 n 列由 1 和 0 组成的数据,最后两行是起始点和结束点。

输出格式

所有可行的路径,描述一个点时用 (x,y) 的形式,除开始点外,其他的都要用 -> 表示方向。

如果没有一条可行的路则输出 -1。

错误代码

无

错误原因

无

正确思路

  1. 于前两题几乎一样。只需要小小改一下就好了。

正确代码

#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
*/

哦!对了,我还有一份用vector写的代码!

```cpp vector<int> a; // 创建动态数组 a.push_back(x); // 插⼊ x a.pop_back(); // 删除末尾元素 a.size(); // 返回元素个数 sort(a.begin(), a.end()); // 动态数组排序 sort(a.begin(), a.end(), cmp); // 动态数组⾃定义排序(和以前学的⼀样) ``` #### 代码 ```cpp #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]; vector<pair<int,int> > path; 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) { vis[x][y] = 1; path.push_back({x, y}); // cout << x << " " << y << endl; if (x == tx && y == ty) { ans++; //[0,path.size()-1] cout << "(" << sx << "," << sy << ")"; for (int i = 1; i < path.size(); i++) { cout << "->(" << path[i].first << "," << path[i].second << ")"; } // print(x, y); cout << endl; // return; } else { 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) { pre[nx][ny] = {x, y}; dfs(nx, ny); } } } path.pop_back(); vis[x][y] = 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 */ ```