7.9广度优先搜索进阶总结

· · 个人记录

课堂练习

T628045 迷宫问题

题目描述

定义一个二维数组:

int maze\[5\]\[5\] = {
0, 1, 0, 0, 0,
0, 1, 0, 1, 0,
0, 0, 0, 0, 0,
0, 1, 1, 1, 0,
0, 0, 0, 1, 0,
};

它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线。

输入格式

一个5 × 5的二维数组,表示一个迷宫。数据保证有唯一解。

输出格式

左上角到右下角的最短路径。

正确思路

  1. 用一个pre数组来记录每个位置是从哪来的,只需要插入一句pre[nx][ny] = {x, y};
  2. 如果到了终点,就递归输出pre数组,但要注意的是这个题目的下标是从0开始的

代码如下:

void print(int x, int y) {
    if (x == 1 && y == 1) {
        cout << "(0, 0)" << endl;
        return;
    }
    else {
        print(pre[x][y].x, pre[x][y].y);
        cout << "(" << x - 1 << ", " << y - 1 << ")" << endl;
    }
}

正确代码

#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 << "(0, 0)" << endl;
        return;
    }
    else {
        print(pre[x][y].x, pre[x][y].y);
        cout << "(" << x - 1 << ", " << y - 1 << ")" << endl;
    }
}
void dfs(int x, int y) {
    if (x == 5 && y == 5) print(x, y);
    for (int i = 0; i < 2; i++) {
        int nx = x + dx[i];
        int ny = y + dy[i];
        if (nx >= 1 && nx <= 5 && ny >= 1 && ny <= 5 && vis[nx][ny] == false && a[nx][ny] == '0') {
            pre[nx][ny] = {x, y};
            vis[nx][ny] = 1;
            dfs(nx, ny);
        }
    }
}
int main() {
    for (int i = 1; i <= 5; i++)
        for (int j = 1; j <= 5; j++) cin >> a[i][j];
    vis[1][1] = 1;
    dfs(1, 1);
    return 0;
} 

P1379 八数码难题【普及+/提高】

题目描述

在 3\times 3 的棋盘上,摆有八个棋子,每个棋子上标有 1 至 8 的某一数字。棋盘中留有一个空格,空格用 0 来表示。空格周围的棋子可以移到空格中。要求解的问题是:给出一种初始布局(初始状态)和目标布局(为了使题目简单,设目标状态为 123804765),找到一种最少步骤的移动方法,实现从初始布局到目标布局的转变。

输入格式

输入初始状态,一行九个数字,空格用 0 表示。

输出格式

只有一行,该行只有一个数字,表示从初始状态到目标状态需要的最少移动次数。保证测试数据中无特殊无法到达目标状态数据。

输入输出样例 #1

输入 #1

283104765

输出 #1

4

说明/提示

样例解释

图中标有 0 的是空格。绿色格子是空格所在位置,橙色格子是下一步可以移动到空格的位置。如图所示,用四步可以达到目标状态。

并且可以证明,不存在更优的策略。

正确思路

  1. 可以把3 * 3的二维数组转换成一个长度为9字符串
  2. 由于在3 * 3的棋盘里一个位置的上面那一个在字符串里就是这个位置的前三个,下面那一个在字符串里就是这个位置的后三个,左面那一个在字符串里就是这个位置的前一个,右面那一个在字符串里就是这个位置的后一个。因此方向数组的定义为int dx[] = {-3, 3, -1, 1};
  3. 注意!!!如果dx[i] == 1, 那原下标为2、5、8都无法达下一个。如果dx[i] == -1, 那原下标为3、6都无法达下一个。

所以bfs的伪代码如下:

