HDOJ1312 Red and black(DFS深度优先搜索)
There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A man is standing on a black tile. From a tile, he can move to one of four adjacent tiles. But he can't move on red tiles, he can move only on black tiles.
Write a program to count the number of black tiles which he can reach by repeating the moves described above.
Input
The 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)
Output
For 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
#include<stdio.h>
#include<string.h>
using namespace std;
char s[][];
int n,m;
int dfs(int x,int y)
{
int i,j,k;
if(x<||x>n-||y<||y>m-)
return ; if(s[x][y]=='#')
{
return ;
}
else
{
s[x][y]='#';
return +dfs(x+,y)+dfs(x-,y)+dfs(x,y+)+dfs(x,y-);//搜索四个方向
}
}
int main()
{
int i,j,k;
while(scanf("%d%d",&m,&n)!=EOF)
{
if(m==&&n==)
break;
for(i=;i<n;i++)
{
scanf("%s",s[i]);
}
int sum=;
for(i=;i<n;i++)
{
for(j=;j<m;j++)
{
if(s[i][j]=='@')
{
sum=dfs(i,j);
}
}
}
printf("%d\n",sum);
}
return ;
}
永不言弃!和ACM死磕到底!
HDOJ1312 Red and black(DFS深度优先搜索)的更多相关文章
- 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 ...
- HDU 1241 Oil Deposits DFS(深度优先搜索) 和 BFS(广度优先搜索)
Oil Deposits Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...
- HDU 4707 Pet(DFS(深度优先搜索)+BFS(广度优先搜索))
Pet Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submissio ...
- 回溯算法 DFS深度优先搜索 (递归与非递归实现)
回溯法是一种选优搜索法(试探法),被称为通用的解题方法,这种方法适用于解一些组合数相当大的问题.通过剪枝(约束+限界)可以大幅减少解决问题的计算量(搜索量). 基本思想 将n元问题P的状态空间E表示成 ...
- 『ACM C++』HDU杭电OJ | 1416 - Gizilch (DFS - 深度优先搜索入门)
从周三课开始总算轻松了点,下午能在宿舍研究点题目啥的打一打,还好,刚开学的课程还算跟得上,刚开学的这些课程也是复习以前学过的知识,下半学期也不敢太划水了,被各种人寄予厚望之后瑟瑟发抖,只能努力前行了~ ...
- 步步为营(十五)搜索(一)DFS 深度优先搜索
前方大坑预警! 先讲讲什么是搜索吧. 有一天你去一个果园摘梨子,果农告诉你.有一棵树上有一个金子做的梨子,找到就是你的,你该怎么找? 地图例如以下: S 0 0 0 0 0 0 0 0 0 0 0 0 ...
- [算法总结]DFS(深度优先搜索)
目录 一.关于DFS 1. 什么是DFS 2. DFS的搜索方式 二.DFS的具体实现 三.剪枝 1. 顺序性剪枝 2. 重复性剪枝 3. 可行性剪枝 4. 最优性剪枝 5. 记忆化剪枝 四.练习 一 ...
- 回溯 DFS 深度优先搜索[待更新]
首先申明,本文根据微博博友 @JC向北 微博日志 整理得到,本文在这转载已经受作者授权! 1.概念 回溯算法 就是 如果这个节点不满足条件 (比如说已经被访问过了),就回到上一个节点尝试别 ...
- DFS——深度优先搜索的一般格式
DFS是一种深度优先的搜索思想,运用递归完成搜索,本质上也算是穷举思想的一类,可以通过剪枝进行优化. DFS的核心是回溯和递归, 如果以迷宫为例,一般会指定走各个方向的顺序(例如先左再上再右再下).从 ...
随机推荐
- 关于Android的fragment的使用
fragment的静态使用 首先创建两个fragment,就把fragment当成activity去写布局,第一个是fragment_title: <LinearLayout xmlns:and ...
- 查看历史会话等待事件对应的session信息
此处以enq: TX - row lock contention等待时间为例. 查看snap_id对应时间 select to_char(s.startup_time,'dd Mon "at ...
- memory error python报错
np.array时报错内存溢出,检查了python安装的是64位版本,通过下面dtype=np.uint8不再报错texts_vec = (np.array(texts_vec,dtype=np.ui ...
- Windows —— cmd命令
Windows —— cmd命令 cd 命令 进入cmd的默认目录:默认为 C:\Users\Administrator> 进入文件夹:cd 文件夹名 返回上一层目录:cd.. 切换目录: 清屏 ...
- 2018-2019-2 网络对抗技术 20165236 Exp3 免杀原理与实践
2018-2019-2 网络对抗技术 20165236 Exp3 免杀原理与实践 一.实践内容(3.5分) 1.1 正确使用msf编码器(0.5分),msfvenom生成如jar之类的其他文件(0.5 ...
- StringBuufer与StringBulder线程的区别
StringBulder的append方法 不是synchronized修饰的 线程不安全 效率相对很快StringBuufer的append方法 是 synchronized修饰的 ...
- 通过类创建子线程&同步锁
一.通过类创建子线程 import threading class MyThread(threading.Thread): def __init__(self,num): threading.Thre ...
- Nodejs使用robot操作鼠标键盘
1.安装robotjs库 前提是配置了cnpm cnpm i robotjs -g 2.如果报错VCBuild.exe,如下可以安装windows-tool MSBUILD : error MSB ...
- Unity3D 粒子系统 属性
- sha-hmac
MAC----message authentication code,保证数据完整性的一个技术. HMAC类似于标准的sha运算,只是对于生成的mac增加了基于key的密钥保护. 生成的mac值,随着 ...