BFS经典题集(基础)

Lake Counting

#include <bits/stdc++.h>

#define x first
#define y second

using namespace std;

typedef pair<int,int> pii;

const int N = 1010;

char g[N][N];
int n, m, res;

void bfs(int sx,int sy)
{
    queue<pii> q;
    q.push({sx,sy});
    
    g[sx][sy] = '.';
    
    while(q.size())
    {
        pii t = q.front();
        q.pop();
        
        for(int i = -1; i <= 1; i ++)
        {
            for(int j = -1; j <= 1; j++)
            {
                if(i == 0 && j == 0) continue;
                
                int nx = t.x + i, ny = t.y + j;
                
                if(nx >= 0 && nx < n && ny >= 0 && ny < m)
                {
                    if(g[nx][ny] == 'W')
                    {
                        g[nx][ny] = '.';
                        q.push({nx,ny});
                    }
                }
            }
        }
    }
}

int main()
{
    cin >> n >> m;
    
    for(int i = 0; i < n; i++) cin >> g[i];
    
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < m; j++)
        {
            if(g[i][j] == 'W')
            {
                res ++;
                bfs(i,j);
            }
        }
    }
    
    cout << res << endl;
    
    return 0;
}

The Castle

/*
在标注四个方向时一定要注意本题中往下是y轴增加的方向,和我们平时用的数学中的坐标轴不同!!!
*/

#include <bits/stdc++.h>

#define x first
#define y second

using namespace std;

typedef pair<int,int> pii;

//西北东南 
const int dx[4] = {0,-1,0,1};
const int dy[4] = {-1,0,1,0};

const int N = 55;

int g[N][N];
bool st[N][N];
int n, m, area;

void bfs(int sx, int sy)
{
	area = 1;
	queue<pii> q;
	q.push({sx,sy});
	st[sx][sy] = true;
	
	while(q.size())
	{
		pii t = q.front();
		q.pop();
		
		for(int k = 0; k < 4; k++)
		{
			int nx = t.x + dx[k], ny = t.y + dy[k];
			
			if(nx >= 1 && nx <= n && ny >= 1 && ny <= m)
			{
				if(!st[nx][ny] && !(g[t.x][t.y] >> k & 1))
				{
					st[nx][ny] = true;
					area++;
					q.push({nx,ny});
				}
			}
		}
	}
}

int main()
{
	cin >> n >> m;
	
	for(int i = 1; i <= n; i++)
	{
		for(int j = 1; j <= m; j++)
		cin >> g[i][j];
	}
	
	int cnt = 0, max_area = 0;
	
	for(int i = 1; i <= n; i++)
	{
		for(int j = 1; j <= m; j++)
		{
			if(!st[i][j])
			{
				bfs(i, j);
				cnt ++;
				max_area = max(area, max_area);
			}
		}
	}
	
	cout << cnt << endl;
	cout << max_area << endl;
	
	return 0;
} 

山峰和山谷

/*
在判断是否是山峰或者山谷时不能用简单的if-else判断,因为可能会有即是山峰又是山谷的情况出现
*/

#include <bits/stdc++.h>

#define x first
#define y second

using namespace std;

typedef pair<int,int> pii;

const int N = 1010;

int g[N][N];
bool st[N][N];
int n;

void bfs(int sx,int sy, bool& has_lower, bool& has_higher)
{
	queue<pii> q;
	q.push({sx,sy});
	st[sx][sy] = true;
	
	while(q.size())
	{
		pii t = q.front();
		q.pop();
		
		for(int i = -1; i <= 1; i++)
		{
			for(int j = -1; j <= 1; j++)
			{
				if(i == 0 && j  == 0) continue;
				
				int nx = t.x + i, ny = t.y + j;
			
				if(nx >= 1 && nx <= n && ny >= 1 && ny <= n)
				{
					if(g[t.x][t.y] == g[nx][ny] && !st[nx][ny])
					{
						st[nx][ny] = true;
						q.push({nx,ny});
					}
					if(g[t.x][t.y] < g[nx][ny]) has_higher = true;
					if(g[t.x][t.y] > g[nx][ny]) has_lower = true;
				}
			}
		}
	}
}
int main()
{
	cin >> n;
	
	for(int i = 1; i <= n; i++)
	{
		for(int j = 1; j <= n; j++)
		cin >> g[i][j];
	}
	
	int peak = 0, valley = 0;
	
	for(int i = 1; i <= n; i++)
	{
		for(int j = 1; j <= n; j++)
		{
			if(!st[i][j])
			{
				bool has_lower = false, has_higher = false;
				bfs(i, j, has_lower, has_higher);
				if(!has_lower) valley ++;
				if(!has_higher) peak ++;
			}
		}
	}
	
	cout << peak << ' ' << valley << endl;
	
	return 0;
} 

迷宫问题

#include <bits/stdc++.h>

#define x first
#define y second

using namespace std;

typedef pair<int,int> pii;

const int dx[4] = {1,0,-1,0};
const int dy[4] = {0,-1,0,1};

