跳到正文

个人记录

5.8

TchongS1010C约 16 分钟
原文

这是 2026年5月9日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2026年5月9日
收录
2026年5月9日
更新
2026年11月16日
篇幅
约 16 分钟

版本

  1. v2当前
  2. v1

要被气死了

ABCDE总排
1010我服了1001000010106612612699
1001001001000010106621621622

A.T759360

思路非常简单:直接把刚开始就生病的人当做起点,跑一遍多起点广搜

请看错误代码⬇️

C++
#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行把不会生病的也算进去了,所以正确代码是

C++
#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 ;
}

这就是当马大哈的下场

时间复杂度:O(n2)O(n^2)

B.P4328

我们要跑两次BFS,一次关于洪水的,一次关于松鼠的

C++
#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 ;
}

时间复杂度:O(nm)O(nm)

C.P2199

其实还跟普通广搜,只不过多了某个东西,代码来讲

C++
#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 ;
}

时间复杂度:O(tnm)O(tnm)

D.P1649

这个是非常的复杂

核心逻辑:

  1. 把转弯次数当做距离,不是步数!
  2. 往相同方向走,次数不变
  3. 向左拐或者向右拐,次数+1+1
  4. 用BFS搜所有可能性,用数组visi,jvis_{i,j}记录每个点的最小步数
  5. 要跑四次BFS,因为方向可以不一样

只不过更好的方式是visi,j,dvis_{i,j,d}

C++
#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;
}

时间复杂度:O(n2)O(n^2)