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: ...
随机推荐
- Sitecore xDB基础知识 - 识别用户,联系人,访客,客户
体验数据库(xDB)是Sitecore平台的关键元素,特别是当您希望将解决方案提升到简单的内容管理要求之外时.它用于跟踪您的用户(即联系人,访客,客户)与您网站的互动方式.营销人员可以使用此数据来了解 ...
- Vim for Windows --ctags
What is ctags? ctags -- Generate tag files for source code,which is a tool used for facilitating rea ...
- flask 单个表单多个提交按钮
单个表单多个提交按钮 在某些情况下,可能需要为一个表单添加多个提交按钮.比如在创建文章的表单中添加发布按钮和存草稿的按钮.当用户提交表单时,需要在视图函数中根据按下的按钮来做出不同的处理. 下面例子中 ...
- 典型 python 小练习
#格式化输出 3方式a=input('user:').strip()print('%s'%a) #%s 占位符a1=[1,2,3]print(f'333{a1}早') #法二print('ss{0}k ...
- how to backup your system of Autel MS908 Pro
how to backup your system of Autel Scan Tool Autel MS908 Pro: Connect the tablet to a PC desktop or ...
- String小案例(**)、包装类型和普通数据类型的转换(拆装箱)
###String用法: package StringTest; /**功能: * 判断Java文件名是否正确,判断邮箱格式是否正确 * 其中:合法的文件名应该以.java结尾 * 合法的邮箱名至少包 ...
- Java Thread.yield详解
这是Java中的一种线程让步方法,让Java中的线程从执行状态变成就绪状态,然后处理器再从就绪队列中挑选线程进行执行(优先级大的,被挑选的概率较大),这种转换也不确定,让或者不让都是取决与处理器,线程 ...
- Vue小案例 之 商品管理------删除商品与提示
实现删除商品功能 根据索引来进行删除商品: 实现删除商品的HTML: <!--显示表格--> <div class="table-warp"> <di ...
- bzoj2595 / P4294 [WC2008]游览计划
P4294 [WC2008]游览计划 斯坦纳树 斯坦纳树,是一种神奇的树.它支持在一个连通图上求包含若干个选定点的最小生成树. 前置算法:spfa+状压dp+dfs(大雾) 我们设$f[o][P]$为 ...
- 面试题:JS中map的陷阱
题目: ['2', '3', '4'].map(parseInt); 请说出上面代码的执行结果 错误回答: [2, 3, 4] 真正答案: [2, NaN, NaN] 解析: 因为 map 的算子是有 ...