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: ...
随机推荐
- hdu5293 lca+dp+树状数组+时间戳
题意是给了 n 个点的树,会有m条链条 链接两个点,计算出他们没有公共点的最大价值, 公共点时这样计算的只要在他们 lca 这条链上有公共点的就说明他们相交 dp[i]为这个点包含的子树所能得到的最 ...
- 即时通信系统中实现全局系统通知,并与Web后台集成【附C#开源即时通讯系统(支持广域网)——QQ高仿版IM最新源码】
像QQ这样的即时通信软件,时不时就会从桌面的右下角弹出一个小窗口,或是显示一个广告.或是一个新闻.或是一个公告等.在这里,我们将其统称为“全局系统通知”.很多使用C#开源即时通讯系统——GGTalk的 ...
- 【CDH学习之一】CDH简介
环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4 jdk8 zookeeper-3.4.11 一.CDH在商业应用中,对 ...
- QString 与 string转换
[1]QString 转换为string QString qString("好好学习天天向上"); std::string stdString = qString.toStdStr ...
- nextjs 服务端渲染请求参数
Post.getInitialProps = async function (context) { const { id } = context.query const res = await fet ...
- bzoj2049 [Sdoi2008]Cave 洞穴勘测 link cut tree入门
link cut tree入门题 首先说明本人只会写自底向上的数组版(都说了不写指针.不写自顶向下QAQ……) 突然发现link cut tree不难写... 说一下各个函数作用: bool isro ...
- [转载]CSS教程--字体与文本属性
b>font-family功能:用于改变HTML标志或元素的字体,你可设置一个可用字体清单.浏览器由前向后选用字体.语法:{font-family:字体1,字体2, ... ,字体n} font ...
- scrapy 去重 dont_filter=False
yield Request(...... dont_filter=False)
- 理解 neutron
之前大师发个结构图. understanding_neutron.pdf 自己走读了代码: 1. get_extensions_path() # 在/opt/stack/neutron/neutro ...
- oracle基础——内存管理、优化
内存图解: 自动管理:11g:AMM 10g:ASMM SGA(system global area):由所有服务进程和后台进程共享 PGA(program global area): 由每个服务 ...