浙工大姗姗杯round2 CodeForces 116B Little Pigs and Wolves

B. Little Pigs and Wolves
time limit per test
 2 seconds
memory limit per test
 256 megabytes
input
 standard input
output
 standard output

Once upon a time there were several little pigs and several wolves on a two-dimensional grid of size n × m. Each cell in this grid was either empty, containing one little pig, or containing one wolf.

A little pig and a wolf are adjacent if the cells that they are located at share a side. The little pigs are afraid of wolves, so there will be at most one wolf adjacent to each little pig. But each wolf may be adjacent to any number of little pigs.

They have been living peacefully for several years. But today the wolves got hungry. One by one, each wolf will choose one of the little pigs adjacent to it (if any), and eats the poor little pig. This process is not repeated. That is, each wolf will get to eat at most one little pig. Once a little pig gets eaten, it disappears and cannot be eaten by any other wolf.

What is the maximum number of little pigs that may be eaten by the wolves?

Input

The first line contains integers n and m (1 ≤ n, m ≤ 10) which denotes the number of rows and columns in our two-dimensional grid, respectively. Then follow n lines containing m characters each — that is the grid description. "." means that this cell is empty. "P" means that this cell contains a little pig. "W" means that this cell contains a wolf.

It is guaranteed that there will be at most one wolf adjacent to any little pig.

Output

Print a single number — the maximal number of little pigs that may be eaten by the wolves.

Sample test(s)
input
2 3
PPW
W.P
output
2
input
3 3
P.W
.P.
W.P
output
0
Note

In the first example, one possible scenario in which two little pigs get eaten by the wolves is as follows.

题意:猪和狼一起住,有一天狼饿了想吃猪,一只狼只能吃一只在边上的猪,问做多几只猪被吃
思路:开始觉得是dfs遍历不断更新最大值,因为考虑了两只狼不能吃同一只猪的问题。但是后来看到题目里的一句话,一只猪旁边只有一只狼,结果就变成了很简单的水题。
#include <iostream>
using namespace std;
int main(){
    int n, m, p = 0;
    cin >> n >> m;
    char a[11][11];
    for(int i = 0; i < n; i++)
        cin >> a[i];
    for(int i = 0; i < n; i++){
        for(int j = 0; j < m; j++){
            if(a[i][j] == 'W' && ((a[i-1][j] == 'P') || a[i][j-1] == 'P' || a[i+1][j] == 'P' || a[i][j+1] == 'P'))
                p++;
        }
    }
    cout << p;
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值