2016 Multi-University Training Contest 1 C.Game
Game
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 588 Accepted Submission(s): 146
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.
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.
Round the answer to four decimals.
2 2
##
G#
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)
题意:问一个图中,在不经过障碍的前提下,随意选取起点终点的期望路径长度。
有一个条件,就是障碍物每行、每列只有一个,且一个障碍的八联通方格内不会有另一个障碍。
题解:
障碍物的条件使此题简单不少。
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的更多相关文章
- 2016 Al-Baath University Training Camp Contest-1
2016 Al-Baath University Training Camp Contest-1 A题:http://codeforces.com/gym/101028/problem/A 题意:比赛 ...
- 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 ...
- 2016 Al-Baath University Training Camp Contest-1 A
Description Tourist likes competitive programming and he has his own Codeforces account. He particip ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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, ...
随机推荐
- Java开发面试
有很多文章说面试相关的问题,有国内也有国外的,但是我相信不少人,特 别是新人看完后还是觉得比较虚比较泛,似乎好像懂了,但是一遇到面试还 是有些手无足措或者重复犯一些错误.本篇文章正是结合实际经 ...
- PL/SQL 将旧表的一些字段赋值给新的表中的字段的做法
INSERT INTO 新表(字段1,字段2,.......) SELECT 字段1,字段2,...... FROM 旧表
- SDL第一个程序:加载一张图片
直接看代码吧 using System; using System.Collections.Generic; using System.ComponentModel; using System.Dat ...
- RSync实现文件备份同步
[rsync实现网站的备份,文件的同步,不同系统的文件的同步,如果是windows的话,需要windows版本cwrsync] 一.什么是rsync rsync,remote synchronize顾 ...
- Maven个人手册
一.Maven基本使用与设置 1.安装maven插件 1).下载maven并解压到指定目录,到该目录下复制当前路径path 2).在eclipse的dropins目录下编辑maven.link,将ma ...
- Python之路【第二十篇】其他WEB框架
WEB框架功能分析 WEB框架本质上,就是一个SOCKET Server WEB框架前面有WSGI或者是自己写的SOCKET,然后交给URL路由系统处理,然后交给某个函数或某个类,然后在模板里拿到模板 ...
- XMPP开发环境配置
首先配置XMPP开发环境配置需要的软件 先安装xampp-osx-1.8.3-5-installer.dmg 安装成功后launchpad里会多出一个XAMPP(其他),点开里面的manager-os ...
- 关于ajax的提交未完再续!
$.ajax({ cache: true, type: "POST", url:"__URL__/add", data:$('#myform').seriali ...
- PHP系统声明式事务处理
转自:http://www.jianshu.com/p/34261804bc45 1.数据库事务 事务(Transaction)是并发控制的基本单位.所谓的事务,它是一个操作序列,这些操作要么都执行, ...
- Holt-Winters原理和初始值的确定
关于模型 (来自以下PPT,从第4页开始) 关于初始值: 以下文档给出了三个模型的初始值计算的思路. 大致思路如下,建立一个p阶移动平均模型,估计出参数即为初始值,具体的根据三种不同的模型,有 ...