bfs1
#include<bits/stdc++.h>
using namespace std;
struct P{
int x, y, t;
};
int dx[] = {0, 0, -1, 1};
int dy[] = {-1, 1, 0, 0};
int n, dis[1005][1005], sx, sy, ex, ey;
char a[1005][1005];
queue<P>q;
void bfs(){
q.push({sx, sy, 0});
dis[sx][sy] = 1;
while(!q.empty()){
P f = q.front();
q.pop();
if(f.x == ex && f.y == ey){
cout << f.t;
return;
}
for(int i = 0; i < 4; i++){
int xx = f.x + dx[i], yy = f.y + dy[i];
if(xx < 1 || xx > n || yy < 1 || yy > n || dis[xx][yy] || a[xx][yy] == '1'){
continue;
}
dis[xx][yy] = 1;
q.push({xx, yy, f.t + 1});
}
}
}
int main(){
cin >> n;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
cin >> a[i][j];
}
}
cin >> sx >> sy >> ex >> ey;
bfs();
return 0;
} 思路:这是一道bfs的板子题。
bfs2
#include<bits/stdc++.h>
using namespace std;
struct P{
int x, y;
};
int dx[] = {-2, 2, -2, 2, -1, 1, -1, 1};
int dy[] = {-1, -1, 1, 1, -2, -2, 2, 2};
int n, m, dis[1005][1005], vis[1005][1005], sx, sy, ex, ey;
queue<P>q;
void bfs(){
q.push({sx, sy});
dis[sx][sy] = 1;
vis[sx][sy] = 0;
while(!q.empty()){
P f = q.front();
q.pop();
for(int i = 0; i < 8; i++){
int xx = f.x + dx[i], yy = f.y + dy[i];
if(xx < 1 || xx > n || yy < 1 || yy > m || dis[xx][yy]){
continue;
}
dis[xx][yy] = 1;
vis[xx][yy] = vis[f.x][f.y] + 1;//标记最少步数
q.push({xx, yy});
}
}
}
int main(){
cin >> n >> m;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
vis[i][j] = -1;//将初始值设为-1.
}
}
cin >> sx >> sy;
bfs();
for(int i = 1; i <= n; i++){//输出标记数组
for(int j = 1; j <= m; j++){
cout << vis[i][j] << " ";
}
cout << '\n';
}
return 0;
} 思路:这道题和上一道题差不多,只要将上一道题的方向数组改一下,再加一个标记步数的数组即可。
dfs3
#include<bits/stdc++.h>
using namespace std;
struct P{
int x, y;
};
int dx[] = {0, 0, -1, 1};
int dy[] = {-1, 1, 0, 0};
int n, m, dis[1005][1005], vis[1005][1005], a, b, x, y;
queue<P>q;
void bfs(){
while(!q.empty()){
P f = q.front();
q.pop();
for(int i = 0; i < 4; i++){
int xx = f.x + dx[i], yy = f.y + dy[i];
if(xx < 1 || xx > n || yy < 1 || yy > m || vis[xx][yy] != -1){//如果vis被标记过就说明这个点有最短距离了。
continue;
}
vis[xx][yy] = vis[f.x][f.y] + 1;
q.push({xx, yy});
}
}
}
int main(){
cin >> n >> m >> a >> b;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
vis[i][j] = -1;
}
}
for(int i = 1; i <= a; i++){
cin >> x >> y;
q.push({x, y});//将每个起点都放入队列中,然后一起广搜
vis[x][y] = 0;
}
bfs();
for(int i = 1; i <= b; i++){
cin >> x >> y;
cout << vis[x][y] << '\n';
}
return 0;
} 思路:这道题采用了多源点广搜。
dfs4
#include<bits/stdc++.h>
using namespace std;
struct P{
int x, y, t;
}dis[1005][1005];
int dx[] = {0, 0, -1, 1};
int dy[] = {-1, 1, 0, 0};
int n, m, sx, sy, ex, ey;
char a[1005][1005];
queue<P>q;
void COUT(int x, int y){//递归输出
if(x == 0 && y == 0){
return;
}
COUT(dis[x][y].x, dis[x][y].y);
cout << x << " " << y << '\n';
}
void bfs(){
q.push({1, 1, 0});
dis[1][1] = {0, 0, 1};
while(!q.empty()){
P f = q.front();
q.pop();
if(f.x == n && f.y == m){
COUT(n, m);
return;
}
for(int i = 0; i < 4; i++){
int xx = f.x + dx[i], yy = f.y + dy[i];
if(xx < 1 || xx > n || yy < 1 || yy > m || dis[xx][yy].t || a[xx][yy] == '*'){
continue;
}
dis[xx][yy] = {f.x, f.y, 1};//保存上一个的位置
q.push({xx, yy, f.t + 1});
}
}
}
int main(){
cin >> n >> m;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
cin >> a[i][j];
}
}
bfs();
return 0;
}
/*
5 8
..*...**
*.*.*.**
*...*...
*.*.*.*.
....*.*.
*/思路:这道题只要在bfs的板子上加一个记录和输出即可
dfs5
#include<bits/stdc++.h>
using namespace std;
struct P{
int x, y, t;
}dis[1005][1005];
int dx[] = {0, 0, -1, 1};
int dy[] = {-1, 1, 0, 0};
int n, m, sx, sy, ex, ey;
char a[1005][1005];
queue<P>q;
void COUT(int x, int y){
if(x == 0 && y == 0){
return;
}
COUT(dis[x][y].x, dis[x][y].y);
cout << x << " " << y << '\n';
}
void bfs(){
q.push({1, 1, 0});
dis[1][1] = {0, 0, 1};
while(!q.empty()){
P f = q.front();
q.pop();
if(f.x == n && f.y == m){
COUT(n, m);
return;
}
for(int i = 0; i < 4; i++){
int xx = f.x + dx[i], yy = f.y + dy[i];
if(xx < 1 || xx > n || yy < 1 || yy > m || dis[xx][yy].t || a[xx][yy] == '*'){
continue;
}
dis[xx][yy] = {f.x, f.y, 1};
q.push({xx, yy, f.t + 1});
}
}
}
int main(){
cin >> n >> m;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
cin >> a[i][j];
}
}
bfs();
return 0;
}
/*
5 8
..*...**
*.*.*.**
*...*...
*.*.*.*.
....*.*.
*/思路:这道题用了两次广搜,第一次多源点广搜,标记处洪水冲到所有点的时间,第二个则计算出是否能够到达终点