Noise Level CodeForces - 847I

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 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.

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

The illustration to the first example is in the main part of the statement.

题解:普通的BFS,不过别忘了剪枝:噪声降低到0后,就返回。

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 
 4 struct node{
 5     int x,y,step;
 6     node(int x,int y,int step):x(x),y(y),step(step){}; 
 7 };
 8 
 9 int n,m,nq,np;
10 int dx[4]={0,1,0,-1},dy[4]={1,0,-1,0};
11 int sum[255][255];
12 bool vis[255][255];
13 char m_map[255][255];
14 
15 void BFS(int From,int To){
16     memset(vis,false,sizeof(vis));
17     queue<node> q;
18     vis[From][To]=true;
19     int temp=((m_map[From][To]-'A')+1)*nq;
20     
21     q.push(node(From,To,temp)); 
22     while(!q.empty()){
23         node p=q.front();
24         q.pop();
25         if(p.step<=0) continue;   //more important!!!!!!!
26         for(int i=0;i<4;i++){
27             int mx=p.x+dx[i],my=p.y+dy[i];
28             if(mx<0||mx>=n||my<0||my>=m||vis[mx][my]||m_map[mx][my]=='*') continue;
29             vis[mx][my]=true;
30             sum[mx][my]+=p.step/2;
31             q.push(node(mx,my,p.step/2));
32         }
33     }
34     sum[From][To]+=temp;
35 }
36 
37 void Solve(){
38     int cnt=0;    
39     for(int i=0;i<n;i++)
40         for(int j=0;j<m;j++)
41             if(sum[i][j]>np) cnt++;
42     printf("%d\n",cnt);
43 }
44 
45 int main()
46 {   scanf("%d%d%d%d",&n,&m,&nq,&np);
47     for(int i=0;i<n;i++) scanf("%s",m_map[i]);
48     for(int i=0;i<n;i++){ 
49         for(int j=0;j<m;j++){
50             if(m_map[i][j]=='*') sum[i][j]=0;
51             if(m_map[i][j]>='A'&&m_map[i][j]<='Z') BFS(i,j);
52         }
53     }
54     Solve();
55 }

 

 

 

posted @ 2017-10-19 21:26  天之道,利而不害  阅读(475)  评论(0编辑  收藏  举报