string st, ed = "123804765";//起点和终点
struct node {
    string s;
    int x, step;//x:0所在的位置
};
map<string, bool> mp;
void bfs() {
    queue<node> q;
    //起点入队
    //标记起点
    while (q.size()) {
        //取队首, 队首出队
        //判断队首是不是ed
        //找字符串中'0'在哪里
        //四个方向
        for (int i = 0; i < 4; i++) {
            string ns = cur.s; int nx = xx + dx[i];
            if (dx[i] == 1 && (xx == 2 || xx == 5 || xx == 8)) continue;
            if (dx[i] == -1 && (xx == 0 || xx == 3 || xx == 6)) continue;
            if (nx < 0 || nx >= 9) continue;
            swap(ns[xx], ns[nx]);
            if(vis[ns] == true) continue;
            //标记,入队
        }
    }
}

正确代码

#include<bits/stdc++.h>
using namespace std;
const int N = 1007;
int dx[] = {-3, 3, -1, 1};
struct node {
    string s;
    int x, step;
};
string st, ed = "123804765";
queue<node> q;
map<string, bool> vis;
int bfs(string a) {
    node tmp = {a, 0, 0};
    vis[a] = true;
    q.push(tmp);
    while (!q.empty()) {
        node cur = q.front();
        q.pop();
        if (cur.s == ed) return cur.step;
        int xx; string ss = cur.s;
        for (int i = 0; i < 9; i++) if (ss[i] == '0') xx = i;
        for (int i = 0; i <= 3; i++) {
            string ns = cur.s; int nx = xx + dx[i];
            if (dx[i] == 1 && (xx == 2 || xx == 5 || xx == 8)) continue;
            if (dx[i] == -1 && (xx == 0 || xx == 3 || xx == 6)) continue;
            if (nx < 0 || nx >= 9) continue;
            swap(ns[xx], ns[nx]);
            if(vis[ns] == true) continue;
            node nxt = {ns, nx, cur.step + 1};
            vis[ns] = true;
            q.push(nxt);
        }
    }
    return 0;
}
int main() {
    cin >> st;
    cout << bfs(st);
    return 0;
}

P2730 [USACO3.2] 魔板 Magic Squares【普及+/提高】

题目背景

在成功地发明了魔方之后,鲁比克先生发明了它的二维版本,称作魔板。这是一张有 8 个大小相同的格子的魔板:

1\quad2\quad3\quad4 8\quad7\quad6\quad5

题目描述

我们知道魔板的每一个方格都有一种颜色。这 8 种颜色用前 8 个正整数来表示。可以用颜色的序列来表示一种魔板状态,规定从魔板的左上角开始,沿顺时针方向依次取出整数,构成一个颜色序列。对于上图的魔板状态,我们用序列 \{1,2,3,4,5,6,7,8\} 来表示。这是基本状态。

这里提供三种基本操作,分别用大写字母 \text A,\text B,\text C 来表示(可以通过这些操作改变魔板的状态):

$\text B$:将最右边的一列插入最左边; $\text C$:魔板中央四格作顺时针旋转。 下面是对基本状态进行操作的示范: $\text A$: $8\quad7\quad6\quad5 1\quad2\quad3\quad4 $4\quad1\quad2\quad3 5\quad8\quad7\quad6 $1\quad7\quad2\quad4 8\quad6\quad3\quad5

对于每种可能的状态,这三种基本操作都可以使用。

你要编程计算用最少的基本操作完成基本状态到目标状态的转换,输出基本操作序列。

输入格式

只有一行,包括 8 个整数 a_1,a_2\cdots a_8(1\leq a_1,a_2\cdots a_8\leq8),用空格分开,不换行,表示目标状态。

输出格式

第一行包括一个整数,表示最短操作序列的长度。

第二行在字典序中最早出现的操作序列,用字符串表示,除最后一行外,每行输出 60 个字符。

输入输出样例 #1

输入 #1

2 6 8 4 5 7 3 1

输出 #1

7 
BCABCCB

正确思路

  1. 注意!!!题目是以S形的路线给出的数字,假如给出的数字是2 6 8 4 5 7 3 1那么变成两行就是这样:第一行是2 6 8 4,第二行是1 3 7 5

  2. 注意!!!这个题目起点是固定的是"12345678",而题目给出的是终点,不然你就会和我一样卡好久

  3. 直接用swap模拟A、B、C三种操作

A操作代码如下:

swap(ns[0], ns[7]);
swap(ns[1], ns[6]);
swap(ns[2], ns[5]);
swap(ns[3], ns[4]);

B操作代码如下:

swap(ns[3], ns[0]);
swap(ns[3], ns[2]);
swap(ns[2], ns[1]);
swap(ns[7], ns[4]);
swap(ns[4], ns[5]);
swap(ns[5], ns[6]);

C操作代码如下:

swap(ns[1], ns[2]);
swap(ns[1], ns[6]);
swap(ns[6], ns[5]);

4.bfs伪代码如下:

int dx[] = {-3, 3, -1, 1};
struct node {
    string s, ans;
    int step;
};
int a[15];
string st = "12345678", ed = "";
queue<node> q;
map<string, bool> vis;
void bfs(string a) {
    //起点入队
    //标记起点
    while (!q.empty()) {
        //取队首, 队首出队
        //判断队首是不是ed
        //三种操作 
        for (int i = 0; i <= 2; i++) {
            string ns = cur.s;
            if (i == 0) {
                //操作A 
            }
            if (i == 1) {
                //操作B 
            }
            if (i == 2) {
                //操作C 
            }
            if (vis[ns] == false) {
                //更新ans字符串 
                //标记,入队 
            }
        }
    }
    cout << 0 << endl; 
    return ;
}

正确代码

#include<bits/stdc++.h>
using namespace std;
const int N = 1007;
int dx[] = {-3, 3, -1, 1};
struct node {
    string s, ans;
    int step;
};
int a[15];
string st = "12345678", ed = "";
queue<node> q;
map<string, bool> vis;
void bfs(string a) {
    node tmp = {a, "", 0};
    vis[a] = true;
    q.push(tmp);
    while (!q.empty()) {
        node cur = q.front();
        q.pop();
        if (cur.s == ed) {
            cout << cur.step << endl; cout << cur.ans << endl;
            return ;
        }
        for (int i = 0; i <= 2; i++) {
            string ns = cur.s;
            if (i == 0) {
                swap(ns[0], ns[7]);
                swap(ns[1], ns[6]);
                swap(ns[2], ns[5]);
                swap(ns[3], ns[4]);
            }
            if (i == 1) {
                swap(ns[3], ns[0]);
                swap(ns[3], ns[2]);
                swap(ns[2], ns[1]);
                swap(ns[7], ns[4]);
                swap(ns[4], ns[5]);
                swap(ns[5], ns[6]);
            }
            if (i == 2) {
                swap(ns[1], ns[2]);
                swap(ns[1], ns[6]);
                swap(ns[6], ns[5]);
            }
            if (vis[ns] == false) {
                string sum = cur.ans;
                if (i == 0) sum = sum + 'A';
                if (i == 1) sum = sum + 'B';
                if (i == 2) sum = sum + 'C';
                node nxt = {ns, sum, cur.step + 1};
                vis[ns] = true;
                q.push(nxt);
            }
        }
    }
    cout << 0 << endl; 
    return ;
}
int main() {
    for (int i = 1; i <= 8; i++) {char ch; cin >> ch; ed = ed + ch;}
    bfs(st);
    return 0;
}

T628732 矩阵距离【普及/提高−】

题目描述

给定一个 N 行 M 列的 01 矩阵 A,A[i][j] 与 A[k][l] 之间的曼哈顿距离定义为:

dist(A[i][j], A[k][l])=|i - k| + |j - l|

输出一个 N 行 M 列的整数矩阵 B,其中:

B[i][j] = min_{1 ≤ x ≤ N, 1 ≤ y ≤ M,A[x][y] = 1}⁡{dist(A[i][j], A[x][y])}

输入格式

第一行两个整数 N,M。

接下来一个 N 行 M 列的 01 矩阵,数字之间没有空格。

输出格式

一个 N 行 M 列的矩阵 B,相邻两个整数之间用一个空格隔开。

输入输出样例 #1

输入 #1

3 4
0001
0011
0110

输出 #1

3 2 1 0
2 1 0 0
1 0 0 1

说明/提示

1 \le N,M \le 1000

