Codeforces #828B: Black Square 题解
来源:互联网 发布:抢票软件开发 编辑:程序博客网 时间:2024/05/02 18:41
如果没有黑格子直接输出1
记录当前所有黑格子中列最靠左的minc,最靠右的maxc,行最靠上的minr,最靠下的maxr
如果maxc-minc+1=maxr-minr+1,那么一定能填出正方形,只要拿面积减去black个数即可
如果不等,那答案最小的正方形边长一定是max(maxr-minr+1,maxc-minc+1)
也就是填成长方形以后再补成正方形
看看剩下的行或列够不够补成正方形即可
#include <cstdio>#include <iostream>#include <cstring>#include <string>#include <cmath>#include <algorithm>#include <cstdlib>#include <utility>#include <map>#include <stack>#include <set>#include <vector>#include <queue>#include <deque>#define x first#define y second#define mp make_pair#define pb push_back#define LL long long#define Pair pair<int,int>#define LOWBIT(x) x & (-x)using namespace std;const int INF=0x7ffffff;int n,m;char a[101][101];int main (){int i,j;scanf("%d%d",&n,&m);for (i=1;i<=n;i++) scanf("%s",a[i]+1);int minc=INF,maxc=-INF,minr=INF,maxr=-INF,cnt=0;for (i=1;i<=n;i++)for (j=1;j<=m;j++)if (a[i][j]=='B'){cnt++;minc=min(minc,j);maxc=max(maxc,j);minr=min(minr,i);maxr=max(maxr,i);}if (cnt==0){printf("1\n");return 0;}if (maxr-minr==maxc-minc){printf("%d\n",(maxr-minr+1)*(maxr-minr+1)-cnt);return 0;}if (maxr-minr<maxc-minc){int ss=maxc-minc+1;int rr=maxr-minr+1,cc=maxc-minc+1;int rem=n-rr;if (rem<cc-rr)printf("-1\n");elseprintf("%d\n",ss*ss-cnt);}else{int ss=maxr-minr+1;int rr=maxr-minr+1,cc=maxc-minc+1;int rem=m-cc;if (rem<rr-cc)printf("-1\n");elseprintf("%d\n",ss*ss-cnt);}return 0;}
阅读全文
0 0
- Codeforces #828B: Black Square 题解
- CodeForces 828B Black Square
- Codeforces 828 B Black Square
- codeforces 828B. Black Square(水题)
- Codeforces 828B Black Square【暴力枚举+二维前缀和】
- Codeforces #423-Div. 2-B. Black Square
- Codeforces Round #423 B. Black Square
- Codeforces Round #423 B. Black Square
- Codeforces828 B. Black Square
- 423B. Black Square
- B. Black Square
- Codeforces Round #423 (Div. 2) B. Black Square(思路)
- Codeforces Round #423 (Div. 2) B. Black Square
- codeforces 431A - Black Square
- Codeforces 431A Black Square
- codeforces 431A Black Square
- Codeforces Round #247 (Div. 2)a.Black Square,b.Shower Line,c.k-Tree前三题
- Codeforces Round #423 (Div. 2) B Black Square 黑色正方形 思维题
- Oracle清空表数据
- 463. Island Perimeter
- 算法系列——Intersection of Two Arrays
- 广播之有序广播以及系统广播的短息
- 日夜切换两种模式
- Codeforces #828B: Black Square 题解
- linux系统快捷键设置
- 平衡树 splay区间翻转 模板
- ios-关于@property和@synthesize
- 预处理相关问题
- java中三个数值大小比较
- [BZOJ 2140] 稳定婚姻 Tarjan求强连通分量
- Verilog HDL小练习
- 11. 图--图的表示