Noise Level

The Berland's capital has the form of a rectangle with sizes n × m quarters. All quarters are divided into three types:

regular (labeled with the character '.') — such quarters do not produce the noise but are not obstacles to the propagation of the noise;
sources of noise (labeled with an uppercase Latin letter from 'A' to 'Z') — such quarters are noise sources and are not obstacles to the propagation of the noise;
heavily built-up (labeled with the character '*') — such quarters are soundproofed, the noise does not penetrate into them and they themselves are obstacles to the propagation of noise.
A quarter labeled with letter 'A' produces q units of noise. A quarter labeled with letter 'B' produces 2·q units of noise. And so on, up to a quarter labeled with letter 'Z', which produces 26·q units of noise. There can be any number of quarters labeled with each letter in the city.

When propagating from the source of the noise, the noise level is halved when moving from one quarter to a quarter that shares a side with it (when an odd number is to be halved, it's rounded down). The noise spreads along the chain. For example, if some quarter is located at a distance 2 from the noise source, then the value of noise which will reach the quarter is divided by 4. So the noise level that comes from the source to the quarter is determined solely by the length of the shortest path between them. Heavily built-up quarters are obstacles, the noise does not penetrate into them.

The values in the cells of the table on the right show the total noise level in the respective quarters for q = 100, the first term in each sum is the noise from the quarter 'A', the second — the noise from the quarter 'B'.
The noise level in quarter is defined as the sum of the noise from all sources. To assess the quality of life of the population of the capital of Berland, it is required to find the number of quarters whose noise level exceeds the allowed level p.

Input
The first line contains four integers n, m, q and p (1 ≤ n, m ≤ 250, 1 ≤ q, p ≤ 106) — the sizes of Berland's capital, the number of noise units that a quarter 'A' produces, and the allowable noise level.

Each of the following n lines contains m characters — the description of the capital quarters, in the format that was described in the statement above. It is possible that in the Berland's capital there are no quarters of any type.

Output
Print the number of quarters, in which the noise level exceeds the allowed level p.

Examples
input
3 3 100 140
...
A*.
.B.
output
3
input
3 3 2 8
B*.
BB*
BBB
output
4
input
3 4 5 4
..*B
..**
D...
output
7
Note
The illustration to the first example is in the main part of the statement.
#include <iostream>
#include <cstring>
#include <queue>
#define N 255

using namespace std;

struct node
{
    int x;
    int y;
    int level;
}head,tail;

int n,m,p,q;
char s[N][N];
int num,vis[N][N],noise[N][N];
int step[4][2]={{0,1},{0,-1},{1,0},{-1,0}};

void bfs(int x,int y,int level)
{
    queue<node>q;
    head.x=x;
    head.y=y;
    head.level=level;
    vis[x][y]=num;
    q.push(head);

    while(!q.empty())
    {
        head=q.front();
        q.pop();

        noise[head.x][head.y]+=head.level;

        if(!head.level) return;

        for(int i=0;i<4;i++)
        {
            tail.x=head.x+step[i][0];
            tail.y=head.y+step[i][1];
            if(tail.x>=1&&tail.x<=n&&tail.y>=1&&tail.y<=m)
            {
                if(s[tail.x][tail.y]!='*'&&vis[tail.x][tail.y]!=num)
                {
                    tail.level=head.level/2;
                    vis[tail.x][tail.y]=num;
                    q.push(tail);
                }
            }
        }
    }
}

int main()
{
    while(cin>>n>>m>>q>>p)
    {
        num=0;
        memset(vis,0,sizeof(vis));
        memset(noise,0,sizeof(noise));

        for(int i=1;i<=n;i++)   cin>>s[i]+1;

        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                if(s[i][j]>='A'&&s[i][j]<='Z')
                {
                    num++;
                    bfs(i,j,(s[i][j]-'A'+1)*q);
                }
            }
        }

        int ans=0;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                if(noise[i][j]>p)   ans++;
            }
        } 
        cout<<ans<<endl;
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值