cf D. Broken Monitor
http://codeforces.com/contest/370/problem/D
题意:输入一张图,上面只有两个字符'w'和‘.’ ,如果可以用一个正方形把所有的‘w’围起来,所有的‘w’都在正方形的边上。如果有多种输出最小的一个。
先预处理出[1,1]到[i,j]里面有多少个'w'存在dp[i][j]中。找到正方形的大小,然后枚举找左上角的点。就可以找到符合题意的正方形。
#include <cstdio>
#include <cstring>
#include <algorithm>
#define maxn 2001
using namespace std; char g[maxn][maxn];
int dp[maxn][maxn];
int n,m; int get_num(int x1,int y1,int x2,int y2)
{
if(x1>x2||y1>y2) return ;
return dp[x2][y2]-dp[x2][y1-]-dp[x1-][y2]+dp[x1-][y1-];
} int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
int max1=,max2=,min1=n,min2=m;
int cnt=;
for(int i=; i<=n; i++)
{
scanf("%s",g[i]+);
for(int j=; j<=m; j++)
{
if(g[i][j]=='w')
{
cnt++;
min1=min(min1,i);
min2=min(min2,j);
max1=max(max1,i);
max2=max(max2,j);
}
}
}
bool flag=false;
for(int i=min1+; i<max1; i++)
{
for(int j=min2+; j<max2; j++)
{
if(g[i][j]=='w')
{
flag=true;
break;
}
}
if(flag) break;
}
if(flag)
{
printf("-1\n");
}
else
{
int x1,y1;
bool flag1=false;
int dx=max1-min1+;
int dy=max2-min2+;
int size=max(dx,dy);
for(int i=; i<=n; i++)
{
for(int j=; j<=m; j++)
{
dp[i][j]=dp[i][j-]+dp[i-][j]-dp[i-][j-];
if(g[i][j]=='w')
{
dp[i][j]+=;
}
}
}
for(int i=; i<=n; i++)
{
if(i+size->n)break;
for(int j=; j<=m; j++)
{
if(j+size->m) break;
if(cnt==get_num(i,j,i+size-,j+size-)-get_num(i+,j+,i+size-,j+size-))
{
x1=i;
y1=j;
flag1=true;
break;
}
}
if(flag1) break;
}
if(flag1)
{
for(int i=x1; i<x1+size; i++)
{
for(int j=y1; j<y1+size; j++)
{
if((g[i][j]=='.'&&i==x1)||(g[i][j]=='.'&&j==y1)||(g[i][j]=='.'&&i==x1+size-)||(g[i][j]=='.'&&j==y1+size-)) g[i][j]='+';
}
}
for(int i=; i<=n; i++)
{
for(int j=; j<=m; j++)
{
printf("%c",g[i][j]);
}
printf("\n");
}
}
else
{
printf("-1\n");
}
}
}
return ;
}
cf D. Broken Monitor的更多相关文章
- CF 24 D. Broken robot
D. Broken robot 链接. 题意: 一个方格,从(x,y)出发,等价的概率向下,向左,向右,不动.如果在左右边缘上,那么等价的概率不动,向右/左,向下.走到最后一行即结束.求期望结束的步数 ...
- CF 258 D. Little Elephant and Broken Sorting
D. Little Elephant and Broken Sorting 链接 题意: 长度为n的序列,m次操作,每次交换两个位置,每次操作的概率为$\frac{1}{2}$,求m此操作后逆序对的期 ...
- CF 16C. Monitor
题目链接 水题依旧无法1Y. #include <cstdio> #include <iostream> #include <cmath> using namesp ...
- Codeforces 846D Monitor(简单二分+二维BIT)
D. Monitor time limit per test 2 seconds memory limit per test 256 megabytes input standard input ou ...
- 11g新特性:Health Monitor Checks
一.什么是Health Monitor ChecksHealth Monitor Checks能够发现文件损坏,物理.逻辑块损坏,undo.redo损坏,数据字典损坏等等.Health Monitor ...
- B - Broken Keyboard (a.k.a. Beiju Text)
Problem B Broken Keyboard (a.k.a. Beiju Text) You're typing a long text with a broken keyboard. Well ...
- uva - Broken Keyboard (a.k.a. Beiju Text)(链表)
11988 - Broken Keyboard (a.k.a. Beiju Text) You’re typing a long text with a broken keyboard. Well i ...
- B - Broken Keyboard (a.k.a. Beiju Text) 数组模拟链表
You're typing a long text with a broken keyboard. Well it's not so badly broken. The only problem wi ...
- Broken Keyboard (a.k.a. Beiju Text) 思路
问题:You’re typing a long text with a broken keyboard. Well it’s not so badly broken. The only problem ...
随机推荐
- PS:改装店收的是友情价,包安装十五个毛主席。
糟糠之妻下堂,娇俏公主上位--更换宝马三系座椅作业 - 切诺基 Jeep家族 越野e族论坛 越野/SUV/旅行/赛事/改装/互动中心 PS:改装店收的是友情价,包安装十五个毛主席.
- Sublime Text 2.0.2 注册码
Sublime Text 2.0.2 注册码 直接输入注册码就可以了----- BEGIN LICENSE -----Andrew WeberSingle User LicenseEA7E-85560 ...
- RESTFul中的那些事(1)---在RESTFul中,HTTP Put和Patch操作的差别?
笔者在用调用Google Calendar和Google Tasks的RESTFul API的时候.遇到了一个特殊的操作,PATCH. 那么PATCH操作和PUT操作的差别是什么呢? 依据PATCH ...
- MySQL高可用基础之keepalived+双主复制【转】
环境:MySQL-VIP:192.168.1.3MySQL-master1:192.168.1.1MySQL-master2:192.168.1.2 OS版本:CentOS release 6.4 ( ...
- 初探swift语言的学习笔记四(类对象,函数)
作者:fengsh998 原文地址:http://blog.csdn.net/fengsh998/article/details/29606137 转载请注明出处 假设认为文章对你有所帮助,请通过留言 ...
- mysql 修复表和优化表
REPAIR TABLE `table_name` 修复表 OPTIMIZE TABLE `table_name` 优化表
- 斐波那契数列(fabnacci)java实现
斐波那契数列定义:From Wikipedia, the free encyclopedia http://en.wikipedia.org/wiki/Fibonacci_number In math ...
- LA 6450 Social Advertising
[题目] 给一个无向图,每当对某个点操作,该点以及与该点相连的点都获得标记,问标记所有点至少需要操作多少次 输入 第一行为T,表示测试数据组数 每组测试数据第一行为n(1<=n<=20)表 ...
- jdbc连接数据库和jdbc-odbc桥接方式连接数据库
//这种方式为jdbc直接连接,需要添加jar文件 1 package com.howe2; import java.sql.*; public class test2 { public static ...
- ExecuteNonQuary接收存储过程的输出类型的变量的值
1.设置所调用的存储过程需要的参数 public decimal CreateOrder(string orderId, int userId, string address) { SqlParame ...