2016 Multi-University Training Contest 1 C.Game
2016-08-24 22:08
218 查看
Game
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 588 Accepted Submission(s): 146
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
1
2 2
##
G#
[align=left]Sample Output[/align]
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)
[align=left]Author[/align]
HIT
[align=left]Source[/align]
2016 Multi-University Training Contest 1
题意:问一个图中,在不经过障碍的前提下,随意选取起点终点的期望路径长度。 有一个条件,就是障碍物每行、每列只有一个,且一个障碍的八联通方格内不会有另一个障碍。
题解: 障碍物的条件使此题简单不少。 1、首先两个格子要么不会被阻碍(即最小距离为曼哈顿距离),要么只会绕行2的距离。 证明: 按照此种策略走即可:假设起点在左上方,终点在右下方,不影响结论。 那么,先优先往下走,若被阻挡则往右移动一个单位,再重复这个过程。 可以知道最后要么直接到达终点,要么停留在与终点同一条直线上(正上方或者正左方)。后者只需绕行距离2。 2、被阻挡的情形为 #S### ##### #G### ##### ##G## ##### ###G# ###E# 即从S到E每一个竖列都有障碍。 其余情况可以有这种情况翻转得到。
const int N = 1010; int n, m; char graph ; int onx , ony ; // 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 = (2 * x * x - 2 * x + n * n - 2 * n * x + n); ll deltay = (2 * y * y - 2 * y + m * m - 2 * m * y + m); return deltax * m / 2 + deltay * n / 2; } inline void init() { clr(onx, -1), clr(ony, -1); for(int i = 0; i < n; ++i) for(int j = 0; j < m; ++j) if(graph[i][j] == 'G') onx[i] = j, ony[j] = i; } inline void upsidedown() { for(int i = 0, _i = n - i - 1; i < _i; ++i, --_i) for(int j = 0; j < m; ++j) swap(graph[i][j], graph[_i][j]); init(); } inline void symmetryflip() { int len = max(n, m); for(int i = 0; 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 = 0; int lasty = -1, cnt = 0; for(int i = 0; i < n; ++i) if(onx[i] == -1) lasty = -1, cnt = 0; else if(onx[i] > lasty) { ret += cnt * 2ll * (m - onx[i] - 1); 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 - 1) * nm * (n + m) / 3; // cout << ans << endl; // The start point is guard for(int i = 0; i < n; ++i) for(int j = 0; j < m; ++j) if(graph[i][j] == 'G') ans -= manhattanTo(i, j); // The end point is guard for(int i = 0; i < n; ++i) for(int j = 0; j < m; ++j) if(graph[i][j] == 'G') ans -= manhattanTo(i, j); // Both start point and end point are guard for(int i = 0; i < n; ++i) if(onx[i] != -1) { for(int j = 0; j < n; ++j) if(onx[j] != -1) ans += abs(onx[i] - onx[j]); } for(int i = 0; i < m; ++i) if(ony[i] != -1) { for(int j = 0; j < m; ++j) if(ony[j] != -1) ans += abs(ony[i] - ony[j]); } // cout << ans << endl; // Detour in one line for(int i = 0; i < n; ++i) if(onx[i] != -1) ans += 4ll * onx[i] * (m - onx[i] - 1); for(int i = 0; i < m; ++i) if(ony[i] != -1) ans += 4ll * ony[i] * (n - ony[i] - 1); // 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 = 0; i < n; ++i) for(int j = 0; 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 / (1. * ways)); } int main() { int testCase; scanf("%d", &testCase); while(testCase--) { scanf("%d%d", &n, &m); for(int i = 0; i < n; ++i) scanf("%s", graph[i]); solve(); } return 0; }
View Code
相关文章推荐
- 【HDU5725 2016 Multi-University Training Contest 1C】【最短路 曼哈顿距离+贪心 前缀和】Game 有特殊守卫地图中所有空格点的最短路之和
- 2016 Multi-University Training Contest 1 C Game(hdu 5725)
- 2016 Multi-University Training Contest 1-1004---HDU 5726 GCD
- 2016 Multi-university training contest
- hdu 5734 Acperience(2016 Multi-University Training Contest 2——化简公式,数学推导)
- 2016 Multi-University Training Contest 2(补题ing)
- 2016 Multi-University Training Contest 1 1005 Necklace (hdu5727)【枚举+匹配】
- 【HDU5731 2016 Multi-University Training Contest 1I】【轮廓线DP+容斥】Solid Dominoes Tilings nm棋盘1x2多边形填充稳定方案
- 2016 Multi-University Training Contest 3
- HDU 5755 Gambler Bo(高斯消元裸题)——2016 Multi-University Training Contest 3
- [HDU5756] Boss Bo [2016 Multi-University Training Contest 3(2016多校联合训练3) E]
- 【HDU5739 2016 Multi-University Training Contest 2F】【cdq分治+并查集做法 or 点双连通做法】Fantasia 每点删除后联通块权值和的积
- 2016 Multi-University Training Contest 4 Another Meaning
- 2016 多校 Multi-University Training Contest 4 Another Meaning
- 2016 Multi-University Training Contest 3 1010 Teacher Bo(数学)
- 【substr + 逆序数+ 线段树push_up的位置!!!】2016 Multi-University Training Contest 4
- 2016 Multi-University Training Contest 5 解题报告
- 2016 Multi-University Training Contest 6题解报告
- 2016 Multi-University Training Contest 6
- HDU 5274 Chess(SG博弈)---2016 Multi-University Training Contest 1