原题链接

题目大意:鼠标点击一块,求与之联通的所有区域的边长之和。

解法:广度优先搜索。从选中的这个点开始,往周围8个点依次搜索,访问过的点做上标记。如果该点上下左右的一个或多个方向没有相邻的点,边长+1。代码BSF函数中,有两个数组存放相邻8个点的坐标,这一段代码感觉很简洁,是从其他地方学习来的。

参考代码:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std; int r,c,x,y,i,j,ans;
int visited[22][22],grid[22][22];
char s;
int BFS(int i,int j); struct Node{
int x,y;
}p,q; int main(){ while(cin>>r>>c>>x>>y){
if(r==0&&c==0&&x==0&&y==0)break;
memset(grid,0,sizeof(grid));
memset(visited,0,sizeof(visited));
ans=0;
for(i=1;i<=r;i++){
for(j=1;j<=c;j++){
cin>>s;
if(s=='X')grid[i][j]=1;
if(s=='.')grid[i][j]=0;
}
getchar();
} //first fill the matrix with 0 or 1
ans=BFS(x,y); // do broad first search in this matrix
cout<<ans<<endl;
} return 0;
} int BFS(int i,int j){
int k,num,x0,y0;
int sx[9]={0,-1,1,0,0,-1,-1,1,1};
int sy[9]={0,0,0,-1,1,-1,1,-1,1};
queue<Node> point; p.x=i;
p.y=j;
point.push(p); //push the first node in queue
visited[x][y]=1; while(!point.empty()){ //if the queue is not empty, pop the front, visit it and push its neighbours
q=point.front();
point.pop();
num=0;
for(k=1;k<=8;k++){
x0=q.x+sx[k];
y0=q.y+sy[k];
if(grid[x0][y0]){
if(k<=4)num++; //another 'X' is next to this side
if(!visited[x0][y0]){
visited[x0][y0]=1;
p.x=x0;
p.y=y0;
point.push(p);
}
} //until the queue is empty
}
ans+=4-num;
}
return ans;
}

ZOJ 1047 Image Perimeters的更多相关文章

  1. POJ题目细究

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

  2. 【转】POJ百道水题列表

    以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight ...

  3. ZOJ People Counting

    第十三届浙江省大学生程序设计竞赛 I 题, 一道模拟题. ZOJ  3944http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=394 ...

  4. ZOJ 3686 A Simple Tree Problem

    A Simple Tree Problem Time Limit: 3 Seconds      Memory Limit: 65536 KB Given a rooted tree, each no ...

  5. BZOJ 1047 二维单调队列

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1047 题意:见中文题面 思路:该题是求二维的子矩阵的最大值与最小值的差值尽量小.所以可以考 ...

  6. ZOJ Problem Set - 1394 Polar Explorer

    这道题目还是简单的,但是自己WA了好几次,总结下: 1.对输入的总结,加上上次ZOJ Problem Set - 1334 Basically Speaking ac代码及总结这道题目的总结 题目要求 ...

  7. ZOJ Problem Set - 1392 The Hardest Problem Ever

    放了一个长长的暑假,可能是这辈子最后一个这么长的暑假了吧,呵呵...今天来实验室了,先找了zoj上面简单的题目练练手直接贴代码了,不解释,就是一道简单的密文转换问题: #include <std ...

  8. ZOJ Problem Set - 1049 I Think I Need a Houseboat

    这道题目说白了是一道平面几何的数学问题,重在理解题目的意思: 题目说,弗雷德想买地盖房养老,但是土地每年会被密西西比河淹掉一部分,而且经调查是以半圆形的方式淹没的,每年淹没50平方英里,以初始水岸线为 ...

  9. ZOJ Problem Set - 1006 Do the Untwist

    今天在ZOJ上做了道很简单的题目是关于加密解密问题的,此题的关键点就在于求余的逆运算: 比如假设都是正整数 A=(B-C)%D 则 B - C = D*n + A 其中 A < D 移项 B = ...

随机推荐

  1. Mongoose全面理解

    一.创建schemas 创建schemas的方式: 1 var userSchema = new mongoose.Schema({ 2 name: String, 3 email: String, ...

  2. 数列F[19] + F[13]的值

    已知数列如下:F[1]=1, F[2]=1, F[3]=5,......,F[n] =F[n-1] + 2*F[n-2],求F[19] + F[13]? #include <stdio.h> ...

  3. [开发笔记]-flowplayer视频播放插件

    最近项目中需要添加播放视频的功能,视频文件是flv格式的.在网上找了一些jQuery视频播放插件,还是觉得“flowplayer”要好一些.特将使用方法记录一下. flowplayer也有html5版 ...

  4. java基础之 http

    HTTP(HyperText Transfer Protocol)是一套计算机通过网络进行通信的规则.计算机专家设计出HTTP,使HTTP客户(如Web浏览器)能够从HTTP服务器(Web服务器)请求 ...

  5. eclipse web项目实际工程路径对应

    src/1.properties ---->实际路径 /WEB-INF/classes/1.propertiessrc/com.ayong.one/2.properties /WEB-INF/c ...

  6. greenDao 3.0基础

    引入greenDao3.0 首先在project的gradle文件中引入greenDAO插件 dependencies {       classpath 'com.android.tools.bui ...

  7. C#中的五个访问修饰符

    一.public, private, protected, internal, protected internal 1.public : 公开的, 公共的 2.private : 私有的 (只能在当 ...

  8. 3D中的切线空间简介

    转自:http://www.cnblogs.com/cxrs/archive/2009/10/25/1589515.html 1. 什么是Tangent space? Tangent space和wo ...

  9. 使用struts2的<s>标签出错

    15:org.apache.struts2.views.jsp.ActionTag 16:JSP 17:18:19:executeResult Server: Resin/3.1.4a Content ...

  10. linux常用命令:5网络命令

    网络命令 1. 指令名称:write 指令所在路径:/usr/bin/write 执行权限:所有用户 语法:write  <用户名> 功能描述:给用户发送信息,以Ctrl+D保存结束