# IMAGE - Image Perimeters

news2024/7/3 15:23:36

# IMAGE - Image Perimeters

## 题面翻译

### 描述
给出一张由"x"和"."组成的矩阵。每个"x"可以向上下左右及两个斜对角进行连通,请问由某个点开始的"x",它所连通的图形的周长为多少。
### 输入
整个测试有多组数据,整个测试以四个零代表结束。
对于每个数据,第一行给出整个图形的大小(长度小于50),再给出开始点的坐标。接下来若干行用于描述这个图形。
### 输出
如题

## 题目描述

Technicians in a pathology lab analyze digitized images of slides. Objects on a slide are selected for analysis by a mouse click on the object. The perimeter of the boundary of an object is one useful measure. Your task is to determine this perimeter for selected objects.

 The digitized slides will be represented by a rectangular grid of periods, '.', indicating empty space, and the capital letter 'X', indicating part of an object. Simple examples are

 **XX Grid 1 .XXX Grid 2**   
**XX .XXX**   
 **.XXX**   
 **...X**   
 **..X.**   
 **X...**

 An X in a grid square indicates that the entire grid square, including its boundaries, lies in some object. The X in the center of the grid below is _adjacent_ to the X in any of the 8 positions around it. The grid squares for any two adjacent X's overlap on an edge or corner, so they are connected.

 XXX   
