当前位置: 首页 > news >正文

OJ-1015图像物体的边界

分析

思路

1.输入读取:读取网格的维度(M,N)和像素值到一个二维数组中。
2.迭代:遍历二维数组中的每个单元格。
3.边界检测:对于每个像素值为1的单元格,检查其八个相邻的单元格。如果任何相邻单元格的像素值为5,则增加边界计数。
4,边界计数调整:由于每个边界被计算两次(分别与相邻的两个像素1相关联),需要将计数调整为实际的边界数量。
5,输出结果:输出最终的像素1代表的物体的边界数量。
这个算法基于遍历和相邻像素的检查,以计算像素1代表的物体的边界数量。
 

示例1

输入
6 6
1 1 1 1 1 1
1 5 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 5
输出
2

示例2

输入
6 6
1 1 1 1 1 1
1 5 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 5 1
1 1 1 1 1 1
输出
1

代码优化:

import java.util.Scanner;public class 图像物体边界 {public static int row;public static int col;public static int[][] matrix;public static void main(String[] args) {Scanner in = new Scanner(System.in);row = in.nextInt();col = in.nextInt();matrix = new int[row][col];for (int i = 0; i < row; i++) {for (int j = 0; j < col; j++) {matrix[i][j] = in.nextInt();}}for (int i = 0; i < row; i++) {for (int j = 0; j < col; j++) {if (matrix[i][j] == 5) {if (i > 0 && j > 0) matrix[i - 1][j - 1] = 0;if (i > 0) matrix[i - 1][j] = 0;if (i > 0 && j < col - 1) matrix[i - 1][j + 1] = 0;if (j > 0) matrix[i][j - 1] = 0;if (i > 0 && j < col - 1) matrix[i][j + 1] = 0;if (i < row - 1 && j > 0) matrix[i + 1][j - 1] = 0;if (i < row - 1 && j < col - 1) matrix[i + 1][j + 1] = 0;}}}int count = 0;for (int i = 0; i < row; i++) {for (int j = 0; j < col; j++) {if (matrix[i][j] == 0) {count++;matrix[i][j] = -1;countBorder(i, j);}}}System.out.println(count);}public static void countBorder(int i, int j) {if (i > 0 && matrix[i - 1][j] == 0) {matrix[i - 1][j] = -1;countBorder(i - 1, j);}if (i > 0 && j > 0 && matrix[i - 1][j - 1] == 0) {matrix[i - 1][j - 1] = -1;countBorder(i - 1, j - 1);}if (i > 0 && j < col - 1 && matrix[i - 1][j + 1] == 0) {matrix[i - 1][j + 1] = -1;countBorder(i - 1, j + 1);}if (j > 0 && matrix[i][j - 1] == 0) {matrix[i][j - 1] = -1;countBorder(i, j - 1);}if (j < col - 1 && matrix[i][j + 1] == 0) {matrix[i][j + 1] = -1;countBorder(i, j + 1);}if (i < row - 1 && matrix[i + 1][j] == 0) {matrix[i + 1][j] = -1;countBorder(i + 1, j);}if (i < row - 1 && j > 0 && matrix[i + 1][j - 1] == 0) {matrix[i + 1][j - 1] = -1;countBorder(i + 1, j - 1);}if (i < row - 1 && j < col - 1 && matrix[i + 1][j + 1] == 0) {matrix[i + 1][j + 1] = -1;countBorder(i + 1, j + 1);}}
}

277.【华为OD机试】图像物体的边界(深度优先搜索 (DFS)—Java&Python&C++&JS实现)_图像物体的边界华为od-CSDN博客

http://www.lryc.cn/news/460584.html

相关文章:

  • RAG 入门实践:从文档拆分到向量数据库与问答构建
  • 445: 选择问题
  • IP地址类型选择指南:动态IP、静态IP还是数据中心IP?
  • 基于Python flask的豆瓣电影可视化系统,豆瓣电影爬虫系统
  • 面试不是一场遭遇战
  • 【力扣 | SQL题 | 每日3题】力扣1795,1907,1398,602
  • centos7.9升级rockylinux8.8
  • C++初阶(三)---C++入门(下)
  • Linux--多路转接之epoll
  • 自动化工具Nico,从零开始干掉Appium,移动端自动化测试框架实现
  • Fast CRC32
  • 生成一个带有二维数据和对应标签的螺旋形数据集(非线性可分数据集)的代码解析
  • PHP unset() 函数的作用
  • 长篇故事可视化方法Story-Adapter:能够生成更高质量、更具细腻交互的故事图像,确保每一帧都能准确地传达故事情节。
  • C++基础面试题 | 什么是C++中的运算符重载?
  • 深入 IDEA 字节码世界:如何轻松查看 .class 文件?
  • NodeJS 利用代码生成工具编写GRPC
  • uni-app基础语法(一)
  • Linux:进程控制(三)——进程程序替换
  • LeetCode279:完全平方数
  • python爬虫--某动漫信息采集
  • 使用Rollup.js快速开始构建一个前端项目
  • 10.15学习
  • mongodb-7.0.14分片副本集超详细部署
  • C++运算出现整型溢出
  • LeetCode岛屿数量
  • Karmada核心概念
  • Rust 与生成式 AI:从语言选择到开发工具的演进
  • Python爬虫高效数据爬取方法
  • C语言之扫雷小游戏(完整代码版)