red and black(BFS)
| Time Limit: 1000MS | Memory Limit: 30000K | |
| Total Submissions: 40685 | Accepted: 22079 |
Description
Write a program to count the number of black tiles which he can reach by repeating the moves described above.
Input
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)
The end of the input is indicated by a line consisting of two zeros.
Output
Sample Input
6 9
....#.
.....#
......
......
......
......
......
#@...#
.#..#.
11 9
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
###.###
...@...
###.###
..#.#..
..#.#..
0 0
Sample Output
45
59
6
13
Source
题解:简单的BFS
AC代码
1 #include<stdio.h>
2 #include<iostream>
3 #include<stdio.h>
4 #include<string>
5 #include<cmath>
6 #include<algorithm>
7 using namespace std;
8
9 char a[25][25];
10 int w, h;
11 int ant;
12
13 void dfs(int x, int y)
14 {
15 if(a[x][y] != '#' && x >= 0 && x < h && y >= 0 && y < w)
16 {
17 ant++;
18 a[x][y] = '#';
19 dfs(x+1, y);
20 dfs(x-1, y);
21 dfs(x, y+1);
22 dfs(x, y-1);
23 }
24 }
25
26 int main()
27 {
28 while(scanf("%d%d", &w, &h) != EOF)
29 {
30 ant = 0;
31 if(w == 0 && h == 0)
32 break;
33 for(int i = 0; i < h; i++)
34 {
35 for(int j = 0; j < w; j++)
36 {
37 cin >> a[i][j];
38 }
39 }
40 for(int i = 0; i < h; i++)
41 for(int j = 0; j < w; j++)
42 if(a[i][j] == '@')
43 dfs(i, j);
44 cout << ant << endl;
45 }
46 return 0;
47 }
red and black(BFS)的更多相关文章
- Red and Black(BFS or DFS) 分类: dfs bfs 2015-07-05 22:52 2人阅读 评论(0) 收藏
Description There is a rectangular room, covered with square tiles. Each tile is colored either red ...
- POJ 1979 Red and Black (BFS)
链接 : Here! 思路 : 简单的搜索, 直接广搜就ok了. /****************************************************************** ...
- HDU 1312 Red and Black --- 入门搜索 BFS解法
HDU 1312 题目大意: 一个地图里面有三种元素,分别为"@",".","#",其中@为人的起始位置,"#"可以想象 ...
- HDU 1312 Red and Black DFS(深度优先搜索) 和 BFS(广度优先搜索)
Red and Black Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...
- Red and Black(poj 1979 bfs)
Red and Black Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 27891 Accepted: 15142 D ...
- HDU 1312 Red and Black(bfs)
Red and Black Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Descr ...
- HDU 1312 Red and Black(bfs,dfs均可,个人倾向bfs)
题目代号:HDU 1312 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312 Red and Black Time Limit: 2000/100 ...
- B - Red and Black 直接BFS+队列
There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A ...
- hdu1312 Red and Black 简单BFS
简单BFS模版题 不多说了..... 直接晒代码哦.... #include<cstdlib> #include<iostream> #include<cstdio> ...
随机推荐
- 配置Nginx的坑及思路
我配置的是Django + uwsgi + Nginx 说下思路,先进行模块化测试: Django: Django 下 第一个坑是sql版本低问题,原因用pip安装不正确,在网上查了下按这个文章重装下 ...
- SpringBoot(七):SpringBoot中如何使用过滤器(Filter)?
方式一: 通过注解方式实现: 1.编写一个Servlet3的注解过滤器(和上一章Servlet相似) 贴代码: package com.example.springbootweb.filter; im ...
- Java方法详解
Java方法详解 什么是方法? Java方法是语句的集合,它们在一起执行一个功能. 方法是解决一类问题的步骤的有序组合 方法包含于类或对象中 方法在程序中被创建,在其他地方被引用 示例: packag ...
- CRLF注入原理
CRLF 指的是回车符(CR,ASCII 13,\r,%0d) 和换行符(LF,ASCII 10,\n,%0a),操作系统就是根据这个标识来进行换行的,你在键盘输入回车键就是输出这个字符,只不过win ...
- 【知识点】 gcc和g++的联系和区别
目前(2020-09)GCC 编译器已经更新至 10.2版本,其功能也由最初仅能编译 C 语言,扩增至可以编译多种编程语言,其中就包括 C++ . 除此之外,当下的 GCC 编译器还支持编译 Go.O ...
- 模式识别Pattern Recognition
双目摄像头,单目摄像头缺少深度 Train->test->train->test->predicive
- WeihanLi.Npoi 1.16.0 Release Notes
WeihanLi.Npoi 1.16.0 Release Notes Intro 最近有网友咨询如何设置单元格样式,在之前的版本中是不支持的,之前主要考虑的是数据,对于导出的样式并没有支持,这个 is ...
- 前端学习 node 快速入门 系列 —— 初步认识 node
其他章节请看: 前端学习 node 快速入门 系列 初步认识 node node 是什么 node(或者称node.js)是 javaScript(以下简称js) 运行时的一个环境.不是一门语言. 以 ...
- python工业互联网应用实战8—django-simpleui
笔者也使用过一段时间adminx组件,后来由于adminx停更,又遇到更简单的django-simpleui后,现在基本上只使用simpleui了,使用simpleui的几个好处,笔者认为排在第一位的 ...
- 下载微软pdb符号文件
使用symchk.exe 逐层下载c:\windows\system32下的pdb文件 symchk /r c:\windows\system32 /s SRV*D:\mypdb\*http://m ...