Description

给出一张由"x"和"."组成的矩阵。每个"x"可以向上下左右及两个斜对角进行连通,请问由某个点开始的"x",它所连通的图形的周长为多少。

Input

整个测试有多组数据,整个测试以四个零代表结束。

对于每个数据,第一行给出整个图形的大小(长度小于50),再给出开始点的坐标。接下来若干行用于描述这个图形。

Output

如题

Sample 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

Sample Output

8

18

40

48

8

这道题是找连通块,只是多了斜线,算周长只要在四周打点,ans++,剩下就是BFS日常操作。

八个方向:

int dir[8][2]={{0,1},{1,0},{-1,0},{0,-1},{1,1},{1,-1},{-1,1},{-1,-1}};

结构体:

struct node{
int x;
int y;
node(){ };
node (int sx,int sy){
x=sx;
y=sy;
}
};

BFS主体:

void bfs(int x,int y){
q1.push(node(x,y));
vis[x][y]=1;
while(!q.empty()){
node prg = q.front();
q.pop(); for(int i=0;i<8;i++){
int tx = prg.x+dir[i][0];
int ty = prg.y+dir[i][1]; if(!vis[tx][ty]&&mp[tx][ty] == 'X'){
q.push(node(tx,ty));
q1.push(node(tx,ty));
vis[tx][ty]=1;
}
}
}
}

打点,计算答案:

void change(){
for(int i=0;i<=n+1;i++){
for(int j=0;j<=m+1;j++){
if(mp[i][j]!='X'&&mp[i][j]!='.'){
mp[i][j]='.';
}
}
}
q.push(node(x,y));
bfs(x,y);
while(!q1.empty()){
node prg = q1.front();
q1.pop();
for(int i=0;i<4;i++){
int tx=prg.x+dir[i][0];
int ty=prg.y+dir[i][1];
if(mp[tx][ty]=='.'){
ans++;
}
}
}
}

完整代码:

#include<bits/stdc++.h>
using namespace std; int ans=0; int dir[8][2]={{0,1},{1,0},{-1,0},{0,-1},{1,1},{1,-1},{-1,1},{-1,-1}}; struct node{
int x;
int y;
node(){ };
node (int sx,int sy){
x=sx;
y=sy;
}
}; char mp[1001][1001];
bool vis[1001][1001]; queue<node> q;
queue<node> q1; void bfs(int x,int y){
q1.push(node(x,y));
vis[x][y]=1;
while(!q.empty()){
node prg = q.front();
q.pop(); for(int i=0;i<8;i++){
int tx = prg.x+dir[i][0];
int ty = prg.y+dir[i][1]; if(!vis[tx][ty]&&mp[tx][ty] == 'X'){
q.push(node(tx,ty));
q1.push(node(tx,ty));
vis[tx][ty]=1;
}
}
}
} int n,m,x,y; void change(){
for(int i=0;i<=n+1;i++){
for(int j=0;j<=m+1;j++){
if(mp[i][j]!='X'&&mp[i][j]!='.'){
mp[i][j]='.';
}
}
}
q.push(node(x,y));
bfs(x,y);
while(!q1.empty()){
node prg = q1.front();
q1.pop();
for(int i=0;i<4;i++){
int tx=prg.x+dir[i][0];
int ty=prg.y+dir[i][1];
if(mp[tx][ty]=='.'){
ans++;
}
}
}
} int main(){ while(cin >> n >> m>> x>>y&&n&&m){ ans=false; memset(vis,0,sizeof vis);
memset(mp,'\0',sizeof mp); for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin >> mp[i][j];
}
}
change();
cout<< ans << endl;
} return 0;
}