const int N = 10;

int g[N][N];
pii pre[N][N];
bool st[N][N];

void bfs()
{
	queue<pii> q;
	q.push({0,0});
	st[0][0] = true;
	
	while(q.size())
	{
		pii t = q.front();
		q.pop();
		
		for(int k = 0; k < 4; k++)
		{
			int nx = t.x + dx[k], ny = t.y + dy[k];
			
			if(nx >= 0 && nx < 5 && ny >= 0 && ny < 5)
			{
				if(!st[nx][ny] && g[nx][ny] == 0)
				{
					pre[nx][ny] = {t.x, t.y};
					q.push({nx,ny});
					st[nx][ny] = true;
				}
			}
		}
	}
	
}

int main()
{
	for(int i = 0; i < 5; i++)
	{
		for(int j = 0; j < 5; j++)
		cin >> g[i][j];
	}
	
	bfs();
	
	vector<pii> path;
	pii t = {4,4};
	
	while(!(t.x == 0 && t.y == 0))
	{
		path.push_back(t);
		t = pre[t.x][t.y];
	}
	
	reverse(path.begin(),path.end());
	cout << "(0, 0)\n";
	for(auto t : path) cout << "(" << t.x << ", " << t.y << ")" << endl;
	
	return 0;
}

走迷宫

#include <bits/stdc++.h>

#define x first
#define y second

using namespace std;

typedef pair<int,int> pii;

const int dx[4] = {1,0,-1,0};
const int dy[4] = {0,-1,0,1};

const int N = 50;

char g[N][N];
bool st[N][N];
int d[N][N];
int n, m;

void bfs()
{
	queue<pii> q;
	q.push({0,0});
	st[0][0] = true;
	d[0][0] = 1;
	
	while(q.size())
	{
		pii t = q.front();
		q.pop();
		
		for(int k = 0; k < 4; k++)
		{
			int nx = t.x + dx[k], ny = t.y + dy[k];
			
			if(nx >= 0 && nx < n && ny >= 0 && ny < m)
			{
				if(!st[nx][ny] && g[nx][ny] == '.')
				{
					st[nx][ny] = true;
					d[nx][ny] = d[t.x][t.y] + 1;
					q.push({nx,ny});
				}
			}
		}
	}
}

int main()
{
	cin >> n >> m;
	
	for(int i = 0; i < n; i++) cin >> g[i];
	
	bfs();
	
	cout << d[n-1][m-1] << endl;

	return 0;
}

武士风度的牛

#include <bits/stdc++.h>

#define x first
#define y second

using namespace std;

typedef pair<int,int> pii;

const int dx[8] = {2,1,-1,-2,-2,-1,1,2};
const int dy[8] = {-1,-2,-2,-1,1,2,2,1};

const int N = 200;

char g[N][N];
bool st[N][N];
int d[N][N];
int n, m;
int sx, sy, gx, gy;

int bfs()
{
    queue<pii> q;
    q.push({sx,sy});
    st[sx][sy] = true;
    
    while(q.size())
    {
        pii t = q.front();
        q.pop();
        
        for(int k = 0; k < 8; k++)
        {
            int nx = t.x + dx[k], ny = t.y + dy[k];
            
            if(nx == gx && ny == gy) return d[t.x][t.y] + 1;
            
            if(nx >= 0 && nx < n && ny >= 0 && ny < m)
            {
                if(!st[nx][ny] && g[nx][ny] == '.')
                {
                    d[nx][ny] = d[t.x][t.y] + 1;
                    st[nx][ny] = true;
                    q.push({nx,ny});
                }
            }
        }
    }
    return -1;
}
int main()
{
    cin >> m >> n;
    
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < m; j++)
        {
            cin >> g[i][j];
            if(g[i][j] == 'K') sx = i, sy = j;
            if(g[i][j] == 'H') gx = i, gy = j;
        }
    }
    
    cout << bfs() << endl;

    return 0;
}

抓住那头牛

#include <bits/stdc++.h>
using namespace std;

const int N = 2e5 + 10;
int d[N];
bool st[N];
int s, g;

int bfs()
{
    queue<int> q;
    q.push(s);
    d[s] = 0;
    st[s] = true;
    
    while(q.size())
    {
        int t = q.front();
        q.pop();
        
        if(t == g) return d[t];
        
        if(t - 1 >= 0 && !st[t-1])
        {
            d[t-1] = d[t] + 1;
            st[t-1] = true;
            q.push(t - 1);
        }
        
        if(t + 1 <= g && !st[t + 1])
        {
            d[t + 1] = d[t] + 1;
            st[t + 1] = true;
            q.push(t + 1);
        }
        
        if(2 * t < N && !st[2 * t])
        {
            d[2 * t] = d[t] + 1;
            st[2 * t] = true;
            q.push(t * 2);
        }
    }
    return -1;
}

int main()
{
    cin >> s >> g;
    
    cout << bfs() << endl;
    
    return 0;
}
  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值