原题链接: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)的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. HDU 1312 Red and Black(bfs)

    Red and Black Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Descr ...

  5. HDU 1312 Red and Black (DFS)

    Problem Description There is a rectangular room, covered with square tiles. Each tile is colored eit ...

  6. HDU 1312 Red and Black --- 入门搜索 BFS解法

    HDU 1312 题目大意: 一个地图里面有三种元素,分别为"@",".","#",其中@为人的起始位置,"#"可以想象 ...

  7. HDU 1312 Red and Black --- 入门搜索 DFS解法

    HDU 1312 题目大意: 一个地图里面有三种元素,分别为"@",".","#",其中@为人的起始位置,"#"可以想象 ...

  8. HDU 1312:Red and Black(DFS搜索)

      HDU 1312:Red and Black Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d & ...

  9. 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 ...

随机推荐

  1. 一个使用 asyncio 开发的网络爬虫(译文)

    原文地址:https://www.aosabook.org/en/500L/a-web-crawler-with-asyncio-coroutines.html 作者简介 A. Jesse Jiryu ...

  2. pwnable_start & ciscn_2019_es_2 & ez_pz_hackover_2016 & pwn2_sctf_2016

    花了两天时间做了这四道题,感觉收获很多.但是这种收获感觉写文章写不出自己的思路,就录制了一个视频. pwnable_start 这道题考察了系统调用,shellcode的编写,和动态调试的知识. ci ...

  3. BitBake使用攻略--从HelloWorld讲起

    目录 写在前面 1. 什么是BitBake 2. BitBake的安装 3. 使用BitBake构建一个HelloWorld工程 后续 写在前面 <BitBake使用攻略>系列文章将从今天 ...

  4. django - Templates模板嵌套语法

    模板继承 1.继承母板:{% extends '母板html文件名称' %} 2.包含子模板:{% include  '子母板html 文件名' %} 模板内容分块 {% block <分块名& ...

  5. redis启动报错 var/run/redis_6379.pid exists, process is already running or crashed

    redis启动显示 /var/run/redis_6379.pid exists, process is already running or crashed 出现这个执行 rm -rf /var/r ...

  6. 【LeetCode】145. Binary Tree Postorder Traversal 解题报告 (C++&Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leetc ...

  7. 【LeetCode】978. Longest Turbulent Subarray 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 虫取法 日期 题目地址:https://leetco ...

  8. 【LeetCode】380. Insert Delete GetRandom O(1) 解题报告(Python)

    [LeetCode]380. Insert Delete GetRandom O(1) 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxu ...

  9. uniapp医院预约挂号微信小程序

    开头感言:最近看小程序很火,也想弄一个看看,用了一些时间从0开始写,也记录了一些笔记,自己用框架写的模板,不是很精美,后面会慢慢优化,功能也是后面慢慢加上去的, 其中功能这块,起初只是一些简单的功能, ...

  10. 响应式网页设计(Bootstrap)

    Bootstrap官网 AOS官网 Chrome官方教程 Chrome教程 Bootstrap官网中有许多Bootstrap网站示例,大家可以参考