正确思路

  1. 在输入的时候判断一下如果是"1",就将这个位置入队,最后再搜索一次就好了
  2. 在搜索要边搜索边记录答案

正确代码

#include<bits/stdc++.h>
using namespace std;
const int N = 1007;
int n, m, xa, ya;
bool vis[N][N];
int ans[N][N];
char a[N][N];
int dx[] = {0, 0, -1, 1};
int dy[] = {1, -1, 0, 0};
struct node {
    int x, y, step;
};
queue<node> q;
void bfs(int sx, int sy) {
    node tmp = {sx, sy, 0};
    vis[sx][sy] = true;
    q.push(tmp);
    while (!q.empty()) {
        node cur = q.front();
        q.pop();
        ans[cur.x][cur.y] = min(ans[cur.x][cur.y], cur.step);
        for (int i = 0; i <= 3; i++) {
            int nx = cur.x + dx[i];
            int ny = cur.y + dy[i];
            if (nx >= 1 && nx <= n && ny >= 1 && ny <= m && vis[nx][ny] == false) {
                node nxt = {nx, ny, cur.step + 1};
                vis[nx][ny] = true;
                q.push(nxt);
            }
        }
    }
    return;
}
int main() {
    cin >> n >> m;
    memset(ans, 0x3f, sizeof (ans));
    for (int i = 1; i <= n; i++) 
        for (int j = 1; j <= m; j++) {
            cin >> a[i][j];
            if (a[i][j] == '1') {ans[i][j] = 0; q.push({i, j, 0});}
        }
    bfs(0, 0);
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) cout << ans[i][j] << " ";
        cout << endl;
    }       
    return 0;
}

P1825 [USACO11OPEN] Corn Maze S【普及/提高−】

题目描述

去年秋天,农夫约翰带着奶牛们参观了一个玉米迷宫。但这不是一个普通的玉米迷宫:它有几个重力驱动的传送滑梯,可以让奶牛瞬间从迷宫中的一个点传送到另一个点。滑梯是双向的:奶牛可以瞬间从滑梯的起点滑到终点,或者从终点滑到起点。如果奶牛踩到滑梯的任一端,她必须使用滑梯。

玉米迷宫的外部完全由玉米包围,只有一个出口。

迷宫可以用一个 N \times M(2 \leq N \leq 300;2 \leq M \leq 300)的网格表示。每个网格元素包含以下项目之一:

奶牛只能从一个空间移动到相邻的下一个空间,前提是它们相邻且都不包含玉米。每个草地空间有四个潜在的邻居可以让奶牛到达。从一个草地空间移动到相邻空间需要 1 个时间单位;从一个滑梯端点移动到另一个端点需要 0 个时间单位。

