5.8下午bfs进阶--东塘--蒋
Hyr_lu
·
2026-05-12 22:09:26
·
个人记录
流感传染
错误原因:
其实我的整体都是对的,在最后判感染房间的时候,没有判断是否为空房间,所以只有10分!!
题目大意:
给定一个n×n的网格,@代表初始感染者,.代表健康人,#代表不可通行的空房间;病毒每天向上下左右四个方向传播,求第 m 天结束时所有被感染的总人数。
思路:
这题就是个典型的多源 BFS 网格扩散题:先把第一天所有已经得病的人都找出来,一起扔进队列里,用 BFS 一层一层往外扩大,每个格子被感染的天数记在数组里,最后只要数一下,在第 m 天之内被感染的、不是空房间的格子,加起来输出即可。
100pts
#include<bits/stdc++.h>
using namespace std;
const int N=1e2+5;
int n,vis[N][N],ans,m;
int dx[5]={0,0,-1,1};
int dy[5]={-1,1,0,0};
char a[N][N];
struct st
{
int x,y;
};
void bfs()
{
queue<st>q;
memset(vis,-1,sizeof vis);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(a[i][j]=='@'&&vis[i][j]==-1)
{
vis[i][j]=1;
q.push({i,j});
}
}
}
while(q.size())
{
st now=q.front();
q.pop();
for(int i=0;i<4;i++)
{
int nx=now.x+dx[i],ny=now.y+dy[i];
if(vis[nx][ny]!=-1||nx<1||nx>n||ny<1||ny>n||a[nx][ny]=='#')continue;
vis[nx][ny]=vis[now.x][now.y]+1;
q.push({nx,ny});
}
}
}
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
cin>>a[i][j];
cin>>m;
bfs();
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(a[i][j]=='#')continue;
if(vis[i][j]<=m&&vis[i][j]!=-1)ans++;//错的就是这里,仅此一行
}
}
cout<<ans;
return 0;
}
P4328 [COCI 2006/2007 #1] Slikar
题目大意:
有一个画家和小刺猬要从 S 点跑到 D 点躲避洪水。洪水每分钟会向四周扩散一格,无法通过岩石,无法淹没巢穴,不能走到同一分钟会被淹没的地方。我们要算出他们安全到达巢穴的最短时间,如果会被烟丝 的话就输出 KAKTUS。
思路:
先用 BFS 算出来地图上每个空地被洪水淹没的时间,再用 BFS 让画家和小刺猬出发,每次走一步都要判断下一步不会被洪水淹没(不能是岩石),直到走到巢穴就返回总共用时,走不到就说明被烟丝 了。
100pts
#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> pii;
const int N = 55;
bool vis[N][N];
int n,m,sx,sy,ex,ey,ans;
char a[N][N];
struct node
{
int x,y,step;
};
queue<node>q;
int ti[N][N];
int dx[]={0,1,0,-1};
int dy[]={1,0,-1,0};
void bfs1()
{
while(q.size())
{
node now=q.front();
q.pop();
ti[now.x][now.y]=now.step;
for(int i=0;i<=3;i++)
{
int nx=now.x+dx[i],ny=now.y+dy[i];
if(nx<=0||nx>n||ny<=0||ny>m) continue;
if(vis[nx][ny]) continue;
if(a[nx][ny]=='X'||a[nx][ny]=='D') continue;
node tt={nx,ny,now.step+1};
q.push(tt);
vis[nx][ny]=1;
}
}
return ;
}
int bfs2()
{
while(!q.empty()) q.pop();
memset(vis,0,sizeof vis);
q.push({sx,sy,0});
vis[sx][sy]=1;
while(q.size())
{
node now=q.front();
q.pop();
if(now.x==ex&&now.y==ey) return now.step;
for(int i=0;i<=3;i++)
{
int nx=now.x+dx[i],ny=now.y+dy[i];
if(nx<=0||nx>n||ny<=0||ny>m) continue;
if(vis[nx][ny]) continue;
if(a[nx][ny]=='X') continue;
if(ti[nx][ny]<=now.step+1) continue;
node tt={nx,ny,now.step+1};
q.push(tt);
vis[nx][ny]=1;
}
}
return -1;
}
int main()
{
cin>>n>>m;
memset(ti,0x3f,sizeof ti);
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});
ti[i][j]=0;
}
if(a[i][j]=='S') sx=i, sy=j;
if(a[i][j]=='D') ex=i, ey=j;
}
bfs1();
ans=bfs2();
if(ans==-1) cout<<"KAKTUS";
else cout<<ans;
return 0;
}
P2199 最后的迷宫
题目大意:
有一个 n 行 m 列的迷宫,迷宫里有空地、墙和终点奖杯,哈利从指定起点出发,只能上下左右走,哈利只要走到能直接看到奖杯(视线中间没有墙挡住)的空地就算成功,求哈利走到那里的最少步数,如果走不到就输出 Poor Harry。
思路:
我们先把所有能直接看到奖杯的空地都标记好,沿着奖杯的八个方向一直延伸,碰到墙就停下,这些位置都是能看到奖杯的安全点,然后用队列带着哈利从起点出发,一步一步上下左右探索没走过的空地,每走一步记好步数,只要走到标记好的点,就直接输出当前步数,如果把能走的路都走完了还没到安全点,就说明哈利拿不到奖杯——GAME OVER。
100pts
#include <bits/stdc++.h>
using namespace std;
struct P {
int x, y, st;
};
int n, m;
int sx, sy, ex, ey;
vector<vector<char>> mp;
vector<vector<int>> q, vis;
queue<P> qq;
int d[8][2] = {
{1, 0}, {0, 1}, {-1, 0}, {0, -1}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}
};
void solve() {
vector<vector<int>> tmp(n+5, vector<int> (m+5, 0));
q = tmp;
vis = tmp;
while (qq.size()) qq.pop();
q[ex][ey] = 1;
for (int i=0; i<8; i++) {
int nx = ex+d[i][0], ny = ey+d[i][1];
while (nx >= 1 and nx <= n and ny >= 1 and ny <= m and mp[nx][ny] == 'O') {
q[nx][ny] = 1;
nx += d[i][0];
ny += d[i][1];
}
}
qq.push({sx, sy, 0});
vis[sx][sy] = 1;
while (qq.size()) {
P now = qq.front();
qq.pop();
if (q[now.x][now.y] == 1) {
cout << now.st << '\n';
return ;
}
for (int i=0; i<4; i++) {
int nx = now.x+d[i][0], ny = now.y+d[i][1];
if (nx < 1 or nx > n or ny < 1 or ny > m) continue;
if (vis[nx][ny] or mp[nx][ny] == 'X') continue;
qq.push({nx, ny, now.st+1});
vis[nx][ny] = 1;
}
}
cout << "Poor Harry\n";
}
signed main() {
cin >> n >> m;
mp.resize(n+5, vector<char>(m+5));
for (int i=1; i<=n; i++)
for (int j=1; j<=m; j++)
cin >> mp[i][j];
while (cin >> ex >> ey >> sx >> sy) {
if (sx == 0) break;
solve();
}
return 0;
}
P1649 [USACO07OCT] Obstacle Course S
题目大意:
在一个 N×N 的方格场地里,有奶牛起点 A 和终点 B,场地里有些格子是障碍物无法通行,奶牛只能沿着上下左右四个方向走,每次转 90 度就算一次转弯,求从 A 到 B 最少要转几次弯,如果走不通就输出 - 1。
思路:
我们把 “当前位置、前进方向、已经转了弯的次数” 作为状态,从起点出发,先向四个方向尝试一直往前走,直到碰到边界或障碍物,把路上每个格子都标记以下,再尝试左转和右转,每转一次弯转弯次数就加 1,把新的方向和下一步能走到的格子加入队列继续搜索,直接输出第一次到达终点时记录的最小转弯次数就行,但如果搜索完都没到终点就输出 - 1即可。
100pts
#include<bits/stdc++.h>
using namespace std;
const int N=105;
bool vis[N][N][4][N*10];
int n,sx,sy,ex,ey,ans=1e9;
char a[N][N];
struct node
{
int x,y,fx,step;
};
queue<node> q;
int dx[]={0,1,0,-1};
int dy[]={1,0,-1,0};
void bfs()
{
for(int i=0;i<4;i++)
{
q.push({sx,sy,i,0});
vis[sx][sy][i][0]=1;
}
while(q.size())
{
node hh=q.front();
q.pop();
if(hh.x==ex&&hh.y==ey)
{
ans=min(ans,hh.step);
continue ;
}
int nx=hh.x, ny=hh.y;
while(1)
{
nx+=dx[hh.fx], ny+=dy[hh.fx];
if(nx<=0||nx>n||ny<=0||ny>n) break;
if(a[nx][ny]=='x') break;
if(vis[nx][ny][hh.fx][hh.step]) break;
vis[nx][ny][hh.fx][hh.step]=1;
q.push({nx,ny,hh.fx,hh.step});
}
for(int i=1;i<=2;i++)
{
int fx;
if(i==1) fx=(hh.fx+1)%4;
else fx=(hh.fx-1+4)%4;
nx=hh.x+dx[fx],ny=hh.y+dy[fx];
if(nx<=0||nx>n||ny<=0||ny>n) continue;
if(vis[nx][ny][fx][hh.step+1]) continue;
if(a[nx][ny]=='x') continue;
vis[nx][ny][fx][hh.step+1]=1;
node tt={nx,ny,fx,hh.step+1};
q.push(tt);
}
}
return;
}
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
cin>>a[i][j];
if(a[i][j]=='A')sx=i,sy=j;
if(a[i][j]=='B')ex=i,ey=j;
}
bfs();
if(ans==1e9) cout<<"-1";
else cout<<ans;
return 0;
}
P2845 [USACO15DEC] Switching on the Lights S
题目大意:
Bessie 一开始在 (1,1),只有这里灯是亮的,她只能在灯亮的地方走,每个亮灯的房间能打开其他房间的灯。问题是求最多能点亮多少个房间的灯。
思路:
难点在于,点亮新房间后,它周围可能已经有亮灯的房间了,但如果按普通 BFS 一步步走,来回绕路会很慢,甚至超时。所以我们换个方法:只要新点亮的房间旁边有已经亮灯的地方,就说明它肯定能走到,直接把它标记为可到达并加入队列(相当于直接飞过去,因为题目没有说走一步会有什么代价,所以可以这样),这样就能节省了时间。
100pts
#include<bits/stdc++.h>
using namespace std;
const int N=105;
bool a[N][N],b[N][N];
struct st{
int x,y;
};
int n,m,ans;
queue<st> q;
vector<st> c[N][N];
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
void bfs(){
q.push({1,1});
a[1][1]=b[1][1]=1;
while(q.size()){
st now=q.front();
q.pop();
for(auto tmp:c[now.x][now.y])
{
int nxtx=tmp.x,nxty=tmp.y;
if(a[nxtx][nxty]==0)
{
b[nxtx][nxty]=1;
for(int i=0;i<4;i++)
{
int nx=nxtx+dx[i],ny=nxty+dy[i];
if(nx<1||nx>n||ny<1||ny>n) continue;
if(a[nx][ny]==1)
{
a[nxtx][nxty]=1;
q.push({nxtx,nxty});
break;
}
}
}
}
for(int i=0;i<4;i++)
{
int nx=now.x+dx[i],ny=now.y+dy[i];
if(nx<1||nx>n||ny<1||ny>n) continue;
if(a[nx][ny]) continue;
if(!b[nx][ny]) continue;
a[nx][ny]=1;
q.push(st{nx,ny});
}
}
}
int main(){
cin>>n>>m;
while(m--)
{
int a1,b1,a2,b2;
cin>>a1>>b1>>a2>>b2;
c[a1][b1].push_back({a2,b2});
}
bfs();
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
if(b[i][j])
ans++;
cout<<ans;
return 0;
}