2.3 上午 【广搜进阶1】
huangzhan170 · · 个人记录
P1746 离开中山路
思路:用bfs解决,bfs: 先存储地图,创造一个队列,队列包含x和y,再创标记数组vis,取出队首,在清除队尾,判断是否到达终点,是则输出,在退出循环,然后变历上下左右四个方向,进行递归。
#include<bits/stdc++.h>
using namespace std;
const int N = 1e3 + 7;
char c[N][N];
int dis[N][N];
bool vis[N][N];
int dx[] = {0, -1, 1, 0};
int dy[] = {1, 0, 0, -1};
int sx, sy;
int ex, ey;
int n;
struct Node
{
int x, y;
};
bool check(int x, int y)
{
return x >= 1 && x <= n && y >= 1 && y <= n && vis[x][y] == 0 && c[x][y] == '0';
}
void bfs()
{
queue<Node> q;
q.push({sx, sy});
vis[sx][sy] = 1;
while (q.size())
{
Node u = q.front();
q.pop();
if (u.x == ex && u.y == ey)
{
cout << dis[u.x][u.y];
return;
}
for (int i = 0; i < 4; i++)
{
int xx = u.x + dx[i];
int yy = u.y + dy[i];
if (check(xx, yy))
{
vis[xx][yy] = 1;
dis[xx][yy] = dis[u.x][u.y] + 1;
q.push({xx, yy});
}
}
}
}
int main()
{
cin >> n;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
cin >> c[i][j];
}
}
cin >> sx >> sy >> ex >> ey;
bfs();
return 0;
}
P1443 马的遍历
思路:也是用bfs,跟上题相似,遍历马的八个方向,且出始马的位置就是起点每往深一格,步数就等于上一步的步数加一其他不变即可。
#include<bits/stdc++.h>
using namespace std;
const int N = 1e3 + 7;
int dist[N][N];
bool vis[N][N];
int dx[] = {1,2,-1,-2,1,2,-1,-2};
int dy[] = {2,1,2,1,-2,-1,-2,-1};
int sx, sy;
int n,m;
int maxn=0;
bool check(int x, int y)
{
return x >= 1 && x <= n && y >= 1 && y <= m&& vis[x][y] != 1 ;
}
struct Node {
int x, y, cnt;
};
void bfs() {
queue<Node> q;
q.push({sx, sy, 0});
vis[sx][sy] = true;
dist[sx][sy] = 0;
while (!q.empty()) {
Node u = q.front();
q.pop();
int x = u.x, y = u.y, cnt = u.cnt;
for (int i = 0; i < 8; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if (check(nx, ny)) {
vis[nx][ny] = true;
dist[nx][ny] = cnt + 1;
q.push({nx, ny, cnt + 1});
}
}
}
}
int main()
{
cin>>n>>m>>sx>>sy;
bfs();
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
if((i==sx&&j==sy))
{
cout<<dist[i][j]<<" ";
continue;
}
else if(dist[i][j]==0)
{
cout<<-1<<" ";
continue;
}
cout<<dist[i][j]<<" ";
}
cout<<"\n";
}
return 0;
}
P1332 血色先锋队
思路:这类题是bfs多元点,数组要同时扩散,不同点是在结构体里面增加t,每次遍历在ans数组里增加1,最后输出ans数组即可;
#include <bits/stdc++.h>
using namespace std;
int n,m,a,b,gx,gy,lx,ly,num=0;
int x[550][550];
struct node
{
int x,y,t;
};
queue<node> q;
int dx[5]={0,0,1,-1};
int dy[5]={1,-1,0,0};
bool vis[550][550];
int ans[550][550] ;
bool check(int x,int y)
{
return 1<=x&&x<=n&&1<=y&&y<=m;
}
void bfs()
{
while(!q.empty())
{
node u=q.front();
q.pop();
for(int i=0;i<4;i++)
{
int x=u.x+dx[i];
int y=u.y+dy[i];
int t=u.t+1;
if(check(x,y)&&!vis[x][y])
{
vis[x][y]=1;
ans[x][y]=t;
q.push(node{x,y,t});
}
}
}
return ;
}
int main()
{
cin >> n >> m >> a >> b;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
x[i][j]=0;
}
}
for(int i=1;i<=a;i++)
{
cin >> gx >> gy ;
vis[gx][gy]=1;
q.push({gx,gy});
}
bfs();
for(int i=1;i<=b;i++)
{
cin >> lx>> ly;
cout<<ans[lx][ly]<<endl;
}
return 0;
}
P6207 [USACO06OCT] Cows on Skates G
思路:bfs,pre数组代表记录每一步的x,y,好让输出更方便,还要加write函数,函数内用递归的形式输出所有可能。
#include <bits/stdc++.h>
using namespace std;
const int maxn = 155;
char a[maxn][maxn];
struct Node
{
int x, y;
} pre[maxn][maxn];
queue<Node> q;
int dx[5] = {0, 0, -1, 1};
int dy[5] = {-1, 1, 0, 0};
int dis[maxn][maxn];
bool vis[maxn][maxn];
int n, m;
void write(int x, int y)
{
if (pre[x][y].x == -1 && pre[x][y].y == -1)
{
cout << x << " " << y << endl;
return;
}
write(pre[x][y].x, pre[x][y].y);
cout << x << " " << y << endl;
return;
}
bool check(int x, int y)
{
return (x >= 1 && x <= n && y >= 1 && y <= m && vis[x][y]==0 && a[x][y] !='*');
}
void bfs()
{
q.push({1, 1});
vis[1][1] = 1;
pre[1][1] = {-1, -1};
dis[1][1] = 0;
while (q.size())
{
Node u = q.front();
q.pop();
if (u.x == n && u.y == m)
{
write(n, m);
return;
}
for (int i = 0; i < 4; i++)
{
int xx = u.x + dx[i];
int yy = u.y + dy[i];
if (check(xx, yy))
{
q.push({xx, yy});
vis[xx][yy] = 1;
dis[xx][yy] = dis[u.x][u.y] + 1;
pre[xx][yy] = {u.x, u.y};
}
}
}
}
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;
}
P4328 [COCI 2006/2007 #1] Slikar
思路:用bfs,进行两次bfs,最后算出最短路径。
#include <bits/stdc++.h>
using namespace std;
int dx[4] = {0, -1, 1, 0};
int dy[4] = {-1, 0, 0, 1};
const int maxn = 1e3 + 7;
bool vis[maxn][maxn];
char c[maxn][maxn];
int sx, sy;
int ex, ey;
int dis1[maxn][maxn]; // 洪水到达时间
int dis2[maxn][maxn]; // 画家到达时间
int n, m;
struct Node {
int x, y;
};
// 洪水BFS的检查函数
bool check1(int x, int y) {
return x >= 1 && x <= n && y >= 1 && y <= m && !vis[x][y] && c[x][y] != 'X'
&& c[x][y] != 'D';
}
// 画家BFS的检查函数
bool check2(int x, int y, int t) {
if (x < 1 || x > n || y < 1 || y > m) return false;
if (vis[x][y]) return false;
if (c[x][y] == 'X') return false;
if (c[x][y] == 'D') return true; // 终点总是可以走
// 对于其他点,需要洪水到达时间 > t(严格大于)
return dis1[x][y] > t;
}
void bfs1() {
queue<Node> Q;
memset(vis, 0, sizeof(vis)); // 初始化vis数组
memset(dis1, 0x3f, sizeof(dis1));
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (c[i][j] == '*') {
vis[i][j] = 1;
Q.push({i, j});
dis1[i][j] = 0;
}
}
}
while (!Q.empty()) {
Node u = Q.front();
Q.pop();
for (int i = 0; i < 4; i++) {
int xx = u.x + dx[i];
int yy = u.y + dy[i];
if (check1(xx, yy)) {
vis[xx][yy] = 1;
Q.push({xx, yy});
dis1[xx][yy] = dis1[u.x][u.y] + 1;
}
}
}
}
void bfs2() {
queue<Node> q;
q.push({sx, sy});
memset(vis, 0, sizeof(vis));
memset(dis2, 0x3f, sizeof(dis2));
vis[sx][sy] = 1;
dis2[sx][sy] = 0;
while (!q.empty()) {
Node u = q.front();
q.pop();
// 修正:判断到达终点
if (u.x == ex && u.y == ey) {
cout << dis2[u.x][u.y];
return;
}
for (int i = 0; i < 4; i++) {
int xx = u.x + dx[i];
int yy = u.y + dy[i];
int nt = dis2[u.x][u.y] + 1; // 到达下一个点的时间
if (check2(xx, yy, nt)) {
vis[xx][yy] = 1;
q.push({xx, yy});
dis2[xx][yy] = nt;
}
}
}
cout << "KAKTUS";
}
int main()
{
cin >> n >> m;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> c[i][j];
if (c[i][j] == 'D') {
ex = i;
ey = j;
}
if (c[i][j] == 'S') {
sx = i;
sy = j;
}
}
}
bfs1();
bfs2();
return 0;
}