填满玉米的空间用井号(#)表示。草地空间用英文句号(.)表示。滑梯端点对用相同的大写字母(A 到 Z)表示,并且没有两个不同的滑梯端点用相同的字母表示。出口用等号(=)表示。

贝茜迷路了。她知道自己在网格中的位置,并用「at」符号(@)标记了她当前的草地空间。她需要的最短时间是多少才能移动到出口空间?

输入格式

第一行:两个用空格隔开的整数 N 和 M。

第 2 \sim N+1 行:第 i+1 行描述了迷宫中的第 i 行的情况(共有 M 个字符,每个字符之间没有空格)。

输出格式

一个整数,表示起点到出口所需的最短时间。

输入输出样例 #1

输入 #1

5 6
###=##
#.W.##
#.####
#.@W##
######

输出 #1

3

说明/提示

例如以下矩阵,N=5,M=6。

###=##
#.W.##
#.####
#.@W##
######

唯一的一个装置的结点用大写字母 \tt{W} 表示。

最优方案为:先向右走到装置的结点,花费一个单位时间,再到装置的另一个结点上,花费 0 个单位时间,然后再向右走一个,再向上走一个,到达出口处,总共花费了 3 个单位时间。

正确思路

  1. 由于这道题加入了一个新的元素:传送门。因此我们需要在广搜中多增加一个特判:当前是否为传送门,是的话,我们就需要找到另一个传送门所在坐标,然后将那个传送门所在点的坐标储存进队列
  2. 其他的比较简单,在这里不多说

正确代码

#include <bits/stdc++.h>
using namespace std;
const int N = 350;
struct node {
    int x, y, step;
};
queue<node> q;
char a[N][N];
bool vis[N][N];
int n,m;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int sx, sy;
void to(int &nx, int &ny) {
    for (int i = 1; i <= n; i++) 
        for (int j = 1; j <= m; j++)
            if (a[i][j] == a[nx][ny] && (i != nx || j != ny)) {
                nx = i; ny = j;
                return;
            }
}
int bfs(int sx, int sy) {
    q.push({sx, sy, 0});
    while (!q.empty()) {
        node cur = q.front();
        q.pop();
        if (a[cur.x][cur.y] == '=') return cur.step;
        if (a[cur.x][cur.y] >= 'A' && a[cur.x][cur.y] <= 'Z') to(cur.x,cur.y);
        for (int i = 0; i <= 3; i++) {
            int nx = cur.x + dx[i];
            int ny = cur.y + dy[i];
            if (nx >= 1 && nx <= n && ny >= 1 && ny <= m && a[nx][ny] != '#' && vis[nx][ny] == false) {
                vis[nx][ny] = true;
                q.push({nx, ny, cur.step + 1});
            }
        }
    }
    return -1;
}
int main() {
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++) {
            cin >> a[i][j];
            if (a[i][j] == '@') sx = i, sy = j;
        }
    cout << bfs(sx, sy);
    return 0;
}

家庭作业

都是一维bfs没什么好讲的

P1588 [USACO07OPEN] Catch That Cow S

题目描述

FJ 丢失了他的一头牛,他决定追回他的牛。已知 FJ 和牛在一条直线上,初始位置分别为 x 和 y,假定牛在原地不动。FJ 的行走方式很特别:他每一次可以前进一步、后退一步或者直接走到 2\times x 的位置。计算他至少需要几步追上他的牛。

输入格式

第一行为一个整数 t\ ( 1\le t\le 10),表示数据组数;

接下来每行包含一个两个正整数 x,y\ (0<x,y \le 10^5),分别表示 FJ 和牛的坐标。

输出格式

对于每组数据,输出最少步数。

输入输出样例 #1

输入 #1

1 
5 17

输出 #1

4

正确代码

#include <bits/stdc++.h>
using namespace std;
const int MAXN = 5e5 + 7;
int n, m, t, ans;
char a[1005][1005];
bool vis[MAXN];//标记数组
int dx[5] = {-1, 1, 0};//方向数组
struct node {
    int x, step;
};
int bfs(int sx) {
    queue<node> q;//创建队列
    q.push({sx, 0});//加入起点信息
    vis[sx] = true;//标记起点
    while (!q.empty()) {
        node cur = q.front();//取出队首
        q.pop();//弹出
        if (cur.x == m) return cur.step;//返回步数
        dx[2] = cur.x;//由于2x = x + x,所以可以把dx[2]设成cur.x
        for (int i = 0; i <= 2; i++) {
            int nx = cur.x + dx[i];
            if (nx >= 0 && nx <= MAXN && vis[nx] == false) {
                node nxt = {nx, cur.step + 1};
                q.push(nxt);
                vis[nx] = true;
            }
        }
    }
    return -1;
}
int main() {
    cin >> t;
    while (t--) {
        cin >> n >> m;
        memset(vis, 0, sizeof (vis));
        //多测不清空,爆0两行泪。注意!!!一定要清空!!!
        cout << bfs(n) << endl;
    }
    return 0;
} 

T582141 星际跃迁追踪协议

题目描述

在银河纪元2277年,侦察舰「追光者」需要回收一个失控的远古传送门核心。舰载AI测算出两者当前分别位于超空间坐标a(0≤a≤1e5)和b(0≤b≤1e5)处。舰体配备的曲速引擎支持以下跃迁模式:

  1. 量子滑移:从坐标 y 移动到 y-1或 y+1,消耗1个时空单元
  2. 超膜折叠:从坐标 y 直接折叠到2y,消耗1个时空单元

