HDU 1312 Red and Black (DFS & BFS)
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312
题目大意:有一间矩形房屋,地上铺了红、黑两种颜色的方形瓷砖。你站在其中一块黑色的瓷砖上,只能向相邻的黑色瓷砖移动,计算你总共能够到达多少块黑色的瓷砖。
要求:输入包含多组数据,每组数据的第一行输入的是两个整数 W 和 H, 分别表示 x 方向与 y 方向上的瓷砖数量,并且 W 和 H 都不超过20。在接下来的 H 行中,每行包含 W 个字符,每个字符表示一块瓷砖的颜色,规则如下:
' . ' 代表黑色的瓷砖;
' # '代表白色的瓷砖;
' @ ' 代表黑色的瓷砖,并且你站在这块瓷砖上,该字符在每个数据集合中唯一出现一次。
当在一行中读入的是两个零时,表示输入结束。
对每个数据集合,分别输出一行,显示你从初始位置出发能到达的瓷砖数(记数时包括初始位置的瓷砖)。
思路:这道题算是一道比较典型的 DFS 题,但也可以用 BFS 做出来。
代码:
DFS
#include <iostream>
#include <cstring>
using namespace std;
#define clr(a) memset(a,0,sizeof(a))
const int MAXN = 25;
int dir[2][4]={1, -1 , 0, 0, 0, 0, 1, -1};
int w, h, ans, sx, sy;
int mp[MAXN][MAXN];
char s[MAXN][MAXN];
int DFS(int x,int y){
mp[x][y] = 1;
for(int i=0;i<4;i++){
int dx = x + dir[0][i];
int dy = y + dir[1][i];
if (dx >= 0 && dy >= 0 && dy < w && dx < h &&
s[dx][dy] == '.' && !mp[dx][dy]) {
ans++;
DFS(dx, dy);
}
}
return ans;
}
int main() {
while (scanf("%d %d", &w, &h) != EOF) {
if (w == 0 && h == 0) break;
clr(mp);
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
cin >> s[i][j];
if (s[i][j] == '@') {
sx = i;
sy = j;
}
}
}
ans = 1;
cout << DFS(sx, sy) << endl;
}
return 0;
}
BFS (使用 广搜时可以把搜索过的地方用 ' # ' 进行标记)
#include <iostream>
#include <queue>
using namespace std;
const int N = 25;
int w, h, sx, sy, ans;
char s[N][N];
int d[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};
struct node {
int x, y;
};
void bfs() {
node start, end;
queue<node> q;
start.x = sx;
start.y = sy;
q.push(start);
while (!q.empty()) {
start = q.front();
q.pop();
for (int i = 0; i < 4; i++) {
end.x = start.x + d[i][0];
end.y = start.y + d[i][1];
if (end.x >= 0 && end.y >= 0 && end.y < w && end.x < h &&
s[end.x][end.y] == '.' ) {
ans++;
s[end.x][end.y] = '#';
q.push(end);
}
}
}
}
int main() {
while (cin >> w >> h) {
if (w == 0 && h == 0) break;
for (int i = 0; i < h; i++)
for (int j = 0; j < w; j++) {
cin >> s[i][j];
if (s[i][j] == '@') {
sx = i;
sy = j;
}
}
ans = 1;
bfs();
cout << ans << endl;
}
return 0;
}
HDU 1312 Red and Black (DFS & BFS)的更多相关文章
- 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 ...
- 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 1312 Red and Black(DFS,板子题,详解,零基础教你代码实现DFS)
Red and Black Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- 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 (DFS)
Problem Description There is a rectangular room, covered with square tiles. Each tile is colored eit ...
- HDU 1312 Red and Black --- 入门搜索 BFS解法
HDU 1312 题目大意: 一个地图里面有三种元素,分别为"@",".","#",其中@为人的起始位置,"#"可以想象 ...
- HDU 1312 Red and Black --- 入门搜索 DFS解法
HDU 1312 题目大意: 一个地图里面有三种元素,分别为"@",".","#",其中@为人的起始位置,"#"可以想象 ...
- HDU 1312:Red and Black(DFS搜索)
HDU 1312:Red and Black Time Limit:1000MS Memory Limit:30000KB 64bit IO Format:%I64d & ...
- HDU 1312 Red and Black (dfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312 Red and Black Time Limit: 2000/1000 MS (Java/Oth ...
随机推荐
- UIWindow介绍
1.作为容器,包含app所要显示的所有视图 3.与UIViewController协同工作,方便完成设备方向旋转的支持 1.addSubview 2.rootViewController 三.Wind ...
- 模仿写了一个摸鱼APP解决原作者的问题
前几天见到微博里有人提到摸鱼APP,发现需要在windows store下载才可以使用,文件约100多M左右的样子,自已没有登录微软Store的习惯.想想只有一个介面,没有必要这么大,于是,自已动手写 ...
- 【超详细】安全测试===sqlmap使用心得(零)
零.前言 这篇文章是学习Sqlmap的用法时做的笔记,记录了Sqlmap的常见.基础用法. 一.Sqlmap是什么 Sqlmap是开源的自动化SQL注入工具,由Python写成,具有如下特点: 完全支 ...
- java 多线程:Thread类;Runnable接口
1,进程和线程的基本概念: 1.什么是进程: 进程(Process)是计算机中的程序关于某数据集合上的一次运行活动,是系统进行资源分配和调度的基本单位,是操作系统结构的基础.在早期面向进程设计的计算机 ...
- canvas 实现渐变色填充的三角形
实现效果 效果一: 效果二: 实现思路 canvas实现 1. 绘制三角形 // html <canvas id="triangle" width="30" ...
- JAVA字符串拼接操作规则说明
1.常量与常量的拼接结果在常量池,原理是编译期优化 public void test1() { String s1 = "a" + "b" + "c& ...
- Android 控件使用教程(二)—— RecyclerView 展示图片
简介 在上一篇博文中,介绍了大家已经很熟悉的布局控件ListView,在这篇文章中,我将使用比较新.功能也更强大的RecyclerView. RecyclerView 首先,要用这个控件,你需要在gr ...
- 【LeetCode】1013. Pairs of Songs With Total Durations Divisible by 60 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】598. Range Addition II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- The All-purpose Zero(hdu5773)
The All-purpose Zero Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Oth ...