题目链接:http://acm.sdibt.edu.cn/JudgeOnline/problem.php?id=2401

类似求连通块的问题,可以参考紫书(P162 油田),对这两个人分别执行dfs。

解题的时候应该好好分析一下给出的式子,看是否能将代数式映射到几何中,这个题第一个人在几何中的表示就是寻找上下左右四个方向的连通块,第二个人时八个方向的,找出这个关系后就好做了,定义一个方向数组,然后进行dfs深搜,记录连通块的数量就是本题答案。

注意题目的坑点,输出结果后面空一个空格!

AC代码:

#include<cstdio>
#include<cstring> //memset函数的头文件
using namespace std; const int maxn = 110; bool vis[maxn][maxn];
char maze[maxn][maxn];
int n,ind; int go[8][2] = {-1,0,1,0,0,-1,0,1,-1,1,1,1,1,-1,-1,-1}; bool dfs(int x,int y) {
if(x<0 || x>=n || y<0 || y>=n)
return false;
if(vis[x][y] || maze[x][y] == '0')
return false;
vis[x][y] = true;
int temp = (ind == 1 ? 4 : 8);
for(int i = 0;i < temp;i++)
dfs(x+go[i][0],y+go[i][1]);
return true;
} int main() {
int flag = 0;
int cnt1,cnt2;
while((scanf("%d",&n) != EOF) && (n != 0)) {
cnt1 = cnt2 = 0;
getchar();
for(int i = 0;i < n;i++) {
scanf("%s",maze[i]);
}
ind = 1;
memset(vis,false,sizeof(vis));
for(int i = 0;i < n;i++) {
for(int j = 0;j < n;j++) {
if(dfs(i,j))
cnt1++;
}
}
memset(vis,false,sizeof(vis));
ind = 2;
for(int i = 0;i < n;i++) {
for(int j = 0;j < n;j++) {
if(dfs(i,j))
cnt2++;
}
}
printf("Case %d: %d %d\n\n",++flag,cnt1,cnt2);
}
return 0;
}

来源:山东省第一届ACM程序设计大赛2010.10.3

Balloons的更多相关文章

  1. [LeetCode] Minimum Number of Arrows to Burst Balloons 最少数量的箭引爆气球

    There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...

  2. [LeetCode] Burst Balloons 打气球游戏

    Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by ...

  3. 第一届山东省ACM——Balloons(java)

    Description Both Saya and Kudo like balloons. One day, they heard that in the central park, there wi ...

  4. Balloons(山东省第一届ACM省赛)

    Balloons Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 Both Saya and Kudo like balloons ...

  5. [LeetCode] 452 Minimum Number of Arrows to Burst Balloons

    There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...

  6. LeetCode Burst Balloons

    原题链接在这里:https://leetcode.com/problems/burst-balloons/ 题目: Given n balloons, indexed from 0 to n-1. E ...

  7. Leetcode: Minimum Number of Arrows to Burst Balloons

    There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...

  8. codeforces 725D . Contest Balloons(贪心+优先队列)

    题目链接:codeforces 725D . Contest Balloons 先按气球数从大到小排序求出初始名次,并把名次排在第一队前面的队放入优先队列,按w-t-1值从小到大优先,然后依次给气球给 ...

  9. Burst Balloons

    Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by ...

  10. sdutoj 2152 Balloons

    http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2152 Balloons Time Limit: ...

随机推荐

  1. 20155228 2016-2017-2 《Java程序设计》第3周学习总结

    20155228 2016-2017-2 <Java程序设计>第3周学习总结 教材学习内容总结 认识对象 类与对象 类和对象的关系:类是对象的设计图,对象是类的实例 参考:将"名 ...

  2. Quick-Cocos2d-x 新建项目

    开发工具准备就绪以后,下面我们就可以开始创建我们的项目了. 首先启动 Quick 下的 player3,在这儿的示例标签下你可以看到很多Quick自带的示例,对于初学者来说,看看这些示例的使用方法会对 ...

  3. 【CDH学习之三】CDH安装

    登录CM 1.版本选择 免费版本的CM5已经去除50个节点数量的限制. 各个Agent节点正常启动后,可以在当前管理的主机列表中看到对应的节点. 选择要安装的节点,点继续. 接下来,出现以下包名,说明 ...

  4. 利用QPainter绘制散点图

    [1]实例代码 (1)代码目录结构(备注:QtCreator默认步骤新建工程) (2)工程pro文件 QT += core gui greaterThan(QT_MAJOR_VERSION, ): Q ...

  5. Jmeter自己jar包的引用

    1.编写清空指定文件夹里所有内容的jar包 package org.na;import java.io.File;public class deletedir {    public static b ...

  6. hive的find_in_set函数

    集合查找函数: find_in_set语法: find_in_set(string str, string strList) 返回值: int说明: 返回str在strlist第一次出现的位置,str ...

  7. Web 应用架构基础课(转载)

    Web 应用架构基础课 初级 web 应用开发者必学的基础网络架构概念 web 应用主流架构概览 上图便是我司(Storyblocks)网络架构的很好展现.如果你还没成为经验老道的 web 工程师,可 ...

  8. Android获取全局Context的方法

    Android获取全局Context的方法 Android--应用全局获取Context - 超宇的博客 - CSDN博客https://blog.csdn.net/chaoyu168/article ...

  9. CSS 选择器权重计算规则

    其实,CSS有自己的优先级计算公式,而不仅仅是行间>内部>外部样式:ID>class>元素. 一.样式类型 1.行间 <h1 style="font-size: ...

  10. scrapy 自定义图片路径保存,并存到数据库中

    scrapy中有个自带的pipeline工具,ImagesPipeline,可以专门用来储存图片到本地. 但默认储存地址无法配置,所以我们需要写一个自己的pipeline用于储存图片. 先分析一下我们 ...