看了好多天的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; } }
相关推荐
深入解析docx4j:从Word到PDF的高保真转换实战
本文深入解析了如何使用纯Java开源库docx4j实现Word到PDF的高保真转换。针对中文环境,重点讲解了字体映射原理、Linux服务器字体部署方案,并提供了完整的代码示例和配置方法。同时,文章涵盖了表格、图片处理、性能优化及生产环境部署指南,帮助Java开发者构建稳定、可控的文档转换服务。
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...
C语言 100道经典编程题适用于专升本,专接本【详细分析版】_c语言专升本题目
/法2//left指向起始 right指向数组末尾= 0){left++;//如果*left是奇数就循环接着向后指,直到指向偶数就退出//(left
HDU1312 Red and Black(黑红砖块)(dfs入门水题)
题目链接:点击打开链接 题目大意:有一个长方形的房间,铺满了正方形瓷砖。每个瓷砖都是红色或黑色的。一个人站在一块黑色的瓷砖上。从一个瓷砖上,他可以移动到四个相邻(上下左右)的瓷砖中的一个。但是他不能移动到红色的瓷砖,只能在黑色的瓷砖上移动。通过重复上面描述的动作,编写一个程序来计算他能达到的黑瓷砖的数量。 输入:多个数据。第一行给出两个数m,n(0,0代表结束输入);m代表列,n代表行。m,n...
从AD到HFSS:Gerber文件驱动的PCB三维电磁仿真全流程解析
本文详细解析了从AD到HFSS的Gerber文件驱动的PCB三维电磁仿真全流程,重点介绍了Gerber文件作为桥梁的重要性、AD端Gerber文件导出实战指南、HFSS 3D Layout导入技巧及高级问题排查方法。通过实际案例和技巧分享,帮助工程师高效完成PCB电磁仿真,提升设计验证效率。
基于UCD3138的数字控制硬开关全桥DC-DC电源设计实战解析
在开关电源设计中,数字控制技术正逐步取代传统模拟方案,以其卓越的灵活性、可编程性和强大的数据处理能力,成为应对复杂动态响应与智能化需求的关键。其核心原理在于通过高精度模数转换器(ADC)实时采样电压、电流信号,经由数字信号处理器(如ARM内核)运行PID等控制算法,动态调整数字脉宽调制(DPWM)输出,从而实现对功率级的精确控制。这种技术价值体现在能够轻松实现高级算法(如输入电压前馈、非线性补偿)、灵活配置保护参数,并通过PMBus等接口实现智能化监控与管理。其典型应用场景包括通信基站、服务器电源以及工业设
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
多目标优化AI Agent:增强LLM的复杂决策能力
在人工智能领域,尤其是自然语言处理(NLP)和大语言模型(LLM)的应用中,决策问题往往涉及多个目标的平衡与优化。例如,在智能客服系统中,既要提高客户满意度,又要降低运营成本;在自动驾驶中,既要确保安全,又要追求行驶效率。传统的单目标优化方法难以满足这种复杂场景的需求,因此多目标优化成为了一种必然趋势。AI Agent是一种智能系统,能够通过感知环境、处理信息并采取行动来实现特定目标。它可以是一个软件程序,也可以是一个物理设备。NSGA-II:一种基于群体的多目标优化算法,通过遗传算法的思想进行迭代优化。
POJ 1979 Red and Black(红与黑)
原文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.
京东/淘宝商品评论情感分析:基于Python的大规模评论爬取与NLP情感分析实战
在当今电商蓬勃发展的时代,用户评论已成为影响消费者购买决策的关键因素。本文将详细介绍如何使用Python最新技术栈爬取京东和淘宝商品评论,并运用先进的自然语言处理技术对评论进行情感分析,识别用户的好评与差评倾向。通过完整的代码实现和深入的技术解析,帮助读者掌握从数据采集到情感分析的全流程。本文详细介绍了如何使用Python最新技术栈实现京东和淘宝商品评论的爬取与情感分析。通过结合异步爬虫、反爬策略、BERT情感分析等先进技术,我们构建了一个完整的评论分析系统。高效爬取。
【知识图谱】neo4j桌面版安装与配置-解决无法启功问题(2025年最新最全教程)
(经历过各种版本NEO4J,相比社区版而言,个人感觉桌面版更适合新手,安装调试更少更方便。如果是社区版改桌面版的,记得删除系统变量里NEO4J_HOME这个变量,否则会报错。
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
vue项目表格甘特图开发
🧩 甘特图可以管理项目进度,生产进度等信息,管理者可以更直观的查看内容。
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
C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片
基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正
ssm框架搭建
这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载
HDU T1312 Red and Black
HDU T1312 Red and Black 题解: 因为是遍历所有可以走的点,所以不需要回溯。需要注意的是写完要测试最小值 例如:1 1 @ 答案应该为1,本身也算一个。 Dfs代码 #include<cstdio> #include<iostrea...
IEM模型,地表粗糙度模拟
利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟
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 },
廖雪峰Python教程[完整版]
廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白
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...
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
671



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



