XVII Open Cup named after E.V. Pankratiev Stage 14, Grand Prix of Tatarstan, Sunday, April 2, 2017 P...

本文解析了一道名为《典战》的算法题目,通过使用并查集维护炮台覆盖范围,实现对炮台的最大破坏策略。介绍了如何利用方向数组记录炮台位置,并通过遍历确定最佳新增炮台位置。

题目: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 }

 

转载于:https://www.cnblogs.com/weeping/p/7291990.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值