假设核心处于静默状态不会位移。请计算侦察舰达成空间重合所需的最小时空单元消耗量。

输入格式

第1行:两个整数a和b,表示初始坐标与目标坐标

输出格式

一个整数表示达成空间同步所需的最小时空单元数

输入输出样例 #1

输入 #1

1 8

输出 #1

3

输入输出样例 #2

输入 #2

10 23

输出 #2

3

输入输出样例 #3

输入 #3

9 80

输出 #3

4

正确代码

#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 7;
int n, m, ans;
char a[1005][1005];
bool vis[MAXN];//标记数组
int dx[5] = {-1, 1, 0};//方向数组
struct node {
    int x, step;
};
queue<node> q;//创建队列
int bfs(int sx) {
    q.push({sx, 0});//加入起点信息
    vis[sx] = true;//标记起点
    while (!q.empty()) {
        node cur = q.front();//取出队首
        q.pop();//弹出
        if (cur.x == m) return cur.step;//返回步数
        dx[2] = cur.x;//由于2x = x + x,所以可以把dx[2]设成cur.x
        for (int i = 0; i <= 2; i++) {
            int nx = cur.x + dx[i];
            if (nx >= 0 && nx <= MAXN && vis[nx] == false) {
                node nxt = {nx, cur.step + 1};
                q.push(nxt);
                vis[nx] = true;
            }
        }
    }
    return -1;
}
int main() {
    cin >> n >> m;
    cout << bfs(n) << endl;
    return 0;
} 

P1135 奇怪的电梯

题目描述

呵呵,有一天我做了一个梦,梦见了一种很奇怪的电梯。大楼的每一层楼都可以停电梯,而且第 i 层楼(1 \le i \le N)上有一个数字 K_i(0 \le K_i \le N)。电梯只有四个按钮:开,关,上,下。上下的层数等于当前楼层上的那个数字。当然,如果不能满足要求,相应的按钮就会失灵。例如: 3, 3, 1, 2, 5 代表了 K_i(K_1=3,K_2=3,……),从 1 楼开始。在 1 楼,按“上”可以到 4 楼,按“下”是不起作用的,因为没有 -2 楼。那么,从 A 楼到 B 楼至少要按几次按钮呢?

输入格式

共二行。

第一行为三个用空格隔开的正整数,表示 N, A, B(1 \le N \le 200,1 \le A, B \le N)。

第二行为 N 个用空格隔开的非负整数,表示 K_i。

输出格式

一行,即最少按键次数,若无法到达,则输出 -1。

输入输出样例 #1

输入 #1

5 1 5
3 3 1 2 5

输出 #1

3

说明/提示

对于 100 \% 的数据,1 \le N \le 200,1 \le A, B \le N,0 \le K_i \le N。

本题共 16 个测试点,前 15 个每个测试点 6 分,最后一个测试点 10 分。

正确代码

#include<bits/stdc++.h>
using namespace std;
const int N = 205;
int n, a, b;
int k[N];
bool vis[N];//标记数组
int dx[] = {-1, 1};//方向数组
//用于上下距离不是固定的,而是a[i],因此可以直接把dx[]初始值设为{-1, 1},
//上就是a[i] * 1, 下就是a[i] * -1
struct node {//创建结构体
    int x, step;
};
int bfs(int sa) {
    queue<node> q;//创建队列
    q.push({sa, 0});//加入起点信息
    vis[sa] = true;//标记起点
    while (q.empty() == false) {
        node cur = q.front();//取出队首
        q.pop();//弹出
        if (cur.x == b) return cur.step;//返回步数
        for (int i = 0; i <= 1; i++) {
            int nx = cur.x + dx[i] * k[cur.x];
            if (nx >= 1 && nx <= n && vis[nx] == false) {
                node nxt = {nx,cur.step + 1};
                vis[nx] = true;
                q.push(nxt);
            }
        }
    }
    return -1;//若无法到达,则输出-1
}
int main() {
    cin >> n >> a >> b;
    for (int i = 1; i <= n; i++) cin >> k[i];
    cout << bfs(a);
    return 0;
}

