Balloons
题目链接: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的更多相关文章
- [LeetCode] Minimum Number of Arrows to Burst Balloons 最少数量的箭引爆气球
There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...
- [LeetCode] Burst Balloons 打气球游戏
Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by ...
- 第一届山东省ACM——Balloons(java)
Description Both Saya and Kudo like balloons. One day, they heard that in the central park, there wi ...
- Balloons(山东省第一届ACM省赛)
Balloons Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 Both Saya and Kudo like balloons ...
- [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 ...
- LeetCode Burst Balloons
原题链接在这里:https://leetcode.com/problems/burst-balloons/ 题目: Given n balloons, indexed from 0 to n-1. E ...
- Leetcode: Minimum Number of Arrows to Burst Balloons
There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...
- codeforces 725D . Contest Balloons(贪心+优先队列)
题目链接:codeforces 725D . Contest Balloons 先按气球数从大到小排序求出初始名次,并把名次排在第一队前面的队放入优先队列,按w-t-1值从小到大优先,然后依次给气球给 ...
- Burst Balloons
Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by ...
- sdutoj 2152 Balloons
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2152 Balloons Time Limit: ...
随机推荐
- 即时通信系统中实现全局系统通知,并与Web后台集成【附C#开源即时通讯系统(支持广域网)——QQ高仿版IM最新源码】
像QQ这样的即时通信软件,时不时就会从桌面的右下角弹出一个小窗口,或是显示一个广告.或是一个新闻.或是一个公告等.在这里,我们将其统称为“全局系统通知”.很多使用C#开源即时通讯系统——GGTalk的 ...
- 【2017-03-02】C#函数,递归法
函数 函数的意义:独立完成某项功能的个体 函数的优势:1.提高代码的重用性 2.提高功能开发的效率 3.提高程序代码的可维护性 函数四要素: 1,输入:(值的类型+名称) 2,输出:ret ...
- Linux Firewall 开启与关闭 以及sudo 设置
Linux 系统下,普通用户经常需要使用root 用户的权限,所以要经常切换到root用户,比较麻烦,因此可以给普通用户添加root 权限,需要在常规命令前面加上sudo 切换到root vi /e ...
- Gamma函数深入理解
Gamma函数 当n为正整数时,n的阶乘定义如下:n! = n * (n - 1) * (n - 2) * … * 2 * 1. 当n不是整数时,n!为多少?我们先给出答案. 容易证明,Γ(x + 1 ...
- L1正则和L2正则的比较分析详解
原文链接:https://blog.csdn.net/w5688414/article/details/78046960 范数(norm) 数学上,范数是一个向量空间或矩阵上所有向量的长度和大小的求和 ...
- ADC采样对MIC的干扰
使用EFM8SB的MCU做ADC采样,检测MIC Bias的电源,解码android耳机的线控.初始时, ADC的采样频率2KHz, 在录音时后播放,能固定的听到接近2K的一个噪声.示波器探测不到明显 ...
- 现代汽车加入Linux 基金会和 AGL协作平台
1月4日,现代汽车宣布已加入 Linux 基金会和其旗下的非营利协作平台 Automotive Grade Linux(AGL),现代汽车公司副总裁兼信息娱乐技术中心负责人 Paul Choo 表示: ...
- Centos7 安装 apache + php7.0 环境
安装apache rpm -qa|grep httpd 查看是否安装 yum install httpd 安装 service httpd start 启动服务 测试是否 启动 I ...
- FastStone Capture无法录制系统声音解决方法(win10)
步骤一: 右键桌面-->个性化 步骤二: 主题-->高级声音设置 步骤三: 录音-->右键“立体声混音”,单击启用就OK了.
- MD5加密算法Java代码实现
package com.app.utils; import java.math.BigInteger; import java.security.MessageDigest; import java. ...