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. Windows系统清除占用的串口号列表批处理

    蛋疼总是无缘无故被占用 @echo off reg query "HKLM\SYSTEM\CurrentControlSet\Control\COM Name Arbiter" / ...

  2. A Comprehensive Evaluation of Approaches for Built-Up Area Extraction from Landsat OLI Images Using Massive Samples(landsat8建城区提取不同方法比较)

    感觉主要是数据的创新,方法就是比较了传统方法(RF,SVM,Adaboost)和CNN,输入比较了单像素输入和像素周围3,5,7大小的范围.也不是语义分割,最基本的CNN,单像素时还用的1*1的卷积 ...

  3. SpringMVC从Request域中获取数据

    SpringMVC从Request域中获取数据的三种方式 SpringMVC环境自行搭建, 约定存在如下目录和文件:/WEB-INF/pages/success.jsp 方式一:传入Model对象 前 ...

  4. kubernetes(K8S)集群及Dashboard安装配置

    环境准备 机器信息 主机名 操作系统 IP地址 K8sm-218 Centos 7.5-x86_64 172.17.0.218 k8s-219 Centos 7.5-x86_64 172.17.0.2 ...

  5. ES6——class类继承(读书笔记)

    前言 我一定是一个傻子,昨天这篇文章其实我已经写好了一半了,但是我没有保存 这是学习ES6的过程,我没有系统的看完阮大大的书.零零散散的,很多功能知道,但是没有实际的用过 看了几遍,总是看前面几章,所 ...

  6. python技巧获取26个英语字母

    import string string.ascii_uppercase # 获取26个大写字母 string.ascii_lowercase # 获取26个小写字母 string.ascii_let ...

  7. 简要介绍 X Window System (又称为X11 or X)

    X11是一个应用程序,而不是一个操作系统,主要功能是提供图形界面,实现架构和网络架构相同,有X Client和X Server组件,另外还有Window Manager和Display Manager ...

  8. 根据udev的信息判断设备物理路径

    udev会生成by-path路径,根据这个就可以判断 dev目录下 [toybrick@localhost dev]$ find | grep platform-fe3c0000 ./disk/by- ...

  9. c++异常——学习笔记

    1.异常 throw抛出字符串 最好的是:throw抛出对象. catch(...){} 2.使用标准异常类 #include<new> bitset 自己写一个异常 设计自己异常类 堆栈 ...

  10. typescript属性类型接口

    /* typeScript中的接口 - 1.属性类接口 */ /* 接口的作用:在面向对象的编程中,接口是一种规范的定义,它定义了行为和动作的规范,在程序设计里面,接口起到一种限制和规范的作用.接口定 ...