要被气死了
| A | B | C | D | E | 总 | 排 |
|---|---|---|---|---|---|---|
| 我服了 | ||||||
A.T759360
思路非常简单:直接把刚开始就生病的人当做起点,跑一遍多起点广搜
请看错误代码⬇️
#include <bits/stdc++.h>
using namespace std ;
int n , cnt , f[105][105] , m ;
char c[105][105] ;
struct node
{
int x , y ;
} ;
int dx[5] = {1 , -1 , 0 , 0} , dy[5] = {0 , 0 , 1 , -1} ;
void bfs()
{
queue <node> q ;
for(int i = 1 ; i <= n ; i ++)
{
for(int j = 1 ; j <= n ; j ++)
{
if(c[i][j] == '@')
{
f[i][j] = 1 ;
q.push({i , j}) ;
}
}
}
while(q.size())
{
node tmp = q.front() ;
q.pop() ;
for(int i = 0 ; i < 4 ; i ++)
{
int nx = tmp.x + dx[i] , ny = tmp.y + dy[i] ;
if(nx < 1 || nx > n || ny < 1 || ny > n) continue ;
if(c[nx][ny] == '#') continue ;
if(f[nx][ny]) continue ;
f[nx][ny] = f[tmp.x][tmp.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 >> c[i][j] ;
}
}
cin >> m ;
bfs() ;
for(int i = 1 ; i <= n ; i ++)
{
for(int j = 1 ; j <= n ; j ++)
{
if(c[i][j] == '#') continue ;
if(f[i][j] <= m) cnt ++ ;
}
}
cout << cnt ;
return 0 ;
}没错!正是第56行把不会生病的也算进去了,所以正确代码是
#include <bits/stdc++.h>
using namespace std ;
int n , cnt , f[105][105] , m ;
char c[105][105] ;
struct node
{
int x , y ;
} ;
int dx[5] = {1 , -1 , 0 , 0} , dy[5] = {0 , 0 , 1 , -1} ;
void bfs()
{
queue <node> q ;
for(int i = 1 ; i <= n ; i ++)
{
for(int j = 1 ; j <= n ; j ++)
{
if(c[i][j] == '@')
{
f[i][j] = 1 ;
q.push({i , j}) ;
}
}
}
while(q.size())
{
node tmp = q.front() ;
q.pop() ;
for(int i = 0 ; i < 4 ; i ++)
{
int nx = tmp.x + dx[i] , ny = tmp.y + dy[i] ;
if(nx < 1 || nx > n || ny < 1 || ny > n) continue ;
if(c[nx][ny] == '#') continue ;
if(f[nx][ny]) continue ;
f[nx][ny] = f[tmp.x][tmp.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 >> c[i][j] ;
}
}
cin >> m ;
bfs() ;
for(int i = 1 ; i <= n ; i ++)
{
for(int j = 1 ; j <= n ; j ++)
{
if(c[i][j] == '#') continue ;
if(f[i][j] <= m && f[i][j] != 0) cnt ++ ;
}
}
cout << cnt ;
return 0 ;
}这就是当马大哈的下场
时间复杂度:
B.P4328
我们要跑两次BFS,一次关于洪水的,一次关于松鼠的
#include <bits/stdc++.h>
using namespace std ;
int n , cnt , f[105][105] , m , b[105][105] ;
char c[105][105] ;
struct node
{
int x , y ;
} ;
int dx[5] = {1 , -1 , 0 , 0} , dy[5] = {0 , 0 , 1 , -1} ;
void bfs()//洪水
{
queue <node> q ;
memset(b , -1 , sizeof f) ;
for(int i = 1 ; i <= n ; i ++)
{
for(int j = 1 ; j <= m ; j ++)
{
if(c[i][j] == '*')//洪水发源地
{
b[i][j] = 0 ;
q.push({i , j}) ;
}
}
}
while(q.size())
{
node tmp = q.front() ;
q.pop() ;
for(int i = 0 ; i < 4 ; i ++)
{
int nx = tmp.x + dx[i] , ny = tmp.y + dy[i] ;
if(nx < 1 || nx > n || ny < 1 || ny > m) continue ;//超边界
if(c[nx][ny] == 'X') continue ;//是石头
if(c[nx][ny] == 'D') continue ;//是避难所
if(b[nx][ny] != -1) continue ;//走过
b[nx][ny] = b[tmp.x][tmp.y] + 1 ;
q.push({nx , ny}) ;
}
}
}
void bfs2()
{
queue <node> q ;
memset(f , -1 , sizeof f) ;
for(int i = 1 ; i <= n ; i ++)
{
for(int j = 1 ; j <= m ; j ++)
{
if(c[i][j] == 'S')
{
f[i][j] = 0 ;
q.push({i , j}) ;
}
}
}
while(q.size())
{
node tmp = q.front() ;
q.pop() ;
if(c[tmp.x][tmp.y] == 'D')//到达避难所
{
cout << f[tmp.x][tmp.y] ;
return ;
}
for(int i = 0 ; i < 4 ; i ++)
{
int nx = tmp.x + dx[i] , ny = tmp.y + dy[i] ;
if(nx < 1 || nx > n || ny < 1 || ny > m) continue ;//超边界
if(c[nx][ny] == 'X') continue ;//是石头
if(f[nx][ny] != -1) continue ;//走过
if(f[tmp.x][tmp.y] + 1 >= b[nx][ny] && b[nx][ny] != -1) continue ;//会被淹
f[nx][ny] = f[tmp.x][tmp.y] + 1 ;
q.push({nx , ny}) ;
}
}
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] ;
}
}
bfs() ;
bfs2() ;
return 0 ;
}时间复杂度:
C.P2199
其实还跟普通广搜,只不过多了某个东西,代码来讲
#include <bits/stdc++.h>
using namespace std ;
#define N 16400
const int INF = 0x3f3f3f3f ;
const int dx[] = {0 , 0 , 1 , -1} ;//能走到的
const int dy[] = {1 , -1 , 0 , 0} ;
const int ddx[] = {0 , 0 , 1 , 1 , 1 , -1 , -1 , -1} ;//能看到的
const int ddy[] = {1 , -1 , -1 , 0 , 1 , -1 , 0 , 1} ;
int sx , sy , ex , ey ;
string s[N] ;
vector <int> dis[N] ;
struct node
{
int x , y ;
} ;
int n , m ;
queue <node> q ;
bool ok(int x , int y)//来判断
{
return x >= 0 && x < n && y >= 0 && y < m && s[x][y] == 'O' ;//没超边界+不是墙
}
void bfs(int sx , int sy)
{
for(int i = 0 ; i < n ; ++i)//初始化
{
for(int j = 0 ; j < m ; ++j)
{
dis[i][j] = INF ;
}
}
q.push(node{sx , sy}) ;
dis[sx][sy] = 0 ;
while(!q.empty())
{
int x = q.front().x ;
int y = q.front().y ;
q.pop() ;
for(int i = 0 ; i < 4 ; i ++)
{
int xx = x + dx[i] ;
int yy = y + dy[i] ;
if(ok(xx , yy) && dis[xx][yy] == INF)
{
dis[xx][yy] = dis[x][y] + 1 ;
q.push(node{xx , yy}) ;
}
}
}
}
int main()
{
cin >> n >> m ;
for(int i = 0 ; i < n ; ++i)
{
cin >> s[i] ;
dis[i].resize(m + 1) ;
}
while(cin >> ex >> ey >> sx >> sy)
{
if(!ex && !ey && !sx && !sy) break ;
ex -- ;
ey -- ;
sx -- ;
sy -- ;
bfs(sx , sy) ;
int ans = dis[ex][ey] ;//终点的移动部署
for(int i = 0 ; i < 8 ; i ++)//8个方向
{
int x = ex , y = ey ;//终点
while(ok(x + ddx[i] , y + ddy[i]))//沿着视线往回走
{
x += ddx[i] ;
y += ddy[i] ;
ans = min(ans , dis[x][y]) ;//是不是更优
}
}
if(ans == INF) cout << "Poor Harry\n" ;
else cout << ans << "\n" ;
}
return 0 ;
}时间复杂度:
D.P1649
这个是非常的复杂
核心逻辑:
- 把转弯次数当做距离,不是步数!
- 往相同方向走,次数不变
- 向左拐或者向右拐,次数
- 用BFS搜所有可能性,用数组记录每个点的最小步数
- 要跑四次BFS,因为方向可以不一样
只不过更好的方式是
#include <bits/stdc++.h>
using namespace std;
int n, sx, sy, ex, ey;
char a[105][105];
int vis[105][105];
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
struct node
{
int x, y;
int step;
int dx, dy;
};
void bfs(int sx, int sy, int dxi, int dyi)
{
queue <node> q;
vis[sx][sy] = 0;
q.push({sx, sy, 0, dxi, dyi});
while (q.size())
{
node t = q.front();
q.pop();
for (int i = 0; i < 4; i++)
{
int nx = t.x + dx[i];
int ny = t.y + dy[i];
int nstep = t.step; // 新的转弯次数,先默认不变
// 如果当前方向 != 上一步方向 → 转弯,次数+1
if (dx[i] != t.dx || dy[i] != t.dy) nstep++;
// 越界 / 障碍 → 跳过
if (nx < 1 || nx > n || ny < 1 || ny > n || a[nx][ny] == 'x') continue;
// 如果新的转弯次数更少,就更新,并入队继续搜
if (nstep <= vis[nx][ny])
{
vis[nx][ny] = nstep;
q.push({nx, ny, nstep, dx[i], dy[i]});
}
}
}
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;
}
}
memset(vis, 0x3f, sizeof vis);
// 关键:起点可以朝 4 个任意方向出发
// 所以要跑 4 次 BFS,保证所有情况都搜到
for (int i = 0; i < 4; i++) bfs(sx, sy, dx[i], dy[i]);
if (vis[ex][ey] == 0x3f3f3f3f) cout << -1;
else cout << vis[ex][ey];
return 0;
}时间复杂度:
E.P2845
第一次见到搜索都还有绿题的
这道题非常复杂,听我慢慢说
首先我们直接用普通BFS肯定是行不通了,因为我们这里的迷宫可以走回头路到另一个可以开灯的房间。有一些房间就走不到,就导致有些灯打不开,就导致答案……
所以我们可以想一个方法:我们把开了灯的如果旁边已经有可以走的我们直接跳在那里,我们就可以完美的避开啦~
#include <bits/stdc++.h>
using namespace std ;
int n , m , ax , ay , bx , by , cnt ;
struct node
{
int x , y ;
} ;
vector <node> v[105][105] ;
bool vis[105][105] , light[105][105] ;
int dx[4] = {1 , -1 , 0 , 0} , dy[4] = {0 , 0 , 1 , -1} ;
void bfs()
{
queue <node> q //初始化
q.push({1 , 1}) ;
vis[1][1] = light[1][1] = 1 ;//他走过并且亮着灯
while(!q.empty())
{
node tmp = q.front() ;
q.pop() ;
int x = tmp.x , y = tmp.y ;
for(auto now : v[x][y])//所有能开灯的
{
int nx = now.x , ny = now.y ;
if(light[nx][ny]) continue ;
light[nx][ny] = 1 ;//亮灯
for(int d = 0 ; d < 4 ; d ++)
{
int sx = nx + dx[d] ;
int sy = ny + dy[d] ;
if(sx < 1 || sx > n || sy < 1 || sy > n) continue ;
if(vis[sx][sy] && !vis[nx][ny])//旁边有可以到达的
{
vis[nx][ny] = 1 ;
q.push({nx , ny}) ;
}
}
}
for(int d = 0 ; d < 4 ; d ++)
{
int nx = x + dx[d] ;
int ny = y + dy[d] ;
if(nx < 1 || nx > n || ny < 1 || ny > n) continue ;
if(light[nx][ny] && !vis[nx][ny])//亮着灯但没到达且可以到达的
{
vis[nx][ny] = 1 ;
q.push({nx , ny}) ;
}
}
}
}
int main()
{
cin >> n >> m ;
for(int i = 1 ; i <= m ; i ++)
{
cin >> ax >> ay >> bx >> by ;
v[ax][ay].push_back({bx , by}) ;
}
bfs() ;
for(int i = 1 ; i <= n ; i ++)
{
for(int j = 1 ; j <= n ; j ++)
{
cnt += light[i][j] ;//亮着灯的
}
}
cout << cnt ;
return 0 ;
}时间复杂度:太小了
反省A题中……