原题链接

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

解法:广度优先搜索。从选中的这个点开始,往周围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. POJ 1012 Joseph 推导,暴力,约瑟夫环,打表 难度:2

    http://poj.org/problem?id=1012 答案以954ms飘过,不过这道题可以轻松用打表过 思路:如果我们把每个人位于数组中的原始编号记为绝对编号,每次循环过后相对于绝对编号为0的 ...

  2. JavaScript数字精度上代码。

    /**不能超过 9007199254740992 * floatObj 包含加减乘除四个方法,能确保浮点数运算不丢失精度 * * 我们知道计算机编程语言里浮点数计算会存在精度丢失问题(或称舍入误差), ...

  3. invalid types 'int[int]' for array subscrip

    定义重复 如 一个int r 与一个 r[i] 重复

  4. SQL Server 索引介绍

    数据库索引是对数据表中一个或多个列的值进行排序的结构,就像一本书的目录一样,索引提供了在行中快速查询特定行的能力 详细出处参考:http://www.jb51.net/article/30950.ht ...

  5. idea使用generator自动生成model、mapper、mapper.xml(转)

    原文链接:http://www.mamicode.com/info-detail-445217.html TEP 0.在Intellij IDEA创建maven项目(本过程比较简单,略) STEP 1 ...

  6. ASP.NET MVC4 & Entity Framework 6.0 IIS 部署出错解决方案

    博客地址 http://blog.csdn.net/foxdave 近期了解MVC4的时候弄了一个简单的小工程,使用Entity Framework作为Model,F5启动调试运行的时候没有问题,但是 ...

  7. C# CsvFile 类

    using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Tex ...

  8. cometd的js端代码

    一:js端使用方式 CometD JavaScript的配置.整个API可以通过一个单一的原型名为org.cometd.Cometd的对象来调用.Dojo工具包中有一个名称为dojox.cometd的 ...

  9. Spring MVC配置文件解释

    <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...

  10. GPRS Sniffing Tutorial

    - Download sources into ~/gprs_sniffer git clone git://git.osmocom.org/osmocom-bb.git git clone git: ...