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进制数 ...
随机推荐
- Erlang语言学习入门
这是一个命令行程序,可以直接在里面输入表达式进行计算,例如来一个简单的: Erlang R15B01 (erts-5.9.1) [smp:4:4] [async-threads:0] Eshell V ...
- Gson 解析JSON数据
{"province":[{"name":"安徽省","city":[{"name":"安 ...
- java基础---->Java中异常的使用(二)
这一篇博客用例子讲述一下异常的处理过程.那些 我们一直惴惴不安 又充满好奇的未来 会在心里隐隐约约地觉得它们是明亮的. 异常的执行过程 一.实例一:return语句 public class Exce ...
- Spring和quartz整合的入门使用教程
Quartz的maven依赖 <!-- quartz 的jar --> <dependency> <groupId>org.quartz-scheduler< ...
- [Gradle] 给已存在的 task 添加依赖
需求:在编译宿主 APP 之前先编译两个插件 SamplePlugin1 和 SamplePlugin2 tasks.whenTaskAdded { task -> if (task.name ...
- JavaScript面向对象OOP思想Class系统
JavaScript的Class模块,纯天然无依赖,只有2k大小,快速高效,让我们优雅的面向对象... | |目录 1源码:jClass.js 2源码:jClass.min.js 3构建一个类 4访问 ...
- Struts2---输入验证
1. Struts2 的验证 1). 验证分为两种: > 声明式验证* 需要解决的问题如下: >> 确定对哪个 Action 或 Model 的那个字段进行验证 >> 使 ...
- base64图片解析
大家可能注意到了,网页上有些图片的src或css背景图片的url后面跟了一大串字符,比如:data:image/png;base64, iVBORw0KGgoAAAANSUhEUgAAAAEAAAAk ...
- cocoapod 最新安装使用步骤
cocoapod 最新安装使用步骤 安装 1.sudo gem update (2个-)system :更新你的gem system至最新 2.gem sources (2个-)remove htt ...
- TabBar变透明
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackTranslucent animated:YES];