Day2-K-Red and Black-HDU1312
Write a program to count the number of black tiles which he can reach by repeating the moves described above.
InputThe input consists of multiple data sets. A data set starts with a line containing two positive integers W and H; W and H are the numbers of tiles in the x- and y- directions, respectively. W and H are not more than 20.
There are H more lines in the data set, each of which includes W characters. Each character represents the color of a tile as follows.
'.' - a black tile
'#' - a red tile
'@' - a man on a black tile(appears exactly once in a data set)
OutputFor each data set, your program should output a line which contains the number of tiles he can reach from the initial tile (including itself).
Sample Input
6 9
....#.
.....#
......
......
......
......
......
#@...#
.#..#.
11 9
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
###.###
...@...
###.###
..#.#..
..#.#..
0 0
Sample Output
45
59
6
13 思路:比较简单的DFS求连通,读入时找到起点即可,代码如下:
const int maxm = ;
const int dx[] = {-, , , };
const int dy[] = {, , -, }; int m, n, sx, sy, vis[maxm][maxm], t;
char buf[maxm][maxm]; bool inside(int x,int y) {
return x >= && x < m && y >= && y < n;
} void dfs(int x,int y) {
if(vis[x][y])
return;
vis[x][y] = ;
for (int i = ; i < ; ++i) {
int nx = x + dx[i], ny = y + dy[i];
if(inside(nx,ny) && buf[nx][ny] == '.' &&!vis[nx][ny]) {
++t;
dfs(nx, ny);
}
}
} int main() {
while(scanf("%d%d",&n,&m) && m + n) {
getchar();
for (int i = ; i < m; ++i) {
for (int j = ; j < n; ++j) {
scanf("%c", &buf[i][j]);
if(buf[i][j] == '@') {
sx = i, sy = j;
}
}
getchar();
}
memset(vis,,sizeof(vis));
t = ;
dfs(sx, sy);
printf("%d\n", t);
}
return ;
}
Day2-K-Red and Black-HDU1312的更多相关文章
- 计蒜客 Red Black Tree(树形DP)
You are given a rooted tree with n nodes. The nodes are numbered 1..n. The root is node 1, and m of ...
- 用php随机生成福彩双色球号码的2种方法
不瞒您说,俺也是个双色球爱好者,经常买,但迟迟没有中过一等奖,哈哈.这里为大家介绍用php随机生成福彩双色球号码的二种方法,供朋友们学习参考.新的一年,祝大家中大奖,发大财. 方法一 复制代码代码如下 ...
- switch语句的使用,非常好
这是谭浩强课本上枚举类型的例子,但是我贴这个例子的代码不是因为枚举类型,是因为这个代码使用switch语句用得非常好,值得一贴. 题目是这样的:有红.黄.蓝.白.黑5中颜色的球若干,依次取出3个球,求 ...
- css3绘制中国结
<!doctype html> <html> <head> <title></title> <meta charset='utf-8' ...
- php随机生成福彩双色球号码
发布:thebaby 来源:net [大 中 小] 不瞒您说,俺也是个双色球爱好者,经常买,但迟迟没有中过一等奖,哈哈.这里为大家介绍用php随机生成福彩双色球号码的二种方法,供朋友们学习 ...
- NOI 2013 书法家
http://uoj.ac/problem/125 我真是日狗了...... 果然还是没有耐心读题,搞到读题读错了2个地方,结果调试了半天...... 言归正传. 动态规划. 这种题目很常见. 我们发 ...
- NOI2015
D1T1 并查集. #include<cstdio> #include<cstdlib> #include<iostream> #include<fstrea ...
- 基础dp
队友的建议,让我去学一学kuangbin的基础dp,在这里小小的整理总结一下吧. 首先我感觉自己还远远不够称为一个dp选手,一是这些题目还远不够,二是定义状态的经验不足.不过这些题目让我在一定程度上加 ...
- 【C++/函数】实验2
1.实验内容 函数声明和函数定义: 形参和实参: 主调函数把实参的值传送给被调函数的形参从而实现主调函数向被调函数的数据传送. 形参:形式参数,表示主调函数与被调函数之间的联系.标识了在形参出现的位置 ...
- 实验一:C++简单程序设计
[实验一] #2-28 实现一个简单的菜单程序,运行时显示“Menu:A(dd) D(elete) S(ort) Q(uit),Selete one:”提示用户输入.A表示增加,D表示删除,S表示排序 ...
随机推荐
- Hibernate学习(六)
Hibernate的三种查询方式 1.Criteria 查询 ,Query By Criteria ( QBC )JPA 规范中定义的一种查询方法,但是不推荐使用 2.HQL : Hibernate ...
- 转专业后补修C语言的一些体会(3)
1.指针:指针是C语言最为强大的工具之一,有着很多优点,比如可以改善子程序的效率,为动态数据结构提供支持,为C的动态内存分配系统提供支持,为函数提供修改变量值的手段.但指针的使用十分困难.会出现很多意 ...
- Postgresql查询表和表结构
查询表名 SELECT tablename FROM pg_tables WHERE tablename NOT LIKE 'pg%' AND tablename NOT LIKE 'sql_%' O ...
- pip源、搭建虚拟环境、git
一.pip源 1.1 介绍 1.采用国内源,加速下载模块的速度2.常用pip源:-- 豆瓣:https://pypi.douban.com/simple-- 阿里:https://mirrors.al ...
- 学习笔记(5)- ubuntu对话语料
The Ubuntu Dialogue Corpus: A Large Dataset for Research in Unstructured Multi-Turn Dialogue Systems ...
- yp寒假训练一
19年东北四省省赛 做了J G C 补了E H J签到题 G 题意: 给n个正方形的两个斜对角点坐标,问最小的移动可以重叠(移动上下左右一格) 思路: 一开始想的是中心pos移动,但是可能有小数,而且 ...
- Django 学习之Django Rest Framework(DRF)
一. WEB应用模式 在开发Web应用中,有两种应用模式 1. 前后端不分离 把html模板文件和django的模板语法结合渲染完成以后才从服务器返回给客户. 2. 前后端分离 二. API接口 AP ...
- .net core 2.1控制台使用Quartz.net实现定时任务执行
权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/qq_33435149/article/de ...
- EOS主网搭建教程--&&--搭建节点--&&--搭建mongodb数据库
EOS主网搭建教程: 1.git clone https://github.com/EOS-Mainnet/eos.git --recursive 2.cd eos 3.git tag (查看有哪些分 ...
- 关于html的基本知识
先上基本介绍图 HTML 简介 超文本标记语言 Hyper Text Markup Language 什么是标签(大多数标签) 尖括号包围.成对出现 开始标签,结束标签 元素 编辑器 HBuilder ...