版本比较
1-25bfs总结
正文
增加 10 行删除 10 行
1234567891041 行未改动52535455565758596050 行未改动1111121131141151161171181191201211227 行未改动13013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416523 行未改动# P1443 马的遍历## 题意有一个 $n \times m$ 的棋盘,在某个点 $(x, y)$ 上有一个马,要求你计算出马到达棋盘上任意一个点最少要走几步,不能到则输出 $-1$。## 错因没有用数组记录导致超时,并且局部数组开太大还不清零。## Code```cpp#include<bits/stdc++.h>using namespace std;struct node{41 行未改动# P1332 血色先锋队## 题意给定一个 $n$ 行 $m$ 列的矩阵,初始有 $a$ 个感染源,每个感染源会每小时向上下左右四个方向扩散,求 $b$ 个特领主最早被感染的时间。## 错因这里面有多个起点,应该在开始前将所有的起点先插入进去。## Code```cpp#include<bits/stdc++.h>using namespace std;50 行未改动bfs();for(int i = 1;i <= b;i ++){cout << dis[sx[i][1]][sx[i][2]] << endl;}}```# T725010 [USACO06OCT] Cows on Skates G## 题意给定一个 $r \times c$ 的网格,`.` 表示可通过,`*` 表示障碍。起点为 $(1,1)$,终点为 $(r,c)$,保证有解。每次可向相邻四格移动。要求输出任意一条从起点到终点的路径坐标序列,每行一个坐标。## 错因没有,我瞎写的,我自己都不知道在写啥。## 解法7 行未改动};queue<node> q;char c[120][80];bool vis[120][80];node pre[120][80];int d[4][2] = {{0,1}, {1,0}, {-1,0}, {0,-1}};int r, n;void bfs() {node start;start.x = 1;start.y = 1;q.push(start);vis[1][1] = 1;pre[1][1] = {0, 0};while(q.size()) {node t = q.front();q.pop();if(t.x == r && t.y == n) {return;}for(int i = 0; i < 4; i++) {int tx = t.x + d[i][0];int ty = t.y + d[i][1];if(tx >= 1 && tx <= r && ty >= 1 && ty <= n &&c[tx][ty] == '.' && !vis[tx][ty]) {node a;a.x = tx;a.y = ty;q.push(a);vis[tx][ty] = 1;pre[tx][ty] = t;}}}}23 行未改动1234567891041 行未改动52535455565758596050 行未改动1111121131141151161171181191201211227 行未改动13013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416523 行未改动# P1443 马的遍历## 题意有一个 $n \times m$ 的棋盘,在某个点 $(x, y)$ 上有一个马,要求你计算出马到达棋盘上任意一个点最少要走几步,不能到则输出 $-1$。## 错因没有用数组记录导致超时(n*n*m*m了,有大概256亿次,爆了),并且局部数组开太大还不清零。## Code```cpp#include<bits/stdc++.h>using namespace std;struct node{41 行未改动# P1332 血色先锋队## 题意给定一个 $n$ 行 $m$ 列的矩阵,初始有 $a$ 个感染源,每个感染源会每小时向上下左右四个方向扩散,求 $b$ 个特领主最早被感染的时间。## 错因这里面有多个起点,应该在开始前将所有的起点先插入进去,可以把最开始存起点的部分封装成一个函数用多次。## Code```cpp#include<bits/stdc++.h>using namespace std;50 行未改动bfs();for(int i = 1;i <= b;i ++){cout << dis[sx[i][1]][sx[i][2]] << endl;}}```# T725010 [USACO06OCT] Cows on Skates G## 题意给定一个 $r \times c$ 的网格,`.` 表示可通过,`*` 表示障碍。起点为 $(1,1)$,终点为 $(r,c)$,保证有解。每次可向相邻四格移动。要求输出任意一条从起点到终点的路径坐标序列,每行一个坐标。## 错因没有,我瞎写的,我自己都不知道在写啥。## 解法7 行未改动};queue<node> q;char c[120][80];bool vis[120][80];node pre[120][80];int d[4][2] = {{0,1}, {1,0}, {-1,0}, {0,-1}};int r, n;void bfs() {node start;start.x = 1;start.y = 1;q.push(start);vis[1][1] = 1;pre[1][1] = {0, 0};while(q.size()) {node t = q.front();q.pop();if(t.x == r && t.y == n) {return;}for(int i = 0; i < 4; i++) {int tx = t.x + d[i][0];int ty = t.y + d[i][1];if(tx >= 1 && tx <= r && ty >= 1 && ty <= n &&c[tx][ty] == '.' && !vis[tx][ty]) {node a;a.x = tx;a.y = ty;q.push(a);vis[tx][ty] = 1;pre[tx][ty] = t;}}}}23 行未改动