题意:一个n*m的矩阵,每个小矩阵,如果是'*'代表噪音无法路过且无法污染,如果是字母,则为-'A'+1)*q的污染强度,可以向其他矩阵进行污染,依次/2,(污染路径取最短的
思路:bfs+剪枝,最大的最初污染为26*1000000,那么最多跑log(26*1000000)个格子
1 #include<bits/stdc++.h> 2 using namespace std; 3 4 int n,m,q,p; 5 char s[300][300]; 6 int a[300][300]; 7 int vis[300][300]; 8 int b[5][2]={{0,1},{0,-1},{-1,0},{1,0}}; 9 10 struct node{ 11 int x,y; 12 int flag; 13 int vv; 14 node(int xx,int yy,int zz,int vvv) { 15 x=xx;y=yy;flag=zz;vv=vvv; 16 } 17 friend bool operator <(node pp,node qq){ 18 return pp.flag<qq.flag; 19 } 20 }; 21 bool check(int xx,int yy){ 22 if(xx<1||xx>n||yy<1||yy>m) return true; 23 return false; 24 } 25 void dfs(int x,int y,int val){ 26 queue<node>pp; 27 //cout<<x<<" "<<y<<endl; 28 while(!pp.empty()) pp.pop(); 29 pp.push(node(x,y,0,val)); 30 for(int i=max(1,x-40);i<=min(n,(x+40));i++) 31 for(int j=max(1,y-40);j<=min(m,(y+40));j++) vis[i][j]=0; 32 while(!pp.empty()){ 33 node tou=pp.front(); 34 pp.pop(); 35 if(vis[tou.x][tou.y]) continue; 36 vis[tou.x][tou.y]=1; 37 a[tou.x][tou.y]+=tou.vv; 38 for(int i=0;i<4;i++){ 39 int xx=b[i][0]+tou.x; 40 int yy=b[i][1]+tou.y; 41 if(check(xx,yy)||vis[xx][yy]||s[xx][yy]=='*'||tou.vv==0) continue; 42 43 //cout<<xx<<" "<<yy<<" "<<tou.vv/2<<" "<<vis[xx][yy]<<endl; 44 pp.push(node(xx,yy,tou.flag+1,tou.vv/2)); 45 } 46 // cout<<"@"<<endl; 47 } 48 // cout<<endl; 49 } 50 int main(){ 51 scanf("%d%d%d%d",&n,&m,&q,&p); 52 for(int i=1;i<=n;i++){ 53 scanf("%s",s[i]+1); 54 } 55 for(int i=1;i<=n;i++) 56 for(int j=1;j<=m;j++) 57 if(s[i][j]>='A'&&s[i][j]<='Z') { 58 int x=(s[i][j]-'A'+1)*q; 59 dfs(i,j,x); 60 } 61 int sum=0; 62 for(int i=1;i<=n;i++) 63 for(int j=1;j<=m;j++){ 64 if(a[i][j]>p) sum++; 65 } 66 cout<<sum<<endl; 67 }
#include<bits/stdc++.h>usingnamespace std;int n,m,q,p;char s[300][300];int a[300][300];int vis[300][300];int b[5][2]={{0,1},{0,-1},{-1,0},{1,0}};struct node{int x,y;int flag;int vv; node(int xx,int yy,int zz,int vvv){ x=xx;y=yy;flag=zz;vv=vvv;}friendbooloperator<(node pp,node qq){return pp.flag<qq.flag;}};bool check(int xx,int yy){if(xx<1||xx>n||yy<1||yy>m)returntrue;returnfalse;}void dfs(int x,int y,int val){queue<node>pp;//cout<<x<<" "<<y<<endl;while(!pp.empty()) pp.pop(); pp.push(node(x,y,0,val));for(int i=max(1,x-40);i<=min(n,(x+40));i++)for(int j=max(1,y-40);j<=min(m,(y+40));j++) vis[i][j]=0;while(!pp.empty()){ node tou=pp.front(); pp.pop();if(vis[tou.x][tou.y])continue; vis[tou.x][tou.y]=1; a[tou.x][tou.y]+=tou.vv;for(int i=0;i<4;i++){int xx=b[i][0]+tou.x;int yy=b[i][1]+tou.y;if(check(xx,yy)||vis[xx][yy]||s[xx][yy]=='*'||tou.vv==0)continue;//cout<<xx<<" "<<yy<<" "<<tou.vv/2<<" "<<vis[xx][yy]<<endl; pp.push(node(xx,yy,tou.flag+1,tou.vv/2));}// cout<<"@"<<endl;}// cout<<endl;}int main(){ scanf("%d%d%d%d",&n,&m,&q,&p);for(int i=1;i<=n;i++){ scanf("%s",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'){int x=(s[i][j]-'A'+1)*q; dfs(i,j,x);}int sum=0;for(int i=1;i<=n;i++)for(int j=1;j<=m;j++){if(a[i][j]>p) sum++;} cout<<sum<<endl;}