扩展

以前基本做过,在这不多讲,应该看一下就能懂了

T582134 01矩阵

题目描述

给定一个由 0 和 1 组成的矩阵 mp,请输出一个大小相同的矩阵,其中每一个格子是 mp 中对应位置元素到最近的 0 的距离。 两个相邻元素间的距离为 1 。

输入格式

一个由 0 和 1 组成的矩阵 mp。

输出格式

一个大小相同的矩阵,其中每一个格子是输入矩阵中对应位置元素到最近的 0 的距离。

输入输出样例 #1

输入 #1

3 3
0 0 0
0 1 0
1 1 1

输出 #1

0 0 0
0 1 0
1 2 1

说明/提示

数据范围:1 <= m, n <= 10^3

正确代码

#include<bits/stdc++.h>
using namespace std;
const int N = 1007;
int n, m, xa, ya;
bool vis[N][N];
int ans[N][N];
char a[N][N];
int dx[] = {0, 0, -1, 1};
int dy[] = {1, -1, 0, 0};
struct node {
    int x, y, step;
};
queue<node> q;
void bfs(int sx, int sy) {

    node tmp = {sx, sy, 0};
    vis[sx][sy] = true;
    q.push(tmp);
    while (!q.empty()) {
        node cur = q.front();
        q.pop();
        ans[cur.x][cur.y] = min(ans[cur.x][cur.y], cur.step);
        for (int i = 0; i <= 3; i++) {
            int nx = cur.x + dx[i];
            int ny = cur.y + dy[i];
            if (nx >= 1 && nx <= n && ny >= 1 && ny <= m && vis[nx][ny] == false) {
                node nxt = {nx, ny, cur.step + 1};
                vis[nx][ny] = true;
                q.push(nxt);
            }
        }
    }
    return;
}
int main() {
    cin >> n >> m;
    memset(ans, 0x3f, sizeof (ans));
    for (int i = 1; i <= n; i++) 
        for (int j = 1; j <= m; j++) {
            cin >> a[i][j];
            if (a[i][j] == '0') {ans[i][j] = 0; q.push({i, j, 0});}
        }
    bfs(xa, ya);

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) cout << ans[i][j] << " ";
        cout << endl;
    }       
    return 0;
}

T579123 黑吃黑

题目描述

给你一个由 H 个水平行和 W 个垂直列组成的方格,每个方格都涂成白色或黑色。从 A_{11} 到 A_{HW} 的 HW 个字符代表方格的颜色。如果从上往下第 i 行和从左往上第 j 列的方格是黑色的, A_{ij} 就是#;如果该方格是白色的, A_{ij} 就是.。

我们将重复执行下面的操作,直到所有的方格都是黑色为止:

求将要执行的操作次数。初始网格中至少有一个黑色方格。

输入格式

输入内容由标准输入法提供,格式如下:\

$A_{11}$ $A_{12}$ $...$ $A_{1W}$\ $:$\ $A_{H1}$ $A_{H2}$ $...$ $A_{HW}$\ ### 输出格式 输出最少步数。 ### 输入输出样例 #1 #### 输入 #1 ``` 3 3 ... .#. ... ``` #### 输出 #1 ``` 2 ``` ### 输入输出样例 #2 #### 输入 #2 ``` 6 6 ..#..# ...... #..#.. ...... .#.... ....#. ``` #### 输出 #2 ``` 3 ``` ### 说明/提示 #### 数据范围 - $1 \leq H,W \leq 1000

正确代码

