423B. Black Square


    
    
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Polycarp has a checkered sheet of paper of size n × m. Polycarp painted some of cells with black, the others remained white. Inspired by Malevich's "Black Square", Polycarp wants to paint minimum possible number of white cells with black so that all black cells form a square.

You are to determine the minimum possible number of cells needed to be painted black so that the black cells form a black square with sides parallel to the painting's sides. All the cells that do not belong to the square should be white. The square's side should have positive length.

Input

The first line contains two integers n and m (1 ≤ n, m ≤ 100) — the sizes of the sheet.

The next n lines contain m letters 'B' or 'W' each — the description of initial cells' colors. If a letter is 'B', then the corresponding cell is painted black, otherwise it is painted white.

Output

Print the minimum number of cells needed to be painted black so that the black cells form a black square with sides parallel to the painting's sides. All the cells that do not belong to the square should be white. If it is impossible, print -1.

Examples
input
5 4
WWWW
WWWB
WWWB
WWBB
WWWW
output
5
input
1 2
BB
output
-1
input
3 3
WWW
WWW
WWW
output
1
Note

In the first example it is needed to paint 5 cells — (2, 2)(2, 3)(3, 2)(3, 3) and (4, 2). Then there will be a square with side equal to three, and the upper left corner in (2, 2).

In the second example all the cells are painted black and form a rectangle, so it's impossible to get a square.

In the third example all cells are colored white, so it's sufficient to color any cell black.

#include <bits/stdc++.h>
#define y1 danisudnassi
using namespace std;

const int N = 100+5;
int n, m, x1 = N, x2, y1 = N, y2, b, l;
char t[N][N];

int main() {
  scanf("%d%d\n", &n, &m);
  for(int i=0; i<n; ++i) for(int j=0; j<m; ++j) {
    scanf(" %c", &t[i][j]);
    if (t[i][j] == 'B') {
      b++;
      x1 = min(x1, j);
      x2 = max(x2, j);
      y1 = min(y1, i);
      y2 = max(y2, i);
    }
  }

  l = max(x2-x1, y2-y1) + 1;
  if (l > m or l > n) printf("-1\n");
  else if (!b) printf("1\n");
  else printf("%d\n", l*l - b);
  return 0;
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
请优化下面的代码:import turtle # 控制台显示部分 print("Hanoi Tower Game") # 获取用户输入 n = int(input("请输入盘子的个数:")) # 初始化三个柱子 a = list(range(n, 0, -1)) b, c = [], [] # 定义移动函数 def move(n, source, target, auxiliary): if n > 0: # 移动 n-1 个盘子到辅助柱子 move(n-1, source, auxiliary, target) # 将最大的盘子移动到目标柱子 target.append(source.pop()) # 显示移动过程 print("Move disk", n, "from", source, "to", target) # 移动 n-1 个盘子从辅助柱子到目标柱子 move(n-1, auxiliary, target, source) # 开始移动 move(n, a, c, b) # turtle部分 screen = turtle.Screen() screen.setup(600, 600) screen.bgcolor("white") # 绘制柱子 pole1 = turtle.Turtle() pole1.hideturtle() pole1.speed(0) pole1.penup() pole1.goto(-150, -200) pole1.pendown() pole1.width(5) pole1.color("black") pole1.left(90) pole1.forward(400) pole2 = pole1.clone() pole2.penup() pole2.goto(0, -200) pole2.pendown() pole2.forward(400) pole3 = pole1.clone() pole3.penup() pole3.goto(150, -200) pole3.pendown() pole3.forward(400) # 绘制盘子 colors = ["red", "green", "blue", "yellow", "purple", "orange"] turtles = [] for i in range(n): t = turtle.Turtle() t.hideturtle() t.shape("square") t.color(colors[i%6]) t.shapesize(1, (n-i)*2, 1) t.penup() t.goto(-150, -200+(i+1)*20) t.pendown() turtles.append(t) # 移动盘子 def move_turtle(disk, source, target): disk.penup() disk.goto(source, 200) disk.pendown() disk.goto(target, 200) disk.goto(target, -200+len(target)*20) # 开始移动 for i in range(2**n-1): disk = turtles[a.index(n-i)] move_turtle(disk, disk.xcor(), -150) a.remove(n-i) b.append(n-i) disk_index = a.index(n-i-1) if (n-i-1) in a else b.index(n-i-1) disk = turtles[disk_index] move_turtle(disk, disk.xcor(), pole_positions[disk_index]) if (n-i-1) in a: a.remove(n-i-1) else: b.remove(n-i-1) c.append(n-i-1) disk_index = a.index(n-i) if (n-i) in a else b.index(n-i) disk = turtles[disk_index] move_turtle(disk, disk.xcor(), pole_positions[disk_index]) if (n-i) in a: a.remove(n-i) else: b.remove(n-i) c.append(n-i) # 等待用户关闭窗口 screen.mainloop()
最新发布
05-31

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值