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. js 的数组怎么push一个对象. Js数组的操作push,pop,shift,unshift JavaScrip

    push()函数用于向当前数组的添加一个或多个元素,并返回新的数组长度.新的元素将会依次添加到数组的末尾. 该函数属于Array对象,所有主流浏览器均支持该函数. 语法 array.push( ite ...

  2. 对象+String

    public class Fruit { public String toString() { return "Fruit toString."; } public static ...

  3. [FUZZ]文件上传fuzz字典生成脚本—使用方法

    文件上传fuzz字典生成脚本-使用方法 原作者:c0ny1 项目地址:https://github.com/c0ny1/upload-fuzz-dic-builder 项目预览效果图: 帮助手册: 脚 ...

  4. spaceclaim脚本(内摆线)

    import math #导入数学模块,因为会使用π def x_comp(k,r,t): #定义x坐标的计算函数 return r * (k -1) * math.cos(t) + r * math ...

  5. 利用Wireshark抓取并分析OpenFlow协议报文

    OpenFlow 交换机与控制器交互步骤 1. 利用Mininet仿真平台构建如下图所示的网络拓扑,配置主机h1和h2的IP地址(h1:10.0.0.1,h2:10.0.0.2),测试两台主机之间的网 ...

  6. SpringBoot配置虚拟化路径用于图片的展示

    前言:springboot默认可以访问resources下的static文件夹下的静态资源,我们一般将图片指定上传到static下的某个文件夹,例如images,开发阶段可以使用,但是当项目打成jar ...

  7. DELPHI LINUX中间件隆重发布

    LINUX和WINDOWS是目前最流利的两个服务器操作系统,各种云服务商也是同时提供这2种操作系统的云服务器. 即日起,在原有WINDOWS中间件的产品基础上,再隆重推出新产品:LINUX中间件.这样 ...

  8. How to receive JSON as an MVC 5 action method parameter

    How to receive JSON as an MVC 5 action method parameter  解答1 Unfortunately, Dictionary has problems ...

  9. 对实体 "useSSL" 的引用必须以 ';' 分隔符结尾。 Nested exception: 对实体 "useSSL" 的引用必须以 ';' 分隔符结尾

    今天在定义数据源的时候,在配置context.xml文件时,连接mysql数据库的url一行发生错误,报错:“对实体 "useSSL" 的引用必须以 ';' 分隔符结尾”.以下是我 ...

  10. HTTPWebrequest上传文件--Upload files with HTTPWebrequest (multipart/form-data)

    使用HTTPWebrequest上传文件遇到问题,可以参考Upload files with HTTPWebrequest (multipart/form-data)来解决 https://stack ...