CodeFroces Round 423 B. Black Square(暴力枚举)
来源:互联网 发布:数据驱动安全 pdf 编辑:程序博客网 时间:2024/06/06 22:45
http://codeforces.com/contest/828/problem/B
题目大意,给出一个n*m的纸,里面的方格为白色或者黑色,要求描最少的白色格子使得黑色为一个正方形。如果不存在这种解法则输出-1.
解法:先得到黑色格子的最上最下最左最右的坐标,然后枚举。
代码如下:
#include<bits/stdc++.h>using namespace std;char G[105][105];int find(int lx, int rx, int ty, int ly) {int cnt = 0;for(int j = ty; j <= ly; j++) {for(int i = lx; i <= rx; i++) {if(G[j][i] != 'B') {cnt++;}}}return cnt;}int main() {int n, m, lx = 105, rx = -1, ty = 105, ly = -1, ans = 0x3f3f3f3f, big;cin >> n >> m;for(int i = 0; i < n; i++)scanf("%s", &G[i]);for(int i = 0; i < n; i++) {for(int j = 0; j < m; j++) {if(G[i][j] == 'B') {if(j < lx)lx = j;if(j > rx)rx = j;if(i < ty)ty = i;if(i > ly)ly = i;}}}if(lx != 105 && rx - lx >= ly - ty) {big = rx - lx;for(int i = 0; i + big < n; i++) {if(i <= ty && i + big >= ty) {ans = min(ans, find(lx, rx, i, i + big));}}} if(lx != 105 && rx - lx <= ly - ty) {big = ly - ty;for(int i = 0; i + big < m; i++) {if(i <= lx && i + big >= rx) {ans = min(ans, find(i, i + big, ty, ly));}}}if(lx != 105 && ans == 0x3f3f3f3f)cout << "-1" << endl;else if(ans == 0x3f3f3f3f) cout << "1" << endl;elsecout << ans << endl;return 0;}
阅读全文
0 0
- CodeFroces Round 423 B. Black Square(暴力枚举)
- Codeforces 828B Black Square【暴力枚举+二维前缀和】
- Codeforces Round #423 B. Black Square
- Codeforces Round #423 B. Black Square
- 423B. Black Square
- Codeforces Round #423 (Div. 2) B. Black Square(思路)
- Codeforces Round #423 (Div. 2) B. Black Square
- CodeFroces Round 877 B
- Codeforces Round #423 (Div. 2) B Black Square 黑色正方形 思维题
- Codeforces #423-Div. 2-B. Black Square
- Codeforces828 B. Black Square
- B. Black Square
- Codeforces-----233B---Non-square Equation暴力枚举
- CodeFroces 834B. The Festive Evening(暴力)
- CodeForces 828B Black Square
- Codeforces 828 B Black Square
- Codeforces Round #243 (Div. 2) A,B,C(暴力枚举)
- Codeforces Round #392(Div. 2) B Blown Garland【暴力枚举】
- 欢迎使用CSDN-markdown编辑器
- js 原生计算两个时间戳相差天数
- [LeetCode] 26. Remove Duplicates from Sorted Array
- POJ 3176 Cow Bowling G++
- Makefile万能模板
- CodeFroces Round 423 B. Black Square(暴力枚举)
- 编译程序和解释程序
- ACdream 1119 瑶瑶的动感光波(加强版)(LCA)(背包dp)
- 文章标题
- Java中的泛型
- Struts2
- 推荐几个网站 以后可用得到
- 项目加载编译实质性流程
- java 多态环境下对 方法和成员变量的调用