# 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];//初始化标记为全0for (i = 0; i < 30; i++)for (j = 0; j < 30; j++)flag[i][j] = 0;//初始化网格表示为全0memset(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];//初始化标记为全0memset(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;    // 计数器,初始为 1dfs(sx, sy, ans);   // 调用深搜函数cout << ans * 2 << endl;    // 周长为连通块大小乘以 2}return 0;
}

# IMAGE - Image Perimeters相关推荐

  1. SDP(Software Defined Perimeters)核心技术点

    SDP(Software Defined Perimeters)核心技术点 读了一下规范和行业分析,个人觉得SDP的技术核心点: SPA(Single Packet Authenticate) 因为C ...

  2. ACM PKU 1111 Image Perimeters http://acm.pku.edu.cn/JudgeOnline/problem?id=1111

    广搜永远记住,下标不要错,COPY时一定要记得改下标: #include <iostream> #include <queue> using namespace std;cha ...

  3. 深搜(DFS),Image Perimeters

    题目链接:http://poj.org/problem?id=1111 解题报告: 1.这里深搜有一点要注意,对角线上的点,如果为'.',则total不应该增加,因为这不是他的边长. #include ...

  4. ACM题集以及各种总结大全(转)

    ACM题集以及各种总结大全! 虽然退役了,但是整理一下,供小弟小妹们以后切题方便一些,但由于近来考试太多,顾退役总结延迟一段时间再写!先写一下各种分类和题集,欢迎各位大牛路过指正. 一.ACM入门 关 ...

  5. js 不同页面间传递值并取值

    原博主地址:http://blog.csdn.net/web_xyk/article/details/47857033 以前没用到过页面间传递参数再从后台获取数据,然后搜索了一下. 发现了一个比较好的 ...

  6. snort create_mysql_入侵检测系统Snort+Base安装

    安装一些支持库 tar -zxvf zlib-1.2.3.tar.gz cd zlib-1.2.3 ./configure make make install cd .. tar -zxvf libp ...

  7. 勾股定理·圓周率·無窮級數·微積分

    勾股定理 勾股定理 圓 圓形的概念的形成,是人類認知歷史上的一大里程碑. 圓周率 定义1 一个圆形的周长与直径之比: 定义2 以圆形半径为边长作一正方形,然後把圆形面积和此正方形面积比. 圆与外接正方 ...

  8. html 页间传送数据,js 不同页面间传递值并取值,html不同页面间数据传递

    以前没用到过页面间传递参数再从后台获取数据,自己总结了一些. 先说需求:现在有页面pageA.html 和页面pageB.html,页面pageA.html中有一事件,当这个事件触发时会打开页面pag ...

  9. 景观分析工具:arcgis中patch analysis模块

    关键字:景观分析arcgis patch analysis 地理信息系统 转自:http://blog.sina.com.cn/s/blog_44b367b10100xt9m.html 模块下载地址: ...

最新文章

  1. 廉颇老矣,尚能饭否?响鼓重擂,上阵杀敌!
  2. Solr4.7从数据库导数据
  3. SpringBoot 配置绑定
  4. kafka命令行操作
  5. SpringBoot 整合Redis报错:NoClassDefFoundError: redis/clients/util/SafeEncoder
  6. 美团最好战的那个男人要走了
  7. docker知识点查阅
  8. 南开100题计算机三级网络技术,计算机三级网络技术上机南开100题T46-50 -.pdf
  9. 利尔达NB-IOT的PSM和eDRX低功耗模式笔记
  10. jsp网页视频播放器
  11. 方剂学(综合练习)题库【1】
  12. 旷世英才遭天妒——拉马努金
  13. 走出国门,在安防赛道海外本地化部署已成必然
  14. tumblr_使用Tumblr创建美丽且易于更新的博客
  15. 北上杭是梦!“郑福贵”才是中国智慧城市的真相
  16. linux安装nebula
  17. 第三方支付机构有哪些?他们的资金是如何运作保障用户的资金安全的?
  18. teamspeak3快速安装
  19. Signal protocol 开源协议理解
  20. flash粒子文字特效

热门文章

  1. 英语语法精髓,10来节课补完初中,高中英语所有语法,彻底颠覆传统
  2. mysql 数据库备份 乱码_再谈 MySQL 数据库备份恢复和乱码问题
  3. 12_linux笔记-shell脚本命令
  4. html5初学(header、nav、main、aside、footer标签)
  5. 语音识别之Python开发
  6. python爬虫英文翻译_Python爬虫实现翻译功能
  7. Delphi D10.1 移动开发中APP界面基本布局(一)
  8. 多媒体制作技术心得体会_多媒体技术心得体会
  9. 计算机专业英语每天20词-day01
  10. 港湾网络要是真的消失 是中国通信业的悲哀