#include <bits/stdc++.h>
using namespace std;
int n, m, ans = 0;
char a[1005][1005];
bool vis[1005][1005];
int dx[] = {1, 0, 0, -1};
int dy[] = {0, -1, 1, 0};
struct node {
    int x, y, step;
};
queue<node> q;
void bfs() {
    while (!q.empty()) {
        node cur = q.front();
        q.pop();
        for (int i = 0; i <= 3; i++) {
            int nx = cur.x + dx[i];
            int ny = cur.y + dy[i];
            if (nx >= 1 && nx <= n && ny >= 1 && ny <= m && vis[nx][ny] == false && a[nx][ny] != '#') {
                node nxt = {nx, ny, cur.step + 1};
                ans = max(ans, cur.step + 1);
                q.push(nxt);
                vis[nx][ny] = true;
            }
        }
    }
}
int main() {
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++) {
            cin >> a[i][j];
            if (a[i][j] == '#') {
                q.push({i, j, 0});
                vis[i][j] = true;
            }
        }
    bfs();
    cout << ans << endl;
    return 0;
} 

T582138 疯狂补能

题目描述

有一个具有 h 行和 w 列的网格。设 (i,j) 表示从上方第 i 行和从左侧第 j 列的单元格。每个单元格的状态由字符 A_{i,j}表示,其含义如下:

人杰老师可以通过消耗 1 点能量从当前单元格移动到垂直或水平相邻的空单元格。如果他的能量为 0,他就无法移动,也无法离开网格。

网格中有 N 个药品。第 i 个药品位于空单元格 (R_i, C_i),可以用来将能量变为 E_i。请注意,改变之后的能量不一定会增加。他可以在当前单元格使用药品。使用的药品将消失。也就是说,如果人杰老师所在的起点没有药品,就会直接移动不了。

人杰老师从起点出发,初始能量为 0,想要到达终点。确定是否可能实现这一目标。

输入格式

第一行输入2个整数h w
接下来输入h行 w 列的字符矩阵。

接下来输入一个整数 n
接下来 n 行,每行输入三个整数

R_i$ $C_i$ $E_i

输出格式

如果人杰老师能够从起点到达终点,则输出 Yes;否则,输出 No。

输入输出样例 #1

输入 #1

4 4
S...
#..#
#...
..#T
4
1 1 3
1 3 5
3 2 1
2 3 1

输出 #1

Yes

输入输出样例 #2

输入 #2

2 2
S.
T.
1
1 2 4

输出 #2

No

输入输出样例 #3

输入 #3

4 5
..#..
.S##.
.##T.
.....
3
3 1 5
1 2 3
2 2 1

输出 #3

Yes

说明/提示

正确代码

#include <bits/stdc++.h>
using namespace std;
int n, m;
int h;
int ssx, ssy;
char a[205][205];
int dis[205][205];
int b[205][205];
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};
struct node {
    int x, y, nl;
};
struct NODE {
    int r, c, e;
}x[305];
queue<node> q;
void bfs(int sx, int sy) {
    q.push({sx, sy, b[sx][sy]});
    dis[sx][sy] = b[sx][sy];
    while (!q.empty()) {
        node cur = q.front();
        q.pop();
        if(a[cur.x][cur.y] == 'T') {
            cout << "Yes"; return;
        }
        if (cur.nl == 0 || cur.nl < dis[cur.x][cur.y]) continue;
        for (int i = 0; i <= 3; i++) {
            int nx = cur.x + dx[i]; 
            int ny = cur.y + dy[i];
            if (nx >= 1 && nx <= n && ny >= 1 && ny <= m && a[nx][ny] != '#') {
                int v = 0;
                if (b[nx][ny] > 0) v = max(cur.nl - 1, b[nx][ny]);
                else v = cur.nl - 1;
                if (v > dis[nx][ny]) {
                    q.push({nx, ny, v}); dis[nx][ny] = v;
                }
            }
        }
    }
    cout << "No";
    return;
}
int main() {
    cin >> n >> m;
    memset(dis, -0x3f, sizeof(dis));
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++) {
            cin >> a[i][j];
            if (a[i][j] == 'S') ssx = i, ssy = j;
        }
    cin >> h;
    for (int i = 1; i <= h; i++) {
        cin >> x[i].r >> x[i].c >> x[i].e;
        b[x[i].r][x[i].c] = x[i].e;
    }
    bfs(ssx, ssy);
    return 0;
}