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 ...
随机推荐
- [置顶] Hash查找,散列查找
//Hash.h #ifndef HASH_H #define HASH_H #define HASH_ARR_SIZE 100 #define FILL -1 #include <stdlib ...
- Linux设备驱动——内核定时器
内核定时器使用 内核定时器是内核用来控制在未来某个时间点(基于jiffies)调度执行某个函数的一种机制,其实现位于 <Linux/timer.h> 和 kernel/timer.c 文件 ...
- Chapter 1. Introduction gradle介绍
We would like to introduce Gradle to you, a build system that we think is a quantum leap for build ...
- 自定义绘制View
Paint(画笔) Canvas(画布) The Canvas class holds the "draw" calls. To draw s ...
- 配置NFS服务器
一.配置NFS服务器 1.安装软件包 [root@wjb10000 ~]# yum -y install nfs-utils.x86_64 2.修改配置文件[root@wjb10000 ~]# vim ...
- 判断线段相交(hdu1558 Segment set 线段相交+并查集)
先说一下题目大意:给定一些线段,这些线段顺序编号,这时候如果两条线段相交,则把他们加入到一个集合中,问给定一个线段序号,求在此集合中有多少条线段. 这个题的难度在于怎么判断线段相交,判断玩相交之后就是 ...
- hdu 2187
#include <stdio.h> using namespace std; struct race { int p; int w; }; race r[1010]; int cmp(r ...
- 数据类型转换中的一些特殊情况(JY06-JavaScript)
1.字符串的不可变性 字符串定义了后,会一直占据内存空间,企鹅该处内存空间(栈)不可被重新赋值. 2.短路运算 ||.&& 二元运算符,返回参与运算的操作数的原值(原数据类型和原数据) ...
- mssql SUBSTRING和charindex的用法
在工作中用到的例子: select * FROM [CSGDC.DataETLDB].[dbo].[StrategiesList] where strategy_name like '%基建系统%' ...
- 使用UILocalNotification给App添加本地消息通知
使用过的代码,直接贴上 UILocalNotification *notification = [[UILocalNotification alloc] init]; if (notification ...