Red and Black(DFS 深搜练习)
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 black tiles.
Write a program to count the number of black tiles which he can reach by repeating the moves described above.
The 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)
For each data set, your program should output a line which contains the number of tiles he can reach from the initial tile (including itself).
题意:就是N*m的迷宫里面,@ 代表入口,然后 . 代表能通行,#代表不能通行,然后问你从@开始能走多少个格子,也就是连通性问题!
思路:可以理解为像树一样从根部开始,然后的话就往下是各个根,每条路都到达根的底部。
#include<iostream>
#include<stdio.h>
#include<math.h>
using namespace std;
char s[25][25];
int flag;
void dfs(int a, int b)
{
if (s[a + 1][b] == '.')
{
s[a + 1][b] = '#';
flag++;
dfs(a + 1, b);
}
if (s[a - 1][b] == '.')
{
s[a - 1][b] = '#';
flag++;
dfs(a - 1, b);
}
if (s[a ][b+1] == '.')
{
s[a][b + 1] = '#';
flag++;
dfs(a,b+1);
}
if (s[ a ][b-1] == '.')
{
s[a][b-1] = '#';
flag++;
dfs(a ,b-1);
}
}
int main()
{
int i, j, k, n, m;
char c;
while (cin >> n >> m&&(n+m)){
flag = 0;
int a, b;
for (i = 0; i <25; i++)
for (j = 0; j < 25; j++)
s[i][j] = '#';
for (i = 0; i < m; i++){
for (j = 0; j < n; j++){
cin >> s[i][j];
if (s[i][j] == '@')
{
a = i;
b = j;
}
}
}
dfs(a, b);
cout << flag+1 << endl;
}
return 0;
}
然后按照正常的板子来写的话 应该是如下
/*
搜索入门题
*/
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
char map[25][25];
bool vis[25][25];
int s,e;
int N,M;
int mov[4][2]={0,1,1,0,0,-1,-1,0};
bool Judge(int x,int y){
if(x>=0&&x<N&&y>=0&&y<M)return true;
return false;
}
int step;
void DFS(int x,int y){
int xx=x;
int yy=y;
for(int i=0;i<4;i++){
x=xx+mov[i][0];
y=yy+mov[i][1];
if(Judge(x,y)&&map[x][y]=='.'&&vis[x][y]==0)
{
step++;
vis[x][y]=1;
DFS(x,y);
}
}
return;
}
int main(){
while(scanf("%d%d",&M,&N)!=EOF){
if(M==0&&N==0)break;
memset(vis,0,sizeof(vis));
memset(map,0,sizeof(map));
for(int i=0;i<N;i++){
scanf("%s",map[i]);
}
int x,y;
for(int i=0;i<N;i++){
for(int j=0;j<M;j++){
if(map[i][j]=='@'){
x=i;
y=j;
}
}
}
//printf("%d %dn",x,y);
step=1;
vis[x][y]=1;
DFS(x,y);
printf("%dn",step);
}
return 0;
}
- 数组和链表的区别
- 《快学Scala》第一章 基础
- 二分查找法的实现和应用汇总
- 《快学Scala》第一章 基础
- 移动端打印输出内容以及网络请求-vconsole.js
- 二分查找法的实现和应用汇总
- JavaScript前端和Java后端的AES加密和解密
- 《Spark MLlib 机器学习实战》1——读后总结
- angularjs自定义指令实现分页插件
- A+B for Input-Output Practice (V)
- 机器学习——相似度算法汇总
- 白话推荐系统——从原理到实践,还有福利赠送!
- 基于Spring Boot的Logback日志轮转配置
- Java程序员的日常—— Spring Boot单元测试
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- TCP SYN flood洪水攻击原理和防御破解
- 你不知道的Golang map
- go 1.11 模块和版本管理
- Golang Testing单元测试指南
- 你不知道的Go unsafe.Pointer uintptr原理和玩法
- 深度剖析各种BloomFilter的原理、改进、应用场景
- 深入理解哈希表
- iota: Golang 中优雅的常量
- Go template高级用法、深入详解、手册、指南、剖析
- Docker开启Remote API 访问 2375端口
- Docker实用debug调试技巧锦集
- shell 命名管道,进程间通信, ncat作http server
- Linux atop监控
- go modules中使用replace用法
- 分布式文件系统Ceph的挂载方式