X**X**X  Central X and adjacent X's   
XXX

 An object consists of the grid squares of all X's that can be linked to one another through a sequence of adjacent X's. In Grid 1, the whole grid is filled by one object. In Grid 2 there are two objects. One object contains only the lower left grid square. The remaining X's belong to the other object.

 The technician will always click on an X, selecting the object containing that X. The coordinates of the click are recorded. Rows and columns are numbered starting from 1 in the upper left hand corner. The technician could select the object in Grid 1 by clicking on row 2 and column 2. The larger object in Grid 2 could be selected by clicking on row 2, column 3. The click could not be on row 4, column 3.

 ![](https://cdn.luogu.com.cn/upload/vjudge_pic/SP904/52be350def3b5baae704ab9cf4f5f069c1e5dfc4.png) One useful statistic is the perimeter of the object. Assume each X corresponds to a square one unit on each side. Hence the object in Grid 1 has perimeter 8 (2 on each of four sides). The perimeter for the larger object in Grid 2 is illustrated in the figure at the left. The length is 18.

 Objects will not contain any totally enclosed holes, so the leftmost grid patterns shown below could _NOT_ appear. The variations on the right could appear:

**Impossible Possible**

**XXXX XXXX XXXX XXXX**   
**X..X XXXX X... X...**   
**XX.X XXXX XX.X XX.X**   
**XXXX XXXX XXXX XX.X**

**..... ..... ..... .....**   
**..X.. ..X.. ..X.. ..X..**   
**.X.X. .XXX. .X... .....**   
**..X.. ..X.. ..X.. ..X..**   
**..... ..... ..... .....**

 The input will contain one or more grids. Each grid is preceded by a line containing the number of rows and columns in the grid and the row and column of the mouse click. All numbers are in the range 1-20. The rows of the grid follow, starting on the next line, consisting of '.' and 'X' characters.

 The end of the input is indicated by a line containing four zeros. The numbers on any one line are separated by blanks. The grid rows contain no blanks.

 For each grid in the input, the output contains a single line with the perimeter of the specified object.

 ```

Input:
2 2 2 2
XX
XX
6 4 2 3
.XXX
.XXX
.XXX
...X
..X.
X...
5 6 1 3
.XXXX.
X....X
..XX.X
.X...X
..XXX.
7 7 2 6
XXXXXXX
XX...XX
X..X..X
X..X...
X..X..X
X.....X
XXXXXXX
7 7 4 4
XXXXXXX
XX...XX
X..X..X
X..X...
X..X..X
X.....X
XXXXXXX
0 0 0 0
```
 ```

Output:
8
18
40
48
8
```

## 输入格式

## 输出格式


代码:

#include<iostream>
#include<algorithm>
using namespace std;

char s[30][30];//网格表示
int rows, cols;//行列数
int total;//选中目标的周长
int chick_x, chick_y;//鼠标单击的目标坐标

//对角线增量
int diagonal[4][2] = {
	{1,1},{-1,1},{-1,-1},{1,-1}
};
//垂直方向增量
int x_y[4][2] = {
	{1,0},{0,1},{-1,0},{0,-1}
};

//标记已经统计过的坐标
int flag[30][30];

//深搜
void work(int x, int y) {
	int i;
	int newx, newy;

	//标记坐标(x,y)为已经搜索过
	flag[x][y] = 1;

	//搜索水平垂直方向
	for (i = 0; i < 4; i++) {
		newx = x + x_y[i][0];
		newy = y + x_y[i][1];
		if ((s[newx][newy] == 'X') && flag[newx][newy] == 0)
			work(newx, newy);
		else if (s[newx][newy] == '.')
			total++;
	}
	//搜索对角线方向
	for (i = 0; i < 4; i++) {
		newx = x + diagonal[i][0];
		newy = y + diagonal[i][1];
		if ((s[newx][newy] == 'X') && flag[newx][newy] == 0)
			work(newx, newy);
	}
}

int main() {
	int i, j;
	char imge[40];

	//初始化标记为全0
	for (i = 0; i < 30; i++)
		for (j = 0; j < 30; j++)
			flag[i][j] = 0;

	//初始化网格表示为全0
	memset(s, '.', sizeof(s));

	//输入行列数
	cin >> rows >> cols;

	//输入鼠标单击坐标
	cin >> chick_x >> chick_y;

	//构造网格
	for (i = 1; i < rows; i++) {
		cin >> imge[i];
		for (j = 1; j < cols; j++)
			s[i][j] = imge[j - 1];
	}
	work(chick_x, chick_y);
	cout << total;
	return 0;
}

运行结果: 

 


#include<iostream>
#include<algorithm>
using namespace std;

char s[30][30];//网格表示
int rows, cols;//行列数
int total;//选中目标的周长
int chick_x, chick_y;//鼠标单击的目标坐标

//对角线增量
int diagonal[4][2] = {
	{1,1},{-1,1},{-1,-1},{1,-1}
};
//垂直方向增量
int x_y[4][2] = {
	{1,0},{0,1},{-1,0},{0,-1}
};

//标记已经统计过的坐标
int flag[30][30];

//深搜
void work(int x, int y) {
	int i;
	int newx, newy;

	//标记坐标(x,y)为已经搜索过
	flag[x][y] = 1;

	//搜索水平垂直方向
	for (i = 0; i < 4; i++) {
		newx = x + x_y[i][0];
		newy = y + x_y[i][1];
		if ((s[newx][newy] == 'X') && flag[newx][newy] == 0)
			work(newx, newy);
		else if (s[newx][newy] == '.')
			total++;
	}
	//搜索对角线方向
	for (i = 0; i < 4; i++) {
		newx = x + diagonal[i][0];
		newy = y + diagonal[i][1];
		if ((s[newx][newy] == 'X') && flag[newx][newy] == 0)
			work(newx, newy);
	}
}

int main() {
	int i, j;
	char imge[40];

	//初始化标记为全0
	memset(flag, 0, sizeof(flag));

	//初始化网格表示为全为  ‘.’
	memset(s, '.', sizeof(s));

	//输入行列数
	//输入鼠标单击坐标
	cin >> rows >> cols>>chick_x >> chick_y;

	//构造网格
	for (i = 1; i < rows; i++) {
		cin >> imge[i];
		for (j = 1; j < cols; j++)
			s[i][j] = imge[j - 1];
	}
	work(chick_x, chick_y);
	cout << total;
	return 0;
}

#include <iostream>
#include <cstring>
using namespace std;

char s[60][60];     // 储存整个图像
bool vis[60][60];   // 访问标记数组,标记每个点是否已经被访问过
int n, m;           // 图像大小
int sx, sy;         // 开始搜索的起点
int dx[8] = { 0, 0, 1, -1, 1, 1, -1, -1 };    // 方向数组,模拟八个方向移动
int dy[8] = { 1, -1, 0, 0, 1, -1, 1, -1 };

void dfs(int x, int y, int& ans) {   // 深搜函数,引用类型传递 ans 变量
    vis[x][y] = true;               // 标记已经访问过
    for (int i = 0; i < 8; i++) {   // 枚举八个方向
        int nx = x + dx[i];
        int ny = y + dy[i];
        if (nx < 1 || nx > n || ny < 1 || ny > m) continue;   // 判断是否到达图像边界
        if (s[nx][ny] != 'X') continue;      // 如果不是 X,则无需访问
        if (vis[nx][ny]) continue;           // 如果已经访问过,则无需继续访问
        ans++;                              // 更新计数器
        dfs(nx, ny, ans);                   // 继续搜索
    }
}

int main() {
    while (cin >> n >> m >> sx >> sy) {
        if (n == 0 && m == 0 && sx == 0 && sy == 0) break;    // 输入结束标识
        memset(vis, false, sizeof(vis));       // 初始化访问标记数组
        memset(s, '.', sizeof(s));             // 初始化整个图像为 '.'
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                cin >> s[i][j];     // 读入整个图像
            }
        }
        int ans = 1;    // 计数器,初始为 1
        dfs(sx, sy, ans);   // 调用深搜函数
        cout << ans * 2 << endl;    // 周长为连通块大小乘以 2
    }
    return 0;
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/433167.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

