题目链接: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. FileInputStream FileOutputStream

    FileInputStream is a stream to grab the information from files.Combined with FileOutputStream, we ca ...

  2. 基于FPGA摄像头图像采集显示系统

    本系统主要由FPGA主控模块.图像采集模块.图像存储模块以及图像显示模块等模块组成.其中图像采集模块选择OV7670摄像头模块,完成对视频图像的采集和解码功能,并以RGB565标准输出RGB 5:6: ...

  3. 吴恩达讲了干货满满的一节全新AI课,全程手写板书充满诚意非常干货

    吴恩达讲了干货满满的一节全新AI课,全程手写板书充满诚意非常干货 摘要: 目前,AI技术做出的经济贡献几乎都来自监督学习,也就是学习从A到B,从输入到输出的映射.现在,监督学习.迁移学习.非监督学习. ...

  4. linux dns

    linux 用户相关的 root   相当于QQ群主 sudo  QQ群管理员 普通用户  QQ群水军 root  UID 是 0   组UID也是0  普通用户UID从1000开始 查看用户id 信 ...

  5. Django ORM 操作 必知必会13条 单表查询

    ORM 操作 必知必会13条 import os # if __name__ == '__main__': # 当前文件下执行 os.environ.setdefault('DJANGO_SETTIN ...

  6. JustOj 1929: 多输入输出练习1

    题目描述 给定很多行数据,要求输出每一行的最大值. 输入 程序有多行输入,每一行以0结束. 输出 有多行输出,对应输入的行数. 样例输入 23 -456 33 78 0 43 23 987 66 -1 ...

  7. 安装DotNetCore.1.0.0-VS2015Tools.Preview2一直失败,如何解?

    首先要说明的一点是,本地的VS2015的环境已经安装完成,而且vs2015.3也已经更新完成了,这个环境应该是没啥问题.但是安装.DotNetCore.1.0.0-VS2015Tools.Previe ...

  8. 2019/3/25 wen 包,对象的行为

  9. MySQL实现排名并查询指定用户排名功能

    表结构: CREATE TABLE test.testsort ( id ) NOT NULL AUTO_INCREMENT, uid ) COMMENT '用户id', score , ) DEFA ...

  10. postman5.0.2_0+postmanInterceptor0.2.22_0下载安装,可发送header头 cookie 参数

    Postman是chrome上一个非常好用的http客户端插件,可惜由于chrome安全的限制,发不出带cookie的请求.如果想要发送带cookie的请求,需要开启Interceptor 安装方法: ...