Codeforces Round #456 (Div. 2) D. Fishes(BFS)

题目链接:http://codeforces.com/contest/912/problem/D


题目是挺简单的。。。可是我怎么就没想到去bfs去搜呢???我大概是个zz吧


代码:

#include<bits/stdc++.h>
#define mp make_pair
#define xx first
#define yy second
using namespace std;
typedef long long ll;
int n,m,r,k;
int dir[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
ll cal(int i,int j)
{
	ll x=min(n-r+1,i)-max(1,i-r+1)+1;
    ll y=min(m-r+1,j)-max(1,j-r+1)+1;
	ll ret=x*y;
	return ret;
}
struct node
{
	int x,y;
	ll tot;
	node(int _x,int _y,ll _tot):x(_x),y(_y),tot(_tot){}
	bool operator < (const node &o)const
	{
		return tot<o.tot;
	}
};
priority_queue<node> Q;
map<pair<int,int>,int> book;
bool check(int x,int y)
{
	if(x<1||x>n||y<1||y>m||book.find(mp(x,y))!=book.end())
		return true;
	return false;
}
int main()
{
	//freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);
	ll res=0;
	scanf("%d%d%d%d",&n,&m,&r,&k);
	book[mp(r,r)]=1;
	Q.push(node(r,r,cal(r,r)));
	while(k--)
	{
		node now=Q.top();
		Q.pop();
		res+=now.tot;
		for(int i=0;i<4;i++)
		{
			int tx=now.x+dir[i][0];
			int ty=now.y+dir[i][1];
			if(!check(tx,ty))
			{
				book[mp(tx,ty)]=1;
				Q.push(node(tx,ty,cal(tx,ty)));
			}
		}
	}
	printf("%.10lf\n",1.0*res/(1LL*(n-r+1)*(m-r+1)));
	return 0;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值