SpringBoot 整合ChatGPT API项目实战

准备工作 &#xff08;1&#xff09;已成功注册 OpenAI 的账号。 &#xff08;2&#xff09;创建 API KEY&#xff0c;这个 API KEY 是用于 HTTP 请求身份验证的&#xff0c;可以创建多个。 注意这个创建之后需要马上复制好保存&#xff0c;关闭弹框之后就看不到了。 &#xf…

excel的导入导出的两种方案 (1 EasyExcel 2 Hutool工具类)

文章目录 前言1 EasyExcel的导入导出导出1 导入依赖2 项目中的CourseEntity实体类3 CoureseVo VO类 (对CourseEntity进行EasyExcel导入导出操作)4 导出代码的编写 并最终测试导出效果5 (前端内容 可选)通过vue按钮点击 导出 导入1 导入依赖 跟导出相同2 创建回调监听器3 编写导…

1、Typescript基础入门与环境搭建

1、开发工具安装与基本配置 1.1、软件下载安装 如果你还没有使用过VSCode&#xff0c;当然先要去官网下载了。下载完成后双击安装&#xff0c;一直下一步即可。 1.2、编辑器汉化 如果你英语不是很好&#xff0c;配置中文版界面是很有必要的&#xff0c;安装个插件就可以了。打…

虚化背景 - 基于镜头模糊滤镜的深度映射

镜头模糊 Lens Blur等滤镜可以使用深度映射 Depth Map来设置像素在视觉上的前后关系。因此&#xff0c;常利用深度映射来创建真实感虚化效果&#xff0c;或者进行超越镜头的任意虚化处理。 ◆ ◆ ◆ 基于 Alpha 通道的深度映射关系 一般可通过建立 Alpha 通道或图层蒙版来创建…

【算法基础】DP第三弹 —— 竞赛篇

一、计数问题 (一)Question 1. 问题描述 2. Input 输入包含多组测试数据。每组测试数据占一行,包含两个整数 a 和 b。当读入一行为 0 0 时,表示输入终止,且该行不作处理。(0 < a, b < 100000000) 3. Output 每组数据输出一个结果,每个结果占一行。每个结果包…

MIPI D-PHYv2.5笔记(21) -- Forward High-Speed Data Transmission Timing

声明&#xff1a;作者是做嵌入式软件开发的&#xff0c;并非专业的硬件设计人员&#xff0c;笔记内容根据自己的经验和对协议的理解输出&#xff0c;肯定存在有些理解和翻译不到位的地方&#xff0c;有疑问请参考原始规范看 DDR时钟差分信号和Data差分信号的时序关系如下图所示…

计及调度经济性的光热电站储热容量配置方法【IEEE30节点】(Matlab代码实现)

&#x1f4a5; &#x1f4a5; &#x1f49e; &#x1f49e; 欢迎来到本博客 ❤️ ❤️ &#x1f4a5; &#x1f4a5; &#x1f3c6; 博主优势&#xff1a; &#x1f31e; &#x1f31e; &#x1f31e;博客内容尽量做到思维缜密&#xff0c;逻辑清晰&#xff0c;为了方便读者。 …

计算机网络简史

ARPANET的发展 互联网最早的雏形 1931-ARPANET设计 互联网名人堂 1965-packet switching(分包交换) 1969 第一个RFC(Request for Comments)(开始通过APPANET发布)第一个接口信息处理单元&#xff08;Interface Message Processor&#xff09;&#xff08;下图&#xff0c;节…

ChatGPT时代:我们可能站到了自然语言编程的大门口

ChatGPT大火&#xff0c;我现在有种感觉&#xff1a;我们可能站到了自然语言编程的门口&#xff0c;一脚下去&#xff0c;也许能把门踹开。 当然&#xff0c;也可能会踢到一块铁板。 回顾我们的编程之路&#xff0c;基本上就是一个编程门槛不断降低的历史。 最早的一批前辈们…

wireshark抓包工具的使用

前言 ①wireshark是非常流行的网络封包分析软件&#xff0c;功能十分强大。可以截取各种网络封包&#xff0c;显示网络封包的详细信息。 ②使用wireshark的人必须了解网络协议&#xff0c;否则就看不懂wireshark。 ③为了安全考虑&#xff0c;wireshark只能查看封包&#xff…

