您的位置:首页 > 其它

组队选拔赛01 ---- misa

2015-07-19 00:20 176 查看

Problem Description

A nice part of the Roman Catholic Mass is the rite of peace when people shake hands with their neighbours and say “peace be with you”. Mirko has found a way to turn this ritual into his own favor.

Inside the church, there are R rows of benches where each row can hold a capacity of S people. We can imagine the seating order as a matrix sized R x S where each element represents either a person or an empty seating space. Let us assume that each person shakes hands with their neighbours. That means that the neighbours are located in one of the eight neighbouring elements (if such element exists):



A seating order of the people inside the church has been given before Mirko enters. Mirko is, of course,late for the morning Mass and will sit in an empty space so that he shakes hands with as many people as he can. If there are no empty seats left, Mirko will simply give up on the idea and go to the evening Mass instead. We can assume that nobody enters the church after Mirko.

Calculate the total number of handshakes given during the morning Mass.

Input

There are multiple test cases. Please process till EOF.

The first line of input contains positive integers R and S (1 ≤ R, S ≤ 50) as stated in the text.

Each of the following R lines contains S characters. These R x S characters represent the seating order.

The character ‘.’ (dot) represents an empty place and the character ‘o’ (lowercase letter o) represents a person.

Output

The first and only line of output should contain the required number of handshakes.

Sample Input

2 3

..o

o..

2 2

oo

oo

Sample Output

2

6

解题思路

题意是 给出人的位置,当前的人可以和八方向上的人握手, 最后再在空位上加一个人(没空位就不加) ,问 最多可以握手几次.

直接遍历,对有人的位置查找8方向上是否有人 [注意握手是双方的会重复计算,有人的次数应除以二] 再加上 所有空位握手次数的最大值

参考代码

#include <stdio.h>
#include <string.h>
#define MAX_N 55
char map[MAX_N][MAX_N];
int dir[8][2]={{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}};
int search(int x, int y){
int sum = 0;
for (int i = 0;i < 8;i++){
int nx = x+dir[i][0], ny = y+dir[i][1];
if (map[nx][ny] == 'o') sum++;
}
return sum;
}
int main()
{
int r, s;
while (~scanf("%d %d",&r, &s)){
memset(map,'.',sizeof(map));
for (int i = 1;i <= r;i++){
getchar();
for (int j = 1;j <= s;j++)
map[i][j] = getchar();
}
int ans = 0, maxn = 0, t;
for (int i = 1;i <= r;i++){
for (int j = 1;j <= s;j++){
if (map[i][j] == 'o')
ans += search(i, j);
else{
t = search(i, j);
if (t > maxn)   maxn = t;
}
}
}
printf("%d\n",ans/2+maxn);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: