您的位置:首页 > 其它

hdu5725 Game 2016年多校C

2016-07-25 11:02 246 查看
题意:给你一个n*m的棋盘,棋盘上有一些守卫,守卫之间不能相互攻击(守卫的攻击范围为同行,同列与 相邻的其他8个方格)。求从棋盘上任取两方格(可以为同一个)问最短距离的期望。

思路: 我们可以先求出 忽略守卫占位的影响总的最短距离,然后再加上守卫占位使得最短距离+2的条数*2(考虑方向性,不同方格队的数量*4).



根据上图我们可以看出对于每一行的右边部分(1区域)的点到2区域的点的距离会增加2。

我们在考虑从这个棋盘的上,下,左,右四个四个方向分别对他进行考虑就统计出了最短距离增加2的条数(记得*2,路径的方向不同);

代码如下:

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<queue>
#include<vector>
#include<iostream>
#include<string>
#include<set>
#include<map>
#include<algorithm>
#include<complex>
using namespace std;
#pragma comment(linker, "/STACK:1024000000,1024000000")
#define nn 1010
#define maxk 18
#define LL long long
#define ULL unsiged long long
#define mod 1000000007
#define inf 0xffffff
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
char mp[1010][1010];
LL xx[nn],yy[nn],chex[nn],chey[nn];
LL ans;

void get_sum(LL n,LL x[])
{
for(LL i=1;i<=n;i++)
for(LL j=i+1;j<=n;j++)
ans+=(LL)(j-i)*x[i]*x[j];
}

int main()
{
LL t,n,m;
scanf("%lld",&t);
while(t--)
{
scanf("%lld%lld",&n,&m);
LL cnum=0;
ans=0;
memset(xx,0,sizeof(xx));
memset(yy,0,sizeof(yy));
memset(chex,0,sizeof(chex));
memset(chey,0,sizeof(chey));
for(LL i=1;i<=n;i++)
{
scanf("%s",mp[i]+1);
for(LL j=1;j<=m;j++)
{
if(mp[i][j]=='G')
{
chex[i]=j;
chey[j]=i;
}
else
{
xx[i]++;
yy[j]++;
cnum++;
}
}
}
get_sum(n,xx);//不考虑守卫的影响时候的最短距离的和
get_sum(m,yy);
ans*=2LL;
cnum*=cnum;
//加上守卫对距离的影响
LL l=0,r=0;
for(LL i=1;i<=n;i++)
{
if(chex[i]>chex[i-1]) l+=chex[i]-1;
else if(!chex[i]) l=0;
else l=chex[i]-1;
if(chex[i]) r=m-chex[i];
else r=m;
ans += 4LL*l*r;
}
l=0,r=0;
for(LL i=n;i>=1;i--)
{
if(chex[i]<chex[i+1]) r=0;
if(chex[i]) l=m-chex[i];
else l=m;
ans += 4LL*l*r;
if(chex[i] > chex[i+1]) r+=chex[i]-1;
else if(!chex[i]) r=0;
else r=chex[i]-1;
}
l=0,r=0;
for(LL i=1;i<=m;i++)
{
if(chey[i]>chey[i-1]) l+=chey[i]-1;
else if(!chey[i]) l=0;
else l=chey[i]-1;
if(chey[i]) r=n-chey[i];
else r=n;
ans += 4LL*l*r;
}
l=0,r=0;
for(LL i=m;i>=1;i--)
{
if(chey[i] < chey[i+1]) r=0;
if(chey[i]) l=n-chey[i];
else l=n;
ans += 4LL*l*r;
if(chey[i]>chey[i+1]) r+=chey[i]-1;
else if(!chey[i]) r=0;
else r=chey[i]-1;
}
printf("%.4lf\n",(double)ans/cnum);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: