Game

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 588    Accepted Submission(s): 146

Problem Description
  Sea5 and wzh are playing games.

There are some guards on an n × m chessboard. Every guard can attack eight cells around him and release shockwave to attack the whole row and column where he stand.

Sea5 and wzh are at the beginning stage of the game, they have already put some guards on the chess cells. No guards can be attacked by another guard right now. So they all fell asleep.

An innocent passer-by is on the chessboard. He can move to up, down, left or right from where he stands. The guards won’t attack him unless the passer-by move to where they stand. The innocent man may appear at any point on the chessboard and move to any point.

The innocent passer-by wants to know the average shortest distance of all the ways he can move without attacked by guards.

 
Input
  Multiple test cases.

The first line is an integer T(T≤50), the number of cases.

For each case, first line is two integers n and m(2≤n,m,≤1000).

The next n lines contain m symbols indicate the cells of chessboard. ‘G’ indicates a guard and ‘#’ indicates an empty cell.

 
Output
  One line per case, shortest distance of all the ways the passer-by can move without attacked by guards.

Round the answer to four decimals.

 
Sample Input
1
2 2
##
G#
 
Sample Output
0.8889

Hint

Ways of distance 0: 3
Ways of distance 1: 4
Ways of distance 2: 2
The answer is (3 * 0 + 1 * 4 + 2 * 2) / (3 + 4 + 2)

 
Author
HIT
 
Source
 
题意:问一个图中,在不经过障碍的前提下,随意选取起点终点的期望路径长度。
有一个条件,就是障碍物每行、每列只有一个,且一个障碍的八联通方格内不会有另一个障碍。

  

题解:
障碍物的条件使此题简单不少。
1、首先两个格子要么不会被阻碍(即最小距离为曼哈顿距离),要么只会绕行2的距离。
证明:
按照此种策略走即可:假设起点在左上方,终点在右下方,不影响结论。
那么,先优先往下走,若被阻挡则往右移动一个单位,再重复这个过程。
可以知道最后要么直接到达终点,要么停留在与终点同一条直线上(正上方或者正左方)。后者只需绕行距离2。 2、被阻挡的情形为 #S###
#####
#G###
#####
##G##
#####
###G#
###E# 即从S到E每一个竖列都有障碍。
其余情况可以有这种情况翻转得到。

  

 const int N = ;
int n, m;
char graph[N][N];
int onx[N], ony[N];
// onx -> The y coordinate of the guard on i x-coordinate
// ony -> The x coordinate of the guard on i y-coordinate inline ll manhattanTo(int x, int y) {
++x, ++y;
ll deltax = ( * x * x - * x + n * n - * n * x + n);
ll deltay = ( * y * y - * y + m * m - * m * y + m);
return deltax * m / + deltay * n / ;
} inline void init() {
clr(onx, -), clr(ony, -);
for(int i = ; i < n; ++i)
for(int j = ; j < m; ++j)
if(graph[i][j] == 'G') onx[i] = j, ony[j] = i;
} inline void upsidedown() {
for(int i = , _i = n - i - ; i < _i; ++i, --_i)
for(int j = ; j < m; ++j) swap(graph[i][j], graph[_i][j]);
init();
} inline void symmetryflip() {
int len = max(n, m);
for(int i = ; i < len; ++i)
for(int j = i; j < len; ++j) swap(graph[i][j], graph[j][i]);
swap(n, m);
init();
} inline ll work() {
// only work for these kind
// #G......
// ...G....
// ......G#
ll ret = ;
int lasty = -, cnt = ;
for(int i = ; i < n; ++i)
if(onx[i] == -) lasty = -, cnt = ;
else if(onx[i] > lasty) {
ret += cnt * 2ll * (m - onx[i] - );
lasty = onx[i], cnt += onx[i];
} else lasty = onx[i], cnt = onx[i];
return ret;
} inline void solve() {
init(); ll nm = n * m;
ll ans = (nm - ) * nm * (n + m) / ;
// cout << ans << endl; // The start point is guard
for(int i = ; i < n; ++i)
for(int j = ; j < m; ++j)
if(graph[i][j] == 'G') ans -= manhattanTo(i, j);
// The end point is guard
for(int i = ; i < n; ++i)
for(int j = ; j < m; ++j)
if(graph[i][j] == 'G') ans -= manhattanTo(i, j);
// Both start point and end point are guard
for(int i = ; i < n; ++i)
if(onx[i] != -) {
for(int j = ; j < n; ++j)
if(onx[j] != -) ans += abs(onx[i] - onx[j]);
}
for(int i = ; i < m; ++i)
if(ony[i] != -) {
for(int j = ; j < m; ++j)
if(ony[j] != -) ans += abs(ony[i] - ony[j]);
}
// cout << ans << endl; // Detour in one line
for(int i = ; i < n; ++i)
if(onx[i] != -) ans += 4ll * onx[i] * (m - onx[i] - );
for(int i = ; i < m; ++i)
if(ony[i] != -) ans += 4ll * ony[i] * (n - ony[i] - );
// Detour for block
// #..
// G..
// ...
// .G.
// ...
// ..G
// ..#
ans += 2ll * work();
upsidedown();
ans += 2ll * work();
symmetryflip();
ans += 2ll * work();
upsidedown();
ans += 2ll * work(); int noGuards = n * m;
for(int i = ; i < n; ++i)
for(int j = ; j < m; ++j)
if(graph[i][j] == 'G') --noGuards;
ll ways = noGuards * 1ll * noGuards; // cout << noGuards << endl;
// cout << ans << " " << ways << endl; printf("%.4lf\n", ans / (. * ways));
} int main() {
int testCase;
scanf("%d", &testCase);
while(testCase--) {
scanf("%d%d", &n, &m);
for(int i = ; i < n; ++i) scanf("%s", graph[i]);
solve();
}
return ;
}

2016 Multi-University Training Contest 1 C.Game的更多相关文章

  1. 2016 Al-Baath University Training Camp Contest-1

    2016 Al-Baath University Training Camp Contest-1 A题:http://codeforces.com/gym/101028/problem/A 题意:比赛 ...

  2. 2016 Al-Baath University Training Camp Contest-1 E

    Description ACM-SCPC-2017 is approaching every university is trying to do its best in order to be th ...

  3. 2016 Al-Baath University Training Camp Contest-1 A

    Description Tourist likes competitive programming and he has his own Codeforces account. He particip ...

  4. 2016 Al-Baath University Training Camp Contest-1 J

    Description X is fighting beasts in the forest, in order to have a better chance to survive he's gon ...

  5. 2016 Al-Baath University Training Camp Contest-1 I

    Description It is raining again! Youssef really forgot that there is a chance of rain in March, so h ...

  6. 2016 Al-Baath University Training Camp Contest-1 H

     Description You've possibly heard about 'The Endless River'. However, if not, we are introducing it ...

  7. 2016 Al-Baath University Training Camp Contest-1 G

    Description The forces of evil are about to disappear since our hero is now on top on the tower of e ...

  8. 2016 Al-Baath University Training Camp Contest-1 F

    Description Zaid has two words, a of length between 4 and 1000 and b of length 4 exactly. The word a ...

  9. 2016 Al-Baath University Training Camp Contest-1 D

    Description X is well known artist, no one knows the secrete behind the beautiful paintings of X exc ...

  10. 2016 Al-Baath University Training Camp Contest-1 C

    Description Rami went back from school and he had an easy homework about bitwise operations (and,or, ...

随机推荐

  1. 一次完整的HTTP请求所经历的7个步骤

    HTTP通信机制是在一次完整的HTTP通信过程中,Web浏览器与Web服务器之间将完成下列7个步骤: 1. 建立TCP连接在HTTP工作开始之前,Web浏览器首先要通过网络与Web服务器建立连接,该连 ...

  2. C语言基础(1)-基本语法及注意事项

    1. include 头文件包含 #include <stdio.h>这个是hello world程序的第一句话 # 代表预编译指令 #include的意思就是头文件包含,使用C语言库函数 ...

  3. [Search Engine] 搜索引擎分类和基础架构概述

    大家一定不会多搜索引擎感到陌生,搜索引擎是互联网发展的最直接的产物,它可以帮助我们从海量的互联网资料中找到我们查询的内容,也是我们日常学习.工作和娱乐不可或缺的查询工具.之前本人也是经常使用Googl ...

  4. jQuery如何判断元素是否是隐藏的?

    jQuery函数简介: is(expr) 用一个表达式来检查当前选择的元素集合,如果其中至少有一个元素符合这个给定的表达式就返回true. 如果没有元素符合,或者表达式无效,都返回'false'. 注 ...

  5. 如何给外部引用的js文件传递参数

    1.定义全局变量 <script language="javascript"> var g = "I'm here"; </script> ...

  6. Linux 网络子系统

    今天记录一下Linux网络子系统相关的东西. 因为感觉对这一块还是有一个很大的空白,这件事情太可怕了. 摘抄多份博客进行总结一下Linux网络子系统的相关东西. 一. Linux网络子系统体系结构 L ...

  7. Codeforces 696 C. PLEASE

    Description 三个杯子,一开始钥匙在中间,每次等概率的选择两边的两个,与中间的交换,问第 \(n\) 次选择中间的杯子是钥匙的概率是多少. \(n=\sum_{i=1}^{k} a_i,a_ ...

  8. TFS二次开发系列:五、工作项查询

    本节将讲述如何查询工作项,用于二次开发中定义获取工作项列表. 使用WorkItemStore.Query方法进行查询工作项,其使用的语法和SQL语法类似: Select [标题] from worki ...

  9. ActiveMQ开发与简介

    1.概述与介绍 ActiveMQ是Apache出品,最流行的.功能强大的即时通讯和集成模式的开源服务器.ActiveMQ是一个完全支持JMS1.1和J2EE1.4规范的JMSProvider实现.提供 ...

  10. Java基本数据类型与位运算

    >>赋值运算符 赋值使用操作符“=”.它的意思是“取右边的值(即右值),把它复制给左边(即左值)”.右值可以是任何 常数.变量或者表达式 (只要它能 生成 一个值就行).但左值必须是一个明 ...