B. Black Square

本篇博客介绍了一个编程问题:如何通过最少的操作使纸上的黑色格子形成正方形。包括输入输出示例及代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

B. Black Square
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard 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<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<stack>
typedef long long LL;
using namespace std;
char a[111][111];
int main()
{
    int n, m;
    while (cin >> n >> m)
    {
        getchar();
        for (int i = 0; i < n; i++)
            gets(a[i]);
        int minx = 111111, miny = 111111, maxx = -11111, maxy = -11111;
        int f = 0;
        for(int i=0;i<n;i++)
            for (int j = 0; j < m; j++)
            {
                if (a[i][j] == 'B')
                {
                    f ++;
                    if (i < minx) minx = i;
                    if (i > maxx) maxx = i;
                    if (j < miny) miny = j;
                    if (j > maxy) maxy = j;
                }
            }
    /*  cout << "max_x  " << maxx<<endl;
        cout << "min_x    " << minx<<endl;
        cout << "max_y   " << maxy<<endl;
        cout << "min_y   " << miny << endl;*/
    //  cout << f << endl;
        int sx = maxx - minx+1;
        int sy = maxy - miny+1;
        int s = max(sx, sy);
        int sum = 0;
        if (!f) sum = 1;
        else if (s > n || s > m) sum = -1;
        else sum = s*s-f;
        cout << sum << endl;;
    }
    return 0;
}
import tkinter as tk import ttkbootstrap as ttk from ttkbootstrap.constants import * import math class CornerMarkModule: def __init__(self, parent, width, height, module_id): self.parent = parent self.width = width self.height = height self.module_id = module_id self.line_ids = [] # 创建画布 self.canvas = tk.Canvas(parent, width=width, height=height, bg="black", highlightthickness=0) self.canvas.pack(side=LEFT, padx=10, pady=10) # 绘制中心正方形 (80x80) self.square_size = 80 self.start_x = width // 2 - self.square_size // 2 # 水平居中 self.start_y = height // 2 - self.square_size // 2 # 垂直居中 self.canvas.create_rectangle( self.start_x, self.start_y, self.start_x + self.square_size, self.start_y + self.square_size, fill="darkgray", # 正方形填充为深灰色 outline="darkgray" # 正方形轮廓为深灰色 ) # 添加模块编号标签 self.canvas.create_text( width // 2, height - 15, text=f"模块 {module_id}", fill="darkgray", font=("Arial", 10) ) # 初始参数 self.base_gap = 12 self.base_length = 30 self.create_all_lines() # 动画计数器 self.counter = 0 def create_all_lines(self): # 左上角标记 self.line_ids.append(self.canvas.create_line( self.start_x - self.base_gap, self.start_y - self.base_gap, self.start_x - self.base_gap + self.base_length, self.start_y - self.base_gap, width=2, fill="darkgray" )) self.line_ids.append(self.canvas.create_line( self.start_x - self.base_gap, self.start_y - self.base_gap, self.start_x - self.base_gap, self.start_y - self.base_gap + self.base_length, width=2, fill="darkgray" )) # 右上角标记 self.line_ids.append(self.canvas.create_line( self.start_x + self.square_size + self.base_gap, self.start_y - self.base_gap, self.start_x + self.square_size + self.base_gap - self.base_length, self.start_y - self.base_gap, width=2, fill="darkgray" )) self.line_ids.append(self.canvas.create_line( self.start_x + self.square_size + self.base_gap, self.start_y - self.base_gap, self.start_x + self.square_size + self.base_gap, self.start_y - self.base_gap + self.base_length, width=2, fill="darkgray" )) # 左下角标记 self.line_ids.append(self.canvas.create_line( self.start_x - self.base_gap, self.start_y + self.square_size + self.base_gap, self.start_x - self.base_gap + self.base_length, self.start_y + self.square_size + self.base_gap, width=2, fill="darkgray" )) self.line_ids.append(self.canvas.create_line( self.start_x - self.base_gap, self.start_y + self.square_size + self.base_gap, self.start_x - self.base_gap, self.start_y + self.square_size + self.base_gap - self.base_length, width=2, fill="darkgray" )) # 右下角标记 self.line_ids.append(self.canvas.create_line( self.start_x + self.square_size + self.base_gap, self.start_y + self.square_size + self.base_gap, self.start_x + self.square_size + self.base_gap - self.base_length, self.start_y + self.square_size + self.base_gap, width=2, fill="darkgray" )) self.line_ids.append(self.canvas.create_line( self.start_x + self.square_size + self.base_gap, self.start_y + self.square_size + self.base_gap, self.start_x + self.square_size + self.base_gap, self.start_y + self.square_size + self.base_gap - self.base_length, width=2, fill="darkgray" )) def update_animation(self): # 使用正弦函数创建平滑的距离变化 phase = self.counter * 0.08 self.counter += 1 # 每个模块有不同的相位偏移,创建错落有致的效果 phase_offset = self.module_id * 0.5 distance_factor = 0.5 * math.sin(phase + phase_offset) + 0.5 # 计算当前间距和线长 current_gap = 5 + distance_factor * 20 current_length = 25 + distance_factor * 15 # 更新所有线条位置和长度 self.update_lines(current_gap, current_length) # 继续动画循环 self.parent.after(50, self.update_animation) def update_lines(self, gap, length): # 更新左上角水平线 self.canvas.coords( self.line_ids[0], self.start_x - gap, self.start_y - gap, self.start_x - gap + length, self.start_y - gap ) # 更新左上角垂直线 self.canvas.coords( self.line_ids[1], self.start_x - gap, self.start_y - gap, self.start_x - gap, self.start_y - gap + length ) # 更新右上角水平线 self.canvas.coords( self.line_ids[2], self.start_x + self.square_size + gap, self.start_y - gap, self.start_x + self.square_size + gap - length, self.start_y - gap ) # 更新右上角垂直线 self.canvas.coords( self.line_ids[3], self.start_x + self.square_size + gap, self.start_y - gap, self.start_x + self.square_size + gap, self.start_y - gap + length ) # 更新左下角水平线 self.canvas.coords( self.line_ids[4], self.start_x - gap, self.start_y + self.square_size + gap, self.start_x - gap + length, self.start_y + self.square_size + gap ) # 更新左下角垂直线 self.canvas.coords( self.line_ids[5], self.start_x - gap, self.start_y + self.square_size + gap, self.start_x - gap, self.start_y + self.square_size + gap - length ) # 更新右下角水平线 self.canvas.coords( self.line_ids[6], self.start_x + self.square_size + gap, self.start_y + self.square_size + gap, self.start_x + self.square_size + gap - length, self.start_y + self.square_size + gap ) # 更新右下角垂直线 self.canvas.coords( self.line_ids[7], self.start_x + self.square_size + gap, self.start_y + self.square_size + gap, self.start_x + self.square_size + gap, self.start_y + self.square_size + gap - length ) def create_corner_marks(): # 创建主窗口 - 设置背景为黑色 root = ttk.Window(title="动态距离直角标记 (5模块)", themename="darkly") root.configure(bg="black") # 设置窗口大小并居中 window_width = 1000 window_height = 300 screen_width = root.winfo_screenwidth() screen_height = root.winfo_screenheight() center_x = int(screen_width/2 - window_width/2) center_y = int(screen_height/2 - window_height/2) root.geometry(f"{window_width}x{window_height}+{center_x}+{center_y}") # 创建容器框架 container = ttk.Frame(root, bootstyle="dark") container.pack(fill=BOTH, expand=YES, padx=20, pady=20) # 创建5个直角标记模块 modules = [] for i in range(1, 6): # 创建5个模块 module = CornerMarkModule(container, width=180, height=250, module_id=i) modules.append(module) # 启动所有模块的动画 for module in modules: module.update_animation() root.mainloop() if __name__ == "__main__": create_corner_marks()现在需要对模块中心区域贴上标签,分别是,受到干扰,干扰类型,对抗中,知识库,推理中,决策生成。
07-08
import tkinter as tk import ttkbootstrap as ttk from ttkbootstrap.constants import * import math import random class CornerMarkModule: def __init__(self, parent, width, height, module_id, label_text): self.parent = parent self.width = width self.height = height self.module_id = module_id self.label_text = label_text self.line_ids = [] self.animation_id = None # 创建画布 self.canvas = tk.Canvas( parent, width=width, height=height, bg="#121212", highlightthickness=0, bd=0, relief="flat" ) self.canvas.pack(fill=tk.BOTH, expand=True) # 计算中心位置 self.square_size = 80 self.start_x = width // 2 - self.square_size // 2 self.start_y = height // 2 - self.square_size // 2 - 10 # 创建蓝色圆角矩形 self.square = self.create_round_rect( self.start_x, self.start_y, self.start_x + self.square_size, self.start_y + self.square_size, radius=15, fill="#325b74", outline="#5a9bc0", width=2 ) # 添加标签文本 self.label_id = self.canvas.create_text( width // 2, height // 2 - 10, text=label_text, fill="#e0e0e0", font=("Microsoft YaHei", 11, "bold"), width=self.square_size - 10, justify="center" ) # 添加模块编号标签 self.canvas.create_text( width // 2, height - 30, text=f"模块 {module_id}", fill="#7a7a7a", font=("Microsoft YaHei", 9) ) # 添加装饰线条 self.decor_line = self.canvas.create_line( width//2 - 40, height - 25, width//2 + 40, height - 25, fill="#3a6b8c", width=1, dash=(4, 2) ) # 初始参数 self.base_gap = 12 self.base_length = 30 self.create_all_lines() # 动画计数器 self.counter = 0 # 绑定悬停效果 self.canvas.bind("<Enter>", self.on_enter) self.canvas.bind("<Leave>", self.on_leave) def on_enter(self, event): """鼠标悬停效果""" self.canvas.itemconfig(self.square, fill="#5a9bc0") for line_id in self.line_ids: self.canvas.itemconfig(line_id, fill="#5a9bc0") self.canvas.itemconfig(self.decor_line, fill="#5a9bc0") def on_leave(self, event): """鼠标离开效果""" self.canvas.itemconfig(self.square, fill="#325b74") for line_id in self.line_ids: self.canvas.itemconfig(line_id, fill="#3a6b8c") self.canvas.itemconfig(self.decor_line, fill="#3a6b8c") def create_round_rect(self, x1, y1, x2, y2, radius=25, **kwargs): """创建圆角矩形""" points = [] # 左上角 points.append(x1 + radius) points.append(y1) points.append(x2 - radius) points.append(y1) points.append(x2) points.append(y1) points.append(x2) points.append(y1 + radius) # 右上角 points.append(x2) points.append(y2 - radius) points.append(x2) points.append(y2) points.append(x2 - radius) points.append(y2) # 右下角 points.append(x1 + radius) points.append(y2) points.append(x1) points.append(y2) points.append(x1) points.append(y2 - radius) # 左下角 points.append(x1) points.append(y1 + radius) points.append(x1) points.append(y1) points.append(x1 + radius) points.append(y1) return self.canvas.create_polygon(points, **kwargs, smooth=True) def create_all_lines(self): line_color = "#3a6b8c" # 左上角标记 self.line_ids.append(self.canvas.create_line( self.start_x - self.base_gap, self.start_y - self.base_gap, self.start_x - self.base_gap + self.base_length, self.start_y - self.base_gap, width=2, fill=line_color )) self.line_ids.append(self.canvas.create_line( self.start_x - self.base_gap, self.start_y - self.base_gap, self.start_x - self.base_gap, self.start_y - self.base_gap + self.base_length, width=2, fill=line_color )) # 右上角标记 self.line_ids.append(self.canvas.create_line( self.start_x + self.square_size + self.base_gap, self.start_y - self.base_gap, self.start_x + self.square_size + self.base_gap - self.base_length, self.start_y - self.base_gap, width=2, fill=line_color )) self.line_ids.append(self.canvas.create_line( self.start_x + self.square_size + self.base_gap, self.start_y - self.base_gap, self.start_x + self.square_size + self.base_gap, self.start_y - self.base_gap + self.base_length, width=2, fill=line_color )) # 左下角标记 self.line_ids.append(self.canvas.create_line( self.start_x - self.base_gap, self.start_y + self.square_size + self.base_gap, self.start_x - self.base_gap + self.base_length, self.start_y + self.square_size + self.base_gap, width=2, fill=line_color )) self.line_ids.append(self.canvas.create_line( self.start_x - self.base_gap, self.start_y + self.square_size + self.base_gap, self.start_x - self.base_gap, self.start_y + self.square_size + self.base_gap - self.base_length, width=2, fill=line_color )) # 右下角标记 self.line_ids.append(self.canvas.create_line( self.start_x + self.square_size + self.base_gap, self.start_y + self.square_size + self.base_gap, self.start_x + self.square_size + self.base_gap - self.base_length, self.start_y + self.square_size + self.base_gap, width=2, fill=line_color )) self.line_ids.append(self.canvas.create_line( self.start_x + self.square_size + self.base_gap, self.start_y + self.square_size + self.base_gap, self.start_x + self.square_size + self.base_gap, self.start_y + self.square_size + self.base_gap - self.base_length, width=2, fill=line_color )) def update_animation(self): # 使用正弦函数创建平滑的距离变化 phase = self.counter * 0.08 self.counter += 1 # 每个模块有不同的相位偏移 phase_offset = self.module_id * 0.5 distance_factor = 0.5 * math.sin(phase + phase_offset) + 0.5 # 计算当前间距和线长 current_gap = 5 + distance_factor * 20 current_length = 25 + distance_factor * 15 # 更新所有线条位置和长度 self.update_lines(current_gap, current_length) # 继续动画循环并保存ID self.animation_id = self.parent.after(50, self.update_animation) def pause_animation(self): """暂停动画""" if self.animation_id: self.parent.after_cancel(self.animation_id) self.animation_id = None def resume_animation(self): """恢复动画""" if not self.animation_id: self.update_animation() def reset_counter(self): """重置计数器""" self.counter = 0 def update_lines(self, gap, length): # 更新左上角水平线 self.canvas.coords( self.line_ids[0], self.start_x - gap, self.start_y - gap, self.start_x - gap + length, self.start_y - gap ) # 更新左上角垂直线 self.canvas.coords( self.line_ids[1], self.start_x - gap, self.start_y - gap, self.start_x - gap, self.start_y - gap + length ) # 更新右上角水平线 self.canvas.coords( self.line_ids[2], self.start_x + self.square_size + gap, self.start_y - gap, self.start_x + self.square_size + gap - length, self.start_y - gap ) # 更新右上角垂直线 self.canvas.coords( self.line_ids[3], self.start_x + self.square_size + gap, self.start_y - gap, self.start_x + self.square_size + gap, self.start_y - gap + length ) # 更新左下角水平线 self.canvas.coords( self.line_ids[4], self.start_x - gap, self.start_y + self.square_size + gap, self.start_x - gap + length, self.start_y + self.square_size + gap ) # 更新左下角垂直线 self.canvas.coords( self.line_ids[5], self.start_x - gap, self.start_y + self.square_size + gap, self.start_x - gap, self.start_y + self.square_size + gap - length ) # 更新右下角水平线 self.canvas.coords( self.line_ids[6], self.start_x + self.square_size + gap, self.start_y + self.square_size + gap, self.start_x + self.square_size + gap - length, self.start_y + self.square_size + gap ) # 更新右下角垂直线 self.canvas.coords( self.line_ids[7], self.start_x + self.square_size + gap, self.start_y + self.square_size + gap, self.start_x + self.square_size + gap, self.start_y + self.square_size + gap - length ) def create_corner_marks(): # 创建主窗口 root = ttk.Window(title="动态模块系统", themename="superhero") root.configure(bg="#121212") # 设置窗口大小并居中 window_width = 1400 window_height = 800 screen_width = root.winfo_screenwidth() screen_height = root.winfo_screenheight() center_x = int(screen_width/2 - window_width/2) center_y = int(screen_height/2 - window_height/2) root.geometry(f"{window_width}x{window_height}+{center_x}+{center_y}") # 创建主容器框架 main_container = ttk.Frame(root, padding=(30, 20, 30, 10)) main_container.pack(fill=tk.BOTH, expand=True) # 创建标题 header_frame = ttk.Frame(main_container) header_frame.pack(fill=tk.X, pady=(0, 20)) title_label = ttk.Label( header_frame, text="动态模块系统", font=("Microsoft YaHei", 18, "bold"), bootstyle="inverse-primary" ) title_label.pack(side=tk.LEFT) # 添加副标题 subtitle = ttk.Label( header_frame, text="实时监控与响应系统", font=("Microsoft YaHei", 12), bootstyle="secondary" ) subtitle.pack(side=tk.LEFT, padx=(15, 0), pady=(5, 0)) # 添加控制按钮 control_frame = ttk.Frame(header_frame) control_frame.pack(side=tk.RIGHT) # 创建网格容器 grid_container = ttk.Frame(main_container) grid_container.pack(fill=tk.BOTH, expand=True) # 配置网格行和列权重 grid_container.grid_rowconfigure(0, weight=1) # 第1行 grid_container.grid_rowconfigure(1, weight=1) # 第2行 grid_container.grid_rowconfigure(2, weight=1) # 第3行 grid_container.grid_columnconfigure(0, weight=1) # 第1列 grid_container.grid_columnconfigure(1, weight=1) # 第2列 grid_container.grid_columnconfigure(2, weight=1) # 第3列 grid_container.grid_columnconfigure(3, weight=1) # 第4列 # 定义6个模块的标签文本 labels = ["干扰", "威胁分析", "抗干扰中", "知识库", "推理引擎", "决策输出"] # 创建模块容器 modules = [] # 抗干扰中 - 第1行第2列 (行0, 列1) cell0 = ttk.Frame(grid_container) cell0.grid(row=0, column=1, padx=15, pady=15, sticky="nsew") module0 = CornerMarkModule( cell0, width=220, height=280, module_id=3, label_text=labels[2] ) modules.append(module0) # 知识库 - 第3行第2列 (行2, 列1) cell1 = ttk.Frame(grid_container) cell1.grid(row=2, column=1, padx=15, pady=15, sticky="nsew") module1 = CornerMarkModule( cell1, width=220, height=280, module_id=4, label_text=labels[3] ) modules.append(module1) # 干扰 - 第2行第1列 (行1, 列0) cell2 = ttk.Frame(grid_container) cell2.grid(row=1, column=0, padx=15, pady=15, sticky="nsew") module2 = CornerMarkModule( cell2, width=220, height=280, module_id=1, label_text=labels[0] ) modules.append(module2) # 威胁分析 - 第2行第2列 (行1, 列1) cell3 = ttk.Frame(grid_container) cell3.grid(row=1, column=1, padx=15, pady=15, sticky="nsew") module3 = CornerMarkModule( cell3, width=220, height=280, module_id=2, label_text=labels[1] ) modules.append(module3) # 推理引擎 - 第2行第3列 (行1, 列2) cell4 = ttk.Frame(grid_container) cell4.grid(row=1, column=2, padx=15, pady=15, sticky="nsew") module4 = CornerMarkModule( cell4, width=220, height=280, module_id=5, label_text=labels[4] ) modules.append(module4) # 决策输出 - 第2行第4列 (行1, 列3) cell5 = ttk.Frame(grid_container) cell5.grid(row=1, column=3, padx=15, pady=15, sticky="nsew") module5 = CornerMarkModule( cell5, width=220, height=280, module_id=6, label_text=labels[5] ) modules.append(module5) # 启动所有模块的动画 for module in modules: module.update_animation() root.mainloop() if __name__ == "__main__": create_corner_marks() 该代码容器中的模块是独立的画布,那扩大画布,使模块都放在一个画布中是否就可以绘制灰色线条了?
最新发布
07-10
请优化下面的代码: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、付费专栏及课程。

余额充值