BAPC 2018 Preliminaries B dfs、bfs

题目描述

You are a busy little bee, and you have a problem. After collecting nectar all day long, you are returning to the beehive with a large supply of honey. You would really like to take a nap now, but sadly, you have to store all the honey in your beehive first. Opening up a cell in the hive to funnel honey into takes a lot of time, so you want to avoid doing this as much as possible.
Some of the cells in the beehive are already filled with old, hardened honey. The other cells are still empty. If you pour honey into an empty cell, it will automatically start flowing into adjacent empty cells. From these cells, the honey will again flow to other neighbouring empty cells. This saves you from having to funnel honey into them directly. You decide to use this to your advantage, by pouring into cells with lots of (indirect) adjacent open cells.

figure 1: The beehives from the first two samples. The black hexagons already contain hardened honey. The white cells are still empty.
You have some units of honey, and know the layout of your beehive. By cleverly choosing which cells to funnel honey into, what is the minimal amount of work you have to do?

输入

• The input starts with three integers, 0 ≤ h ≤ 106, the amount of honey you have, and 1 ≤ n, m ≤ 103, the dimensions of the grid.
• Then follow n lines, one for each row of the grid. Each row has m symbols, either .,representing an empty cell, or #, representing a filled cell. These symbols are separated by spaces. Furthermore, every second row starts with a space, as these are slightly offset to the right.
The grid always has enough open cells to store all your honey.

输出

Output a single integer, the number of cells you have to funnel honey into directly to store all your honey in the hive.

样例输入

复制样例数据

8 4 4
. # # .
 # . # .
# # # .
 # . . .

样例输出

3

bfs

来自队友

#include<bits/stdc++.h>
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
typedef long long ll;
const double pi=acos(-1);
const double eps = 1e-8;
const int inf = 0x3f3f3f3f;
const int maxn = 1007;//1e5+7
const ll mod = 1000000007;//1e9+7

struct node{
	int x,y;
};
int n,m,h;
int tmp;
char mp[maxn][maxn];
int vis[maxn][maxn];
int blo[1000007];
int vec[4][2] = {{0,1},{0,-1},{1,0},{-1,0}};
int vc[4][2] = {{-1,1},{1,1},{-1,-1},{1,-1}};

queue<node>que;

bool check(int x,int y)
{
	if(x<1||y<1||x>n||y>m||vis[x][y]){
		return false;
	}
	return true;
}

void bfs(int x,int y)
{
	node zz;
	zz.x=x;
	zz.y=y;
	que.push(zz);
	while(!que.empty()){
		node fu = que.front();
		que.pop();
		if(!vis[fu.x][fu.y] && mp[fu.x][fu.y] != '#'){
			tmp++;
			vis[fu.x][fu.y] = 1;
			for(int i = 0 ;i<4;i++){
				node ez;
				ez.x = fu.x+vec[i][0];
				ez.y = fu.y+vec[i][1];
				if(check(ez.x,ez.y)){
					que.push(ez);
				}
			}
			if(fu.x&1){
				for(int i = 2;i<4;i++){
					node ez;
					ez.x = fu.x+vc[i][0];
					ez.y = fu.y+vc[i][1];
					if(check(ez.x,ez.y)){
						que.push(ez);
					}
				}
			}
			else{
				for(int i = 0;i<2;i++){
					node ez;
					ez.x = fu.x+vc[i][0];
					ez.y = fu.y+vc[i][1];
					if(check(ez.x,ez.y)){
						que.push(ez);
					}
				}
			}
		}
	}
}
bool cmp(int q,int p)
{
    return q>p;
}
int main()
{
	ios;
	cin>>h>>n>>m;
	for(int i = 1;i<=n;i++){
		for(int j = 1;j<=m;j++){
			cin>>mp[i][j];
		}
	}
	int cnt = 0;
	for(int i = 1;i<=n;i++){
		for(int j = 1;j<=m;j++){
			if(mp[i][j] != '#' && !vis[i][j]){
				tmp = 0;
				bfs(i,j);
				blo[++cnt] = tmp;
			}
		}
	}
	sort(blo+1,blo+cnt+1,cmp);
	int ans = 0,d=0;
	for(int i = 1;i<=cnt;i++){
		if(d>=h)break;
		else
        {
            ans++;
            d+=blo[i];
        }
	}
	cout<<ans<<endl;
	return 0;
}

dfs

#include<bits/stdc++.h>
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
typedef long long ll;
const double pi=acos(-1);
const double eps = 1e-8;
const int inf = 0x3f3f3f3f;
const int maxn = 1007;//1e5+7
const ll mod = 1000000007;//1e9+7

int n,m,h;
int tmp;
char mp[maxn][maxn];
int blo[1000007];
int r[6][2] = {{0,1},{0,-1},{1,0},{-1,0},{-1,1},{1,1}};
int l[6][2] = {{0,1},{0,-1},{1,0},{-1,0},{-1,-1},{1,-1}};
bool cmp(int q,int p)
{
    return q>p;
}
bool check(int x,int y)
{
	if(x>=1&&x<=n&&y>=1&&y<=m&&mp[x][y]=='.')return true;
	else return false;
}

void dfs(int x,int y)
{
	tmp++;
	int xx,yy;
    mp[x][y]='#';
    for(int i=0;i<6;i++)
    {
        if(x&1)
        {
            xx=x+l[i][0];
            yy=y+l[i][1];
            if(check(xx,yy))dfs(xx,yy);
        }
        else
        {
            xx=x+r[i][0];
            yy=y+r[i][1];
            if(check(xx,yy))dfs(xx,yy);
        }
    }
    return ;
}

int main()
{
	ios;
	cin>>h>>n>>m;
	for(int i = 1;i<=n;i++){
		for(int j = 1;j<=m;j++){
			cin>>mp[i][j];
		}
	}
	int cnt = 0;
	for(int i = 1;i<=n;i++){
		for(int j = 1;j<=m;j++){
			if(mp[i][j] == '.'){
				tmp = 0;
				dfs(i,j);
				blo[++cnt] = tmp;
			}
		}
	}
	sort(blo+1,blo+cnt+1,cmp);
	int ans = 0,d=0;
	for(int i = 1;i<=cnt;i++){
		if(d>=h)break;
		else
        {
            ans++;
            d+=blo[i];
        }
	}
	cout<<ans<<endl;
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值