Image Perimeters
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的更多相关文章
- ZOJ 1047 Image Perimeters
原题链接 题目大意:鼠标点击一块,求与之联通的所有区域的边长之和. 解法:广度优先搜索.从选中的这个点开始,往周围8个点依次搜索,访问过的点做上标记.如果该点上下左右的一个或多个方向没有相邻的点,边长 ...
- B - Image Perimeters
Technicians in a pathology lab analyze digitized images of slides. Objects on a slide are selected f ...
- 深搜(DFS),Image Perimeters
题目链接:http://poj.org/problem?id=1111 解题报告: 1.这里深搜有一点要注意,对角线上的点,如果为'.',则total不应该增加,因为这不是他的边长. #include ...
- POJ1111 Image Perimeters
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 8790 Accepted: 5260 Description Techn ...
- [POJ]1111 Image Perimeters
Description Technicians in a pathology lab analyze digitized images of slides. Objects on a slide ar ...
- poj1111 Image Perimeters 广搜
题目大意: 输入一个矩阵,再输入其中一个“X”的位置(从1开始).从该位置向八个方向扩展,如果是“X”就可以并在一起.问最后得到的模块的周长是多少. 解题思路: 按照广搜的思路来做.用一个二维的数组标 ...
- Project Euler 94:Almost equilateral triangles 几乎等边的三角形
Almost equilateral triangles It is easily proved that no equilateral triangle exists with integral l ...
- POJ题目细究
acm之pku题目分类 对ACM有兴趣的同学们可以看看 DP: 1011 NTA 简单题 1013 Great Equipment 简单题 102 ...
- HOJ题目分类
各种杂题,水题,模拟,包括简单数论. 1001 A+B 1002 A+B+C 1009 Fat Cat 1010 The Angle 1011 Unix ls 1012 Decoding Task 1 ...
随机推荐
- CODE FESTIVAL 2016 qual B题解
传送门 \(A\) 什么玩意儿-- const char t[]={"0CODEFESTIVAL2016"}; char s[25];int res; int main(){ sc ...
- LOJ2327 「清华集训 2017」福若格斯 【不平等博弈】
题目链接:LOJ 对于这道题,我们要分3步来做它. 什么是 Surreal Number 及如何解决博弈问题. 如何用 Surreal Number 解决这道题. 推出结论之后如何计数 首先看看这篇文 ...
- Pytest权威教程10-捕获警告信息
目录 捕获警告信息 @pytest.mark.filterwarnings 禁用警告摘要 完全禁用警告捕获 弃用警告和待命记录警告 确保代码触发弃用警告 用警告函数断言警告 录制警告 自定义失败消息 ...
- Android中进度条
<ProgressBar android:id="@+id/progress_bar" android:layout_width="match_parent&quo ...
- Windows下基于IIS服务的SSL服务器的配置
Windows下基于IIS服务的SSL服务器的配置 实验环境 Windows Server 2008 R1(CA) Windows Server 2008 R2(web服务器) Windows 7 x ...
- ubuntu之路——day7.2 regularization
所有的正则化方法来自于吴恩达老师的免费公开课:https://mooc.study.163.com/learn/2001281003?tid=2001391036#/learn/content?typ ...
- Assignment4:闰年判断输入异常时的处理方法
一.问题描述 在输入界面输入年份,界面返回是否为闰年. 判断依据为:输入的数字可以被4整除但不可以被100整除 || 输入的数字可以被400整除 如果输入为数字以外的其他字符,会抛出异常.那么如何防止 ...
- python No module named 'urlparse'
python3中,取消了urlparse 引用方式改为了: from urllib import parse
- NGUI: Next-Gen UI 2018.3.0f
https://assetstore.unity.com/packages/tools/gui/ngui-next-gen-ui-2413 NGUI is a very powerful UI sys ...
- Java_jdbc 基础笔记之一 数据库连接
方式一: 1.创建一个Driver实现类的对象 2.准备连接数据库的基本信息:url,user,password 3.调用Driver接口的connect(url,info)获取数据库连接 * Dri ...