比赛信息
比赛名称:2.4 上午 【广搜进阶2】
时间:2026.2.4 上午
链接:2.4 上午 【广搜进阶2】
bfs模板
#include <bits/stdc++.h>
using namespace std;
struct node{
int x,y;
};
int xx,yy,sx,sy,n;
int dx[] = {0,-1,1,0};
int dy[] = {1,0,0,-1};
char c;
int vis[10000][10000];
int dis[10000][10000];
int a[10000][10000];
bool check(int x,int y){
return x >= 1 && x <= n && y <= n && y >= 1 && vis[x][y] == 0 && a[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 == xx && u.y == yy){
cout<<dis[u.x][u.y];
return;
}
for (int i = 0;i < 4;i++){
int xxx = u.x + dx[i];
int yyy = u.y + dy[i];
if (check(xxx,yyy)){
vis[xxx][yyy] = 1;
dis[xxx][yyy] = dis[u.x][u.y] + 1;
q.push({xxx,yyy});
}
}
}
}
int main(){
cin>>n;
for (int i = 1;i <= n;i++){
for (int j = 1;j <= n;j++){
cin>>c;
if (c == '0'){
a[i][j] = 0;
}
else{
a[i][j] = 1;
}
}
}
cin>>sx>>sy>>xx>>yy;
bfs();
return 0;
}题目
仙岛求药
题号:T729724
链接:仙岛求药
思路:需要注意是多组输入数据
代码:
#include <bits/stdc++.h>
using namespace std;
int m,n,minn,sx,sy,fx,fy;
int a[100][100];
int vis[100][100];
int dis[100][100];
int dx[] = {0,0,1,-1};
int dy[] = {1,-1,0,0};
char c;
struct node{
int x,y;
};
bool check(int x,int y){
return x <= m && y <= n && x >= 1 && y >= 1 && !vis[x][y] && !a[x][y];
}
void bfs(){
queue <node> q;
q.push({sx,sy});
vis[sx][sy] = 1;
dis[sx][sy] = 0;
while(q.size()){
node u = q.front();
q.pop();
if (u.x == fx && u.y == fy){
minn = min(dis[fx][fy],minn);
break;
}
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(){
while(1){
cin>>m>>n;
if (m == 0 && n == 0){
break;
}
for (int i = 1;i <= m;i++){
for (int j = 1;j <= n;j++){
cin>>c;
if (c == '@'){
a[i][j] = 0;
sx = i;
sy = j;
}
else if (c == '.'){
a[i][j] = 0;
}
else if (c == '#'){
a[i][j] = 1;
}
else{
a[i][j] = 0;
fx = i;
fy = j;
}
}
}
for (int i = 1;i <= 90;i++){
for (int j = 1;j <= 90;j++){
vis[i][j] = 0;
}
}
for (int i = 1;i <= 90;i++){
for (int j = 1;j <= 90;j++){
dis[i][j] = 0;
}
}
minn = 1e9;
bfs();
if (minn == 1e9){
cout<<-1;
}
else{
cout<<minn;
}
cout<<endl;
}
return 0;
}[USACO07DEC] Mud Puddles S
题号:P2873
链接:[USACO07DEC] Mud Puddles S
思路:输入变了一下而已
代码:
#include <bits/stdc++.h>
using namespace std;
int m,n,minn,sx,sy,fx,fy;
int a[10000][10000];
int vis[10000][10000];
int dis[10000][10000];
int dx[] = {0,0,1,-1};
int dy[] = {1,-1,0,0};
int d,c,k;
struct node{
int x,y;
};
bool check(int x,int y){
return x <= 500 && y <= 500 && x >= -500 && y >= -500 && !vis[x + 600][y + 600] && !a[x + 600][y + 600];
}
void bfs(){
queue <node> q;
q.push({sx,sy});
vis[sx + 600][sy + 600] = 1;
dis[sx + 600][sy + 600] = 0;
while(q.size()){
node u = q.front();
q.pop();
if (u.x == fx && u.y == fy){
minn = min(dis[fx + 600][fy + 600],minn);
break;
}
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 + 600][yy + 600] = 1;
dis[xx + 600][yy + 600] = dis[u.x + 600][u.y + 600] + 1;
q.push({xx,yy});
}
}
}
}
int main(){
sx = sy = 0;
cin>>fx>>fy>>k;
for (int i = 1;i <= k;i++){
cin>>c>>d;
a[c + 600][d + 600] = 1;
}
minn = 1e9;
bfs();
cout<<minn<<endl;
return 0;
}
走出迷宫的最短路径
题号:T729726
链接:走出迷宫的最短路径
思路:和昨天的第4题差不多只要改一改输入和输出格式就行
代码:
#include <bits/stdc++.h>
using namespace std;
struct node{
int x,y;
};
int xx,yy,sx,sy,n,m;
int dx[] = {0,-1,1,0};
int dy[] = {1,0,0,-1};
int c;
int vis[1000][1000];
int dis[1000][1000];
node pre[1000][1000];
int a[1000][1000];
bool check(int x,int y){
return x >= 1 && x <= n && y <= m && y >= 1 && vis[x][y] == 0 && a[x][y] == 0;
}
void write(int x,int y){
if (pre[x][y].x == -1 && pre[x][y].y == -1){
cout<<"("<<x<<","<<y<<")";
return;
}
write(pre[x][y].x,pre[x][y].y);
cout<<"->("<<x<<","<<y<<")";
}
void bfs(){
queue <node> q;
q.push({1,1});
vis[1][1] = 1;
dis[1][1] = 0;
pre[sx][sy] = {-1,-1};
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 xxx = u.x + dx[i];
int yyy = u.y + dy[i];
if (check(xxx,yyy)){
vis[xxx][yyy] = 1;
dis[xxx][yyy] = dis[u.x][u.y] + 1;
pre[xxx][yyy] = {u.x,u.y};
q.push({xxx,yyy});
}
}
}
cout<<"no way";
}
int main(){
cin>>n>>m;
for (int i = 1;i <= n;i++){
for (int j = 1;j <= m;j++){
cin>>c;
if (c == 0){
a[i][j] = 0;
}
else{
a[i][j] = 1;
}
}
}
cin>>sx>>sy>>xx>>yy;
bfs();
return 0;
}[[USACO09OCT] Invasion of the Milkweed G
题号:P2960
链接:[[USACO09OCT] Invasion of the Milkweed G](https://www.luogu.com.cn/problem/P2960 ?contestId=307572)
思路:输入上下颠倒了
代码:
#include <bits/stdc++.h>
using namespace std;
int m,n,maxn,sx,sy,fx,fy;
int a[1000][1000];
int vis[1000][1000];
int dis[1000][1000];
int dx[] = {0,0,1,-1,1,1,-1,-1};
int dy[] = {1,-1,0,0,1,-1,1,-1};
char c;
struct node{
int x,y;
};
bool check(int x,int y){
return x <= n && y <= m && x >= 1 && y >= 1 && !vis[x][y] && !a[x][y];
}
void bfs(){
queue <node> q;
q.push({sx,sy});
vis[sx][sy] = 1;
dis[sx][sy] = 0;
while(q.size()){
node u = q.front();
q.pop();
for (int i = 0;i < 8;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;
maxn=max(maxn, dis[xx][yy]);
q.push({xx,yy});
}
}
}
}
int main(){
cin>>m>>n>>sy>>sx;
for (int i = n;i >= 1;i--){
for (int j = 1;j <= m;j++){
cin>>c;
if (c == '.'){
a[i][j] = 0;
}
else{
a[i][j] = 1;
}
}
}
maxn = -1;
bfs();
cout<<maxn<<endl;
return 0;
}Nightmare II
题号:P10487
链接:Nightmare II
思路:
题目大意
小 erriyue(M)和他的女朋友(G)被困在迷宫中,同时有两个鬼魂(Z)在追捕他们:
每一秒,鬼魂会先分裂,占据曼哈顿距离不超过2的所有格子之后,小erriyue可以移动3步,女朋友只能移动1步
人不能进入有鬼魂的格子
问两人能否相遇,以及最短需要多少秒
解题思路
本题是带时间维度的BFS问题,关键在于处理:
两种角色不同的移动速度
鬼魂随时间扩散的特性
核心思想
分别BFS:对主角和女友分别进行BFS,记录到达每个位置的最短时间
鬼魂判定:对于时间t,如果某位置到任一鬼魂的曼哈顿距离≤2t,则该位置已被鬼魂占据
相遇点选择:枚举所有位置,找出两人均可达且max(主角时间, 女友时间)最小的位置
BFS细节
主角BFS:每秒处理3层BFS(每层代表1步)
女友BFS:每秒处理1层BFS
鬼魂影响:在扩展新位置前,检查该位置是否已被鬼魂占据
bool flag = true; for(int j = 0; j <= 1; j++) { if(abs(op[j].x - u.x) + abs(op[j].y - u.y) <= 2 * tim) { flag = false; // 被鬼魂占据 } }
最终答案
遍历所有位置,计算: minn = min(minn, max(dis[0][i][j], dis[1][i][j]));
表示两人在位置(i,j)相遇所需的最短时间。
复杂度分析
时间复杂度:O(T * n * m),T为测试用例数
空间复杂度:O(n * m),用于存储距离和访问状态
代码:
#include <bits/stdc++.h>
using namespace std;
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
char a[1005][1005];
int dis[2][1005][1005];
bool vis[1005][1005];
int sx1, sx2;
int sy1, sy2;
struct node {
int x, y;
};
vector<node> op;
int n, m;
void bfs(int sx, int sy, int k) {
memset(vis, 0, sizeof(vis));
dis[k][sx][sy] = 0;
vis[sx][sy] = true;
queue<node> q;
q.push({sx, sy});
int tim = 0;
while(q.size()) {
tim++; // 时间(秒数)
int cnt = 3; // 主角每秒走3步
if(k == 1) {
cnt = 1; //k=1是女友,每秒走1步
}
while(cnt--) {
int len = q.size(); // 当前队列中所有位置都是同一层的
// 把同一层的全部出队,相当于完成了走1步
while(len--) {
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];
bool flag = true; // 是否遇到鬼魂
for(int j = 0; j <= 1; j++) { // 2个鬼魂
// 鬼魂每秒走2步,且无视障碍物,所以可以直接用曼哈顿距离求出到此位置的距离
if(abs(op[j].x - u.x) + abs(op[j].y - u.y) <= 2 * tim) {
flag = false;
}
}
if(flag && xx >= 1 && xx <= n && yy >= 1 && yy <= m && !vis[xx][yy] && a[xx][yy] != 'X') {
dis[k][xx][yy] = tim;
vis[xx][yy] = true;
q.push({xx, yy});
}
}
}
}
}
}
void solve() {
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] == 'M') { // 主角
sx1 = i;
sy1 = j;
}
if(a[i][j] == 'G') { // 女友
sx2 = i;
sy2 = j;
}
if(a[i][j] == 'Z') { // 鬼魂
op.push_back({i, j});
}
}
}
memset(dis, -1, sizeof(dis));
bfs(sx1, sy1, 0); // 主角广搜,更新dis[0]数组
bfs(sx2, sy2, 1); // 女友广搜,更新dis[1]数组
int minn = 1e9;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
if(dis[0][i][j] == -1 || dis[1][i][j] == -1) { // 主角或女友到不了这个地方
continue;
}
minn = min(minn, max(dis[0][i][j], dis[1][i][j])); // 更新都能到的最短时间
}
}
if(minn == 1e9) {
cout << "-1\n";
}
else {
cout << minn << "\n";
}
op.clear();
}
int main() {
int t;
cin >> t;
while(t--) {
solve();
}
return 0;
}