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 |
思路:
用并查集维护每个点所能炸的点数量,然后处理出每个空位置上下左右最近的炮台的位置。之后枚举放置位置即可。
#include <bits/stdc++.h> using namespace std; #define MP make_pair
#define PB push_back
typedef long long LL;
typedef pair<int,int> PII;
const double eps=1e-;
const double pi=acos(-1.0);
const int K=1e6+;
const int mod=1e9+; char ss[][];
int n,m,ans,ansx,ansy,f[],cnt[];
int dir[][],vis[];
int idx(int i,int j)
{
if(i<||j<||i>n||j>m) return ;
return (i-)*m+j;
}
int fd(int x)
{
return f[x]==x?x:f[x]=fd(f[x]);
}
void join(int y,int x)
{
int fx=fd(x),fy=fd(y);
if(fx!=fy)
f[fy]=fx,cnt[fx]+=cnt[fy];
}
int main(void)
{
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
scanf("%s",&ss[i][]);
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
f[idx(i,j)]=idx(i,j),cnt[idx(i,j)]=ss[i][j]=='+';
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
{
dir[idx(i,j)][]=ss[i-][j]=='+'?idx(i-,j):dir[idx(i-,j)][];
dir[idx(i,j)][]=ss[i][j-]=='+'?idx(i,j-):dir[idx(i,j-)][];
}
for(int i=n;i;i--)
for(int j=m;j;j--)
{
dir[idx(i,j)][]=ss[i+][j]=='+'?idx(i+,j):dir[idx(i+,j)][];
dir[idx(i,j)][]=ss[i][j+]=='+'?idx(i,j+):dir[idx(i,j+)][];
}
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
if(ss[i][j]=='+')
{
if(dir[idx(i,j)][]) join(idx(i,j),dir[idx(i,j)][]);
if(dir[idx(i,j)][]) join(idx(i,j),dir[idx(i,j)][]);
//printf("f[%d][%d]:%d\n",i,j,f[idx(i,j)]);
}
// for(int i=1;i<=n;i++)
// for(int j=1;j<=m;j++)
// {
// printf("dir[%d][%d]:",i,j);
// for(int k=1;k<=4;k++)
// printf("%d ",dir[idx(i,j)][k]);
// printf("\n");
// }
// for(int i=1;i<=n;i++)
// for(int j=1;j<=m;j++)
// if(f[idx(i,j)]==idx(i,j))
// printf("cnt[%d][%d]:%d\n",i,j,cnt[idx(i,j)]);
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
if(ss[i][j]=='.')
{
int sum=;
for(int k=;k<=;k++)
if(!vis[fd(dir[idx(i,j)][k])])
vis[fd(dir[idx(i,j)][k])]=,sum+=cnt[fd(dir[idx(i,j)][k])];
for(int k=;k<=;k++)
vis[fd(dir[idx(i,j)][k])]=;
//printf("sum:%d %d %d\n",i,j,sum);
if(sum>ans)
ans=sum,ansx=i,ansy=j;
}
if(ans) printf("%d\n%d %d\n",ans,ansx,ansy);
else printf("0\n");
return ;
}
XVII Open Cup named after E.V. Pankratiev Stage 14, Grand Prix of Tatarstan, Sunday, April 2, 2017 Problem L. Canonical duel的更多相关文章
- XVII Open Cup named after E.V. Pankratiev Stage 14, Grand Prix of Tatarstan, Sunday, April 2, 2017 Problem F. Matrix Game
题目: Problem F. Matrix GameInput file: standard inputOutput file: standard inputTime limit: 1 secondM ...
- XVII Open Cup named after E.V. Pankratiev Stage 14, Grand Prix of Tatarstan, Sunday, April 2, 2017 Problem J. Terminal
题目:Problem J. TerminalInput file: standard inputOutput file: standard inputTime limit: 2 secondsMemo ...
- XVII Open Cup named after E.V. Pankratiev Stage 14, Grand Prix of Tatarstan, Sunday, April 2, 2017 Problem A. Arithmetic Derivative
题目:Problem A. Arithmetic DerivativeInput file: standard inputOutput file: standard inputTime limit: ...
- 【二分】【字符串哈希】【二分图最大匹配】【最大流】XVII Open Cup named after E.V. Pankratiev Stage 14, Grand Prix of Tatarstan, Sunday, April 2, 2017 Problem I. Minimum Prefix
给你n个字符串,问你最小的长度的前缀,使得每个字符串任意循环滑动之后,这些前缀都两两不同. 二分答案mid之后,将每个字符串长度为mid的循环子串都哈希出来,相当于对每个字符串,找一个与其他字符串所选 ...
- XVII Open Cup named after E.V. Pankratiev Stage 14, Grand Prix of Tatarstan, Sunday, April 2, 2017 Problem D. Clones and Treasures
题目:Problem D. Clones and TreasuresInput file: standard inputOutput file: standard outputTime limit: ...
- 【二分图】【并查集】XVII Open Cup named after E.V. Pankratiev Stage 14, Grand Prix of Tatarstan, Sunday, April 2, 2017 Problem L. Canonical duel
给你一个网格(n<=2000,m<=2000),有一些炸弹,你可以选择一个空的位置,再放一个炸弹并将其引爆,一个炸弹爆炸后,其所在行和列的所有炸弹都会爆炸,连锁反应. 问你所能引爆的最多炸 ...
- 【动态规划】【滚动数组】【bitset】XVII Open Cup named after E.V. Pankratiev Stage 14, Grand Prix of Tatarstan, Sunday, April 2, 2017 Problem J. Terminal
有两辆车,容量都为K,有n(10w)个人被划分成m(2k)组,依次上车,每个人上车花一秒.每一组的人都要上同一辆车,一辆车的等待时间是其停留时间*其载的人数,问最小的两辆车的总等待时间. 是f(i,j ...
- 【枚举】【最小表示法】XVII Open Cup named after E.V. Pankratiev Stage 14, Grand Prix of Tatarstan, Sunday, April 2, 2017 Problem F. Matrix Game
给你一个n*m的字符矩阵,将横向(或纵向)全部裂开,然后以任意顺序首尾相接,然后再从中间任意位置切开,问你能构成的字典序最大的字符串. 以横向切开为例,纵向类似. 将所有横排从大到小排序,枚举最后切开 ...
- 【推导】【构造】XVII Open Cup named after E.V. Pankratiev Stage 14, Grand Prix of Tatarstan, Sunday, April 2, 2017 Problem E. Space Tourists
给你n,K,问你要选出最少几个长度为2的K进制数,才能让所有的n位K进制数删除n-2个元素后,所剩余的长度为2的子序列至少有一个是你所选定的. 如果n>K,那么根据抽屉原理,对于所有n位K进制数 ...
随机推荐
- WPF的DataGrid控件从excel里复制数据然后粘贴
WPF的DataGrid控件不能像winform的DataGridView控件一样,支持值的粘贴.WPF的DataGrid控件本质上是跟数据绑定联系在一起,所以需要进行复制粘贴的操作,可以在wpf里用 ...
- 如何用ChemDraw中的ChemFinder查询反应过程
ChemFinder是ChemDraw化学绘图软件的重要插件之一,ChemFinder是一个贮存众多化学信息的数据库管理系统,不仅可以用于查询基本化学结构,用户还可以用ChemFinder查询需要的反 ...
- Leetcode: Merge/Insert Interval
题目 Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[ ...
- 获取本地的json并展示
我们知道在java中,有两种方式可以传输数据 1.json javaScript Object Notation 是以健值段的方式展示并显示数据的 2.xml 是以节点的方式展示并显示数据的 xml是 ...
- Gradle 模板配置
对于新手配置Gradle是一件很痛苦的事,记住二句话绝对搞定 1.在Gradle-->gradle-wrapper.properties中配置distributionUrl=https\://s ...
- tortoiseSVN如何回滚(切换至)某个历史版本?
tortoiseSVN如何回滚(切换至)某个历史版本? 1.右键需要回滚的项目,tortoiseSVN - >show log 2.右键需要回滚的历史版本,选择revert to this re ...
- js 跨域 之 修改服务器配置-XAMPP-Apache (nginx 拉到最后!)
js高程第21章提到了ajax 跨域技术,方法有很多,如图: 我主要讲这个: 其实代码就是这样就好了,当然只兼容 IE9 及之后的版本 ,IE9 之前的版本请去原书看吧,Page 600 var xh ...
- 160308、java排序(形如1.1、1.2.1)
package com.rick.sample; import java.util.ArrayList; import java.util.Collections; import java.uti ...
- ggplot2画图小试
# 注意aes(x=wt, y=mpg)中的wt不是字符"wt",因此它是属性字段名(例如,EXCel中字段名有Student,那就是Student,而不是"Studen ...
- Bootstrap学习记录
中文官网 Bootstrap 插件 Bootstrap Multiselect bootstrap-multiselect 的简单使用,样式修改,动态创建option JS组件系列——Bootstra ...