【Linux】进程通信之管道通信详解

&#x1f34e;作者&#xff1a;阿润菜菜 &#x1f4d6;专栏&#xff1a;Linux系统编程 一、什么是管道通信 1. 管道通信是一种在进程间传递数据的方法 其实管道通信是Unix中最古老的进程间通信的形式了&#xff1a; 管道通信是一种进程间通信的方式&#xff0c;它可以让一个进…

聚观早报|马斯克将TruthGPT挑战ChatGPT;腾讯披露自研芯片新进展

今日要闻&#xff1a;马斯克将TruthGPT挑战ChatGPT&#xff1b;苹果在印度年销售额近60亿美元&#xff1b;腾讯披露自研芯片沧海最新进展&#xff1b;特斯拉中国工厂普通工人月薪约1万元&#xff1b;飞猪将直接向阿里CEO张勇汇报 马斯克将TruthGPT挑战ChatGPT 4 月 18 日消息&…

Pytorch深度学习笔记(四)梯度向下算法

课程推荐&#xff1a;03.梯度下降算法_哔哩哔哩_bilibili 优化问题&#xff1a;求误差值函数最小的权重w &#xff08;1&#xff09;梯度向下算法思想 在绝大多数的情况下&#xff0c;损失函数是很复杂的&#xff08;比如逻辑回归&#xff09;&#xff0c;根本无法得到参数估计…

从输入url到页面展现(三)通过DNS将域名解析为IP地址以及dns-prefetch的好处

前言 上一节我们用直白的话讲了一下浏览器解析url&#xff0c;而浏览器并不具备发送网络消息的能力&#xff0c;所以委托操作系统发送&#xff0c;而这里的第一步&#xff0c;就是去找到对应web服务器的ip地址&#xff0c;并且对互联网和小子网有了一个认识。 这一节呢说一下我…

换个角度使用Redis去解决跨域存取Session问题

系列文章目录 Redis缓存穿透、击穿、雪崩问题及解决方法 Spring Cache的使用–快速上手篇 分页查询–Java项目实战篇 全局异常处理–Java实战项目篇 该系列文章持续更新&#xff0c;更多的文章请点击我的主页查看哦&#xff01; 文章目录 系列文章目录前言一、遇到的情况二、解…

golang 云效私有模块依赖拉取配置

相关文档 阿里官方文档 go 环境变量配置 export GOPRIVATEcodeup.aliyun.com 凭证设置 非常找重要,https密码配置克隆账户和克隆密码后续会用到 在 可以在 netrc 文件中指定凭据 Linux\MacOS 文件应该存放在执行账户的根目录下&#xff0c;即&#xff1a;~/.netrc sudo v…

SAS 9.3软件安装包下载及安装教程 办公软件

SAS 9.3软件简介&#xff1a; SAS 9.3是目前行业软件中的一款专业的数据统计分析软件&#xff0c;其核心功能包括高级分析、商业智能、客户智能、数据管理、风险管理和欺诈与安全智能六大模块&#xff0c;通过sas软件提供创新的分析、商业智能和数据管理软件与服务&#xff0c…

3Dsmax丨3dsmax2016软件下载安装教程 含全版本软件详细安装流程

大家好~随着计算机技术的发展&#xff0c;3D建模在各个行业中被广泛应用。以游戏行业为例&#xff0c;通过3D建模模型制作、灯光制作、材质渲染、细节润色、渲染合成等能够制作出游戏原神、王者荣耀、和平精英、英雄联盟、明日之后、光遇、哈利波特、剑网3、我的世界、第五人格…

SQL——关于bjpowernode.sql的33道经典例题之1-17

目录 1 查询每个部门最高薪水的人员名称 2 查询哪些人的薪水在部门平均薪水之上 3 查询每个部门的平均薪水等级 3.1 每个部门的平均薪水的等级 3.2 每个部门的平均的薪水等级 4 查询最高薪水&#xff08;不用max函数&#xff09; 5 查询平均薪水最高的部门的部门编号 …

如何使用 GPT-4 为博客目录页打造炫酷前端效果

前不久我用 cmd markdown 写了篇文章《项目 TO 的自我修养》&#xff0c;文章的目录如下&#xff1a; 当我把它发布到线上后&#xff0c;目录却只展示出了二级标题&#xff1a; 这哪行&#xff01;我猜这个可能就是加个配置啥的就能修复。于是马上就问 GPT-4 怎么办&#xff1f…