Image Perimeters的更多相关文章

  1. ZOJ 1047 Image Perimeters

    原题链接 题目大意:鼠标点击一块,求与之联通的所有区域的边长之和. 解法:广度优先搜索.从选中的这个点开始,往周围8个点依次搜索,访问过的点做上标记.如果该点上下左右的一个或多个方向没有相邻的点,边长 ...

  2. B - Image Perimeters

    Technicians in a pathology lab analyze digitized images of slides. Objects on a slide are selected f ...

  3. 深搜(DFS),Image Perimeters

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

  4. POJ1111 Image Perimeters

    Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 8790   Accepted: 5260 Description Techn ...

  5. [POJ]1111 Image Perimeters

    Description Technicians in a pathology lab analyze digitized images of slides. Objects on a slide ar ...

  6. poj1111 Image Perimeters 广搜

    题目大意: 输入一个矩阵,再输入其中一个“X”的位置(从1开始).从该位置向八个方向扩展,如果是“X”就可以并在一起.问最后得到的模块的周长是多少. 解题思路: 按照广搜的思路来做.用一个二维的数组标 ...

  7. Project Euler 94:Almost equilateral triangles 几乎等边的三角形

    Almost equilateral triangles It is easily proved that no equilateral triangle exists with integral l ...

  8. POJ题目细究

    acm之pku题目分类 对ACM有兴趣的同学们可以看看 DP:  1011   NTA                 简单题  1013   Great Equipment     简单题  102 ...

  9. HOJ题目分类

    各种杂题,水题,模拟,包括简单数论. 1001 A+B 1002 A+B+C 1009 Fat Cat 1010 The Angle 1011 Unix ls 1012 Decoding Task 1 ...

随机推荐

  1. (18)打鸡儿教你Vue.js

    介绍一下怎么安装Vue.js vue.js Vue 不支持 IE8 及以下版本,因为 Vue 使用了 IE8 无法模拟的 ECMAScript 5 特性. Vue.js是一个渐进的,可逐步采用的Jav ...

  2. 【转】浅析Linux中的零拷贝技术

    本文探讨Linux中主要的几种零拷贝技术以及零拷贝技术适用的场景.为了迅速建立起零拷贝的概念,我们拿一个常用的场景进行引入: 引文## 在写一个服务端程序时(Web Server或者文件服务器),文件 ...

  3. kubernetes入门学习系列

    一.kubernetes基础概念 初识kubernetes kubernetes相关概念 二.kubernets架构和组件 kubernetes架构 kubernetes单Master架构 kuber ...

  4. zabbix—自动发现端口并监控

    自动批量检查agent开放的端口 PS:如果服务器上的应用都是固定的,不会随机产生的都可以使用自动发现端口来监控:  如果服务器会随机出现端口且每次启动程序都会改变,可以采用第二种方法,来监控指定的端 ...

  5. Check if List<Int32> values are consecutive

    Check if List<Int32> values are consecutive One-liner, only iterates until the first non-conse ...

  6. 编程基础-c语言中指针、sizeof用法总结

    1.指针 学习 C 语言的指针既简单又有趣.通过指针,可以简化一些 C 编程任务的执行,还有一些任务,如动态内存分配,没有指针是无法执行的.所以,想要成为一名优秀的 C 程序员,学习指针是很有必要的. ...

  7. 网络分裂 redis 集群

    REDIS cluster-tutorial -- Redis中文资料站 -- Redis中国用户组(CRUG) http://www.redis.cn/topics/cluster-tutorial ...

  8. 为Apache添加MP4扩展

    apxs是apache的一个辅助工具软件,它通常用来为apache安装扩展模块,甚至可以直接将.c的源程序自动编译成.so程序,并能自动配置httpd.conf文件,将新安装的扩展添加到配置文件中启用 ...

  9. vim卡死

    使用vim时,如果你不小心按了 Ctrl + s后,你会发现不能输入任何东西了,像死掉了一般,其实vim并没有死掉,这时vim只是停止向终端输出而已,要想退出这种状态,只需按Ctrl + q 即可恢复 ...

  10. 【转载】 180623 Conda install 本地压缩包文件tar.bz2

    原文地址“ https://blog.csdn.net/qq_33039859/article/details/80785535 ----------------------------------- ...