XVII Open Cup named after E.V. Pankratiev Stage 14, Grand Prix of Tatarstan, Sunday, April 2, 2017 Problem L. Canonical duel
题目:Problem L. Canonical duel
Input file: standard input
Output file: standard output
Time limit: 2 seconds
Memory limit: 256 megabytes
In the game «Canonical duel» board N × M is used. Some of the cells of the board contain turrets. A
turret is the unit with four cannons oriented to north, east, south and west. Each turret can shoot exactly
once. When turret is hit by the cannon of another turret, its activated. When turret is activated, all four
cannons shoot simultaneously, then self-destruction process causes the turret to disappear.
Given the board with some turrets. You may add exactly one turret on one of cells which does not
contains a turret and activate this new turret. Your goal is to destroy maximum number of turrets.
Input
First line of the input contains two positive integers N and M, does not exceeding 2000 — size of the
board.
Each of the next N lines contain exactly M chars: ‘+’ denotes that cell is occupied by a turret, and ‘.’
that cell is empty.
Output
In the first line print maximum number of existing turrets you may destroy, then in second line print two
space-separated integers — row and column of place where turret can be set. If it is impossible to destroy
ever one turret in such a way, print only one line containing a zero; if several solutions exist, print any of
them.
Examples
standard input | standard output |
3 4 ++.. +... ..++ |
5 2 4 |
4 5 ++... ..+.. ....+ ...++ |
5 4 1 |
3 3 +++ +++ +++ |
0 |
思路:
用并查集维护每个点所能炸的点数量,然后处理出每个空位置上下左右最近的炮台的位置。之后枚举放置位置即可。
1 #include <bits/stdc++.h> 2 3 using namespace std; 4 5 #define MP make_pair 6 #define PB push_back 7 typedef long long LL; 8 typedef pair<int,int> PII; 9 const double eps=1e-8; 10 const double pi=acos(-1.0); 11 const int K=1e6+7; 12 const int mod=1e9+7; 13 14 char ss[2005][2005]; 15 int n,m,ans,ansx,ansy,f[4001000],cnt[4001000]; 16 int dir[4001000][5],vis[4001000]; 17 int idx(int i,int j) 18 { 19 if(i<1||j<1||i>n||j>m) return 0; 20 return (i-1)*m+j; 21 } 22 int fd(int x) 23 { 24 return f[x]==x?x:f[x]=fd(f[x]); 25 } 26 void join(int y,int x) 27 { 28 int fx=fd(x),fy=fd(y); 29 if(fx!=fy) 30 f[fy]=fx,cnt[fx]+=cnt[fy]; 31 } 32 int main(void) 33 { 34 scanf("%d%d",&n,&m); 35 for(int i=1;i<=n;i++) 36 scanf("%s",&ss[i][1]); 37 for(int i=1;i<=n;i++) 38 for(int j=1;j<=m;j++) 39 f[idx(i,j)]=idx(i,j),cnt[idx(i,j)]=ss[i][j]=='+'; 40 for(int i=1;i<=n;i++) 41 for(int j=1;j<=m;j++) 42 { 43 dir[idx(i,j)][1]=ss[i-1][j]=='+'?idx(i-1,j):dir[idx(i-1,j)][1]; 44 dir[idx(i,j)][3]=ss[i][j-1]=='+'?idx(i,j-1):dir[idx(i,j-1)][3]; 45 } 46 for(int i=n;i;i--) 47 for(int j=m;j;j--) 48 { 49 dir[idx(i,j)][2]=ss[i+1][j]=='+'?idx(i+1,j):dir[idx(i+1,j)][2]; 50 dir[idx(i,j)][4]=ss[i][j+1]=='+'?idx(i,j+1):dir[idx(i,j+1)][4]; 51 } 52 for(int i=1;i<=n;i++) 53 for(int j=1;j<=m;j++) 54 if(ss[i][j]=='+') 55 { 56 if(dir[idx(i,j)][1]) join(idx(i,j),dir[idx(i,j)][1]); 57 if(dir[idx(i,j)][3]) join(idx(i,j),dir[idx(i,j)][3]); 58 //printf("f[%d][%d]:%d\n",i,j,f[idx(i,j)]); 59 } 60 // for(int i=1;i<=n;i++) 61 // for(int j=1;j<=m;j++) 62 // { 63 // printf("dir[%d][%d]:",i,j); 64 // for(int k=1;k<=4;k++) 65 // printf("%d ",dir[idx(i,j)][k]); 66 // printf("\n"); 67 // } 68 // for(int i=1;i<=n;i++) 69 // for(int j=1;j<=m;j++) 70 // if(f[idx(i,j)]==idx(i,j)) 71 // printf("cnt[%d][%d]:%d\n",i,j,cnt[idx(i,j)]); 72 for(int i=1;i<=n;i++) 73 for(int j=1;j<=m;j++) 74 if(ss[i][j]=='.') 75 { 76 int sum=0; 77 for(int k=1;k<=4;k++) 78 if(!vis[fd(dir[idx(i,j)][k])]) 79 vis[fd(dir[idx(i,j)][k])]=1,sum+=cnt[fd(dir[idx(i,j)][k])]; 80 for(int k=1;k<=4;k++) 81 vis[fd(dir[idx(i,j)][k])]=0; 82 //printf("sum:%d %d %d\n",i,j,sum); 83 if(sum>ans) 84 ans=sum,ansx=i,ansy=j; 85 } 86 if(ans) printf("%d\n%d %d\n",ans,ansx,ansy); 87 else printf("0\n"); 88 return 0; 89 }
作者:weeping
出处:www.cnblogs.com/weeping/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。