看了好多天的DFS,基本上的程序都是要借助解题报告才可以实现
但是这一题,确实依照自己的思路,完全靠自己写出来的,或许比较简单吧,所以特此发表下文章,犒劳一下自己,再接再厉
http://acm.hdu.edu.cn/showproblem.php?pid=1312
Red and Black
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2364 Accepted Submission(s): 1551
Problem DescriptionThere 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.
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 Input6 9 ....#. .....# ...... ...... ...... ...... ...... #@...# .#..#. 11 9 .#......... .#.#######. .#.#.....#. .#.#.###.#. .#.#..@#.#. .#.#####.#. .#.......#. .#########. ........... 11 6 ..#..#..#.. ..#..#..#.. ..#..#..### ..#..#..#@. ..#..#..#.. ..#..#..#.. 7 7 ..#.#.. ..#.#.. ###.### ...@... ###.### ..#.#.. ..#.#.. 0 0
Sample Output45 59 6 13#include<iostream> #include<string.h> #include<stdio.h> using namespace std; int direct[4][2] = { -1, 0, 1, 0, 0, 1, 0, -1 }; /*定义方向, 左右,上下*/ char map[21][21]; /*输入的字符串*/ bool mark[21][21]; /*标记走过的路程*/ bool flag; int W, H; int Dx, Dy; //记录起始位置@,从这里开始进行搜索 int ans; //记录满足的个数。初始化为1,因为@也包含在内 /*****底下是核心算法,主要是从 上下左右这四个方向进行搜索,注意 满足搜索的条件是不能越界,不能是#, 还有就是没有搜索过的--》主要是靠mark[i][j] 来实现*******/ void DFS( int x, int y ) { mark[x][y] = true; for( int k = 0; k < 4; k ++ ) { int p = x + direct[k][0]; int q = y + direct[k][1]; if( p >= 0 && q >= 0 && p < H && q < W && !mark[p][q] && map[p][q] != '#' ) { ans ++; DFS( p, q ); } } return; } int main() { int i, j, k; while( cin >> W >> H && ( W || H ) ) // W -> column, H -> row; { memset( mark, false, sizeof( mark ) ); for( i = 0; i < H; i ++ ) for( j = 0; j < W; j ++ ) { cin >> map[i][j]; if( map[i][j] == '@' ) { Dx = i; Dy = j; } } ans = 1; DFS( Dx, Dy ); cout << ans << endl; } }
相关推荐
SoC低功耗设计实战:28nm工艺下四核A7的Hierarchical Flow全流程解析
本文深入解析了在TSMC 28nm工艺上,采用Hierarchical Flow(层次化流程)实现四核Cortex-A7 SoC低功耗设计的全流程。文章从项目背景切入,详细探讨了基于UPF的电源规划、时钟树综合、接口时序收敛以及物理验证等关键环节,分享了使用Cadence Innovus等工具进行低功耗物理实现的实战经验与挑战。
Red and Black
Red and Black 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 o...
从零到一:在K210上实现YOLO模型在线训练与部署全流程
本文详细介绍了在K210芯片上实现YOLO模型在线训练与部署的全流程,涵盖数据采集、模型优化、量化转换及内存管理等关键环节。通过实战案例和代码示例,帮助开发者高效利用K210的超低功耗特性与YOLO模型的实时检测能力,适用于边缘计算和物联网应用场景。
HDU1312 Red and Black(黑红砖块)(dfs入门水题)
题目链接:点击打开链接 题目大意:有一个长方形的房间,铺满了正方形瓷砖。每个瓷砖都是红色或黑色的。一个人站在一块黑色的瓷砖上。从一个瓷砖上,他可以移动到四个相邻(上下左右)的瓷砖中的一个。但是他不能移动到红色的瓷砖,只能在黑色的瓷砖上移动。通过重复上面描述的动作,编写一个程序来计算他能达到的黑瓷砖的数量。 输入:多个数据。第一行给出两个数m,n(0,0代表结束输入);m代表列,n代表行。m,n...
西门子WinCC VBS脚本实战:5分钟搞定变量记录导出到Excel(附完整代码)
本文提供了一套高效的西门子WinCC VBS脚本方案,帮助工程师在5分钟内将变量记录数据导出至Excel。方案详细解析了时区转换、数据库连接、SQL查询优化等核心难点,并附带了完整、可直接使用的VBS代码,解决了工业自动化项目中数据导出的实际痛点。
【深度强化学习】(2) Double DQN 模型解析,附Pytorch完整代码
Double DQN 算法是 DQN 算法的改进版本,解决了 DQN 算法过估计行为价值的问题。Double DQN 算法不直接通过最大化的方式选取目标网络计算的所有可能 Q 值,而是首先通过估计网络选取最大 Q 值对应的动作。在 Double DQN 框架中存在两个神经网络模型,分别是训练网络与目标网络。这两个神经网络模型的结构完全相同,但是权重参数不同;每训练一段之间后,训练网络的权重参数才会复制给目标网络。
hdu1312 “Red and Black”——DFS与递归的应用
Red and Black 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 b
AD7920实战:从咪头到ADC的音频信号采集电路设计全解析(附电路图)
本文详细解析了基于AD7920 ADC芯片的音频信号采集电路设计全流程。从驻极体咪头的驱动、AD8542运放的两级增益与偏置设计,到关键的抗混叠滤波器配置,以及ADC接口的驱动缓冲与PCB布局要点,提供了完整的实战指南与电路图,帮助开发者实现高质量音频信号采集。
HDU 1312 Red and Black(经典搜索,DFS&BFS三种方式)
Red and Black Problem Description 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
科研级YOLOv8 Neck革新:BiFPN双向特征金字塔赋能,让多尺度融合更高效的实战指南
在文件夹下新建BiFPN.py,将上述BiFPNLayer和BiFPN类的代码写入其中。这一步是为了让YOLOv8的代码架构能识别我们的自定义Neck模块。
Win7 x64位系统下几乎完美使用安装RTX3050 6G显卡驱动详细教程
RTX3050 6G显卡驱动安装指南:由于该显卡发布时间晚于驱动,需手动修改安装文件。首先在NVIDIA官网下载驱动并解压,通过设备管理器获取硬件ID(PCI\VEN_10DE&DEV_2584),然后在解压目录的.inf文件中添加对应的设备标识。安装完成后可能出现设备管理器感叹号问题,推荐通过命令提示符启用测试模式(bcdedit /set testsigning on)解决,此方法无需每次启动按F8,但会显示测试模式水印。文中提供了详细的图文操作步骤和两种解决方案。
hdu 1312 Red and Black
hdu 1312 Red and Black 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312 DFS水 题目大意:一个人被困在一个迷宫里,问他的生存空间。 题目分析:DFS就好了。 code: #include #include char mp[26][26],vis[26][26]; int m
跟踪雷达点迹处理
1,点迹定义: 假设研究对象为位置x和速度v,预测值是根据这一时刻的x值、v值来预测下一时刻的x、v;测量值是根据传感器也就是雷达得到的,因为各种误差的存在,这两个值都不准确,所以把他们加权融合,得到一个新的量—状态值。 点迹就是所谓的测量值,由雷达对同一个目标探测到的一系列点(假设目标静止,但由于误差的存在,会扫描出不止一个点),航迹对应状态值,是点迹和预测轨迹的加权融合。 2,雷达信号和点迹处理关系: 3,点迹航迹处理流程 4,仿真 clear all; RealData=G
HDU T1312 Red and Black
HDU T1312 Red and Black 题解: 因为是遍历所有可以走的点,所以不需要回溯。需要注意的是写完要测试最小值 例如:1 1 @ 答案应该为1,本身也算一个。 Dfs代码 #include<cstdio> #include<iostrea...
jdk-8u144-linux-x64.tar.gz.
该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包
LinePhone3.2.7(android端最新版本) sip客户端
SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/
Hdu 1312 Red and black(BFS)
HDU 1312 Red and black#include<iostream> #include<cstring> #include<algorithm> #include<queue> using namespace std; char map[25][25]; int flag[25][25];//标记 已经走过的路 int step[4][2] = { { 1, 0 }, { 0, 1 },
复分析 阿尔福斯(Ahlfors) 第三版答案 英文
答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案
HDU-1312 Red and Black
HDU 1312 Red and Black 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...
霍尼韦尔SoftMaster使用说明
霍尼韦尔SoftMaster使用说明
hdu1312 Red and Black
Problem Description 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 adjacen
HDU1312——Red and Black
http://acm.hdu.edu.cn/showproblem.php?pid=1312 这倒题用的是广搜:广搜用的是队列。就是从一个点有四个方向,所以把四个可能都放进队列里。 广搜也是状态改变。就像是泼水的感觉。 题意:就是从@位置最多能走多少步。明显的广搜 #include<stdio.h> #include<cstring> #inclu...
HDU 1312 Red and Black【递归】
Red and Black Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 13111 Accepted Submission(s): 8127 Problem Description There is a re
hdu 1312 Red and Black
Red and Black Time Limit: 2000/1000 MS (Java/Others)Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 29940Accepted Submission(s): 18118 Problem Description There is a rectan...
hdu 1312 Red And Black
hdu 1312 的传送门Problem Description 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
HDU 1312 Red and Black <BFS>
Red and Black Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2364 Accepted Submission(s): 1551 Problem Description There is a recta
hdu_1312 Red and Black
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312 分析:简单的BFS。使用广搜,使每个满足添加的点进队列,统计删除的点,有多少个点被删除就有多少个满足题意的点。 我的代码: #include #include #include using namespace std; char map[21][21]; bool visited[21][
hdu 1312 Red and Black
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312点击打开链接 Red and Black Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 11962 Accepte
330



被折叠的 条评论
为什么被折叠?



