Codeforces847i noiselevel

阅读全文大概需要 4分钟
本文总阅读量
Posted by yjjr's blog on February 6, 2018

标签:bfs,队列

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

·  regular (labeled with the character '.') — such quarters do not produce the noisebut are not obstacles to the propagation of the noise;

·  sources of noise (labeled with an uppercaseLatin letter from 'A' to 'Z') — such quarters are noise sources and arenot 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 thepropagation of noise.

A quarterlabeled with letter 'A' produces q units of noise. Aquarter labeled with letter 'B' produces 2·q units of noise. Andso on, up to a quarter labeled with letter 'Z', which produces 26·q units ofnoise. There can be any number of quarters labeled with each letter in thecity.

Whenpropagating from the source of the noise, the noise level is halved when movingfrom one quarter to a quarter that shares a side with it (when an odd number isto be halved, it's rounded down). The noise spreads along the chain. Forexample, if some quarter is located at a distance 2 from the noise source, then the value of noise which willreach the quarter is divided by 4. So thenoise level that comes from the source to the quarter is determined solely bythe length of the shortest path between them. Heavily built-up quarters areobstacles, the noise does not penetrate into them.


 The values in the cells of the table on the right show the totalnoise level in the respective quarters for q = 100, the first termin each sum is the noise from the quarter 'A', the second — the noise from thequarter 'B'.

The noiselevel in quarter is defined as the sum of the noise from all sources. To assessthe quality of life of the population of the capital of Berland, it is requiredto find the number of quarters whose noise level exceeds the allowed levelp.

Input

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

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

Output

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

Example

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

Theillustration to the first example is in the main part of the statement.

直接用队列模拟bfs,模板题

Code

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int dx[4]={0,1,0,-1};
const int dy[4]={1,0,-1,0};
const int maxn=305;
 
char st[maxn][maxn];
int x[maxn*maxn],y[maxn*maxn],z[maxn*maxn];
int was[maxn][maxn],noise[maxn][maxn];
int n,m,q,p,cnt=0;
int main()
{
       cin>>n>>m>>q>>p;
       rep(i,0,n-1)scanf("%s",st[i]);
       rep(i,0,n-1)
           rep(j,0,m-1)
                  if(st[i][j]>='A'&&st[i][j]<='Z'){
                         inttemp=(st[i][j]-'A'+1)*q;
                         inthead=0,tail=1;
                         x[0]=i,y[0]=j,z[0]=temp;
                         was[i][j]=++cnt;
                         while(head<tail){
                                noise[x[head]][y[head]]+=z[head];
                                if(z[head]>=2)
                                       rep(j,0,3){
                                              intxnow=x[head]+dx[j],ynow=y[head]+dy[j];
                                              if(xnow>=0&&ynow>=0&&xnow<n&&ynow<m)
                                                     if(st[xnow][ynow]!='*'&&was[xnow][ynow]!=cnt){
                                                            x[tail]=xnow,y[tail]=ynow,z[tail]=z[head]>>1;
                                                            was[xnow][ynow]=cnt;tail++;
                                                     }
                                       }
                                head++;
                         }
                 }
       intans=0;
       rep(i,0,n-1)
           rep(j,0,m-1)if(noise[i][j]>p)ans++;
       cout<<ans<<endl;
       return0;
}



本文可以转载,但必须附上原文链接,否则你会终生找不到妹子!!!欢迎关注我的CSDN: ahyjjr