组队选拔赛01 ---- misa

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值