Red and Black(DFS深搜实现)
Description
Write a program to count the number of black tiles which he can reach by repeating the moves described above.
Input
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)
Output
Sample Input
Sample Output
59
6
13
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;
char map[][];
int vis[][];
int dir[][]={{,},{,-},{,},{-,}};
int n,m,num;
void DFS(int x,int y)
{
int a,b,i;
vis[x][y]=;
num++;
for(i=;i<;i++)
{
a=x+dir[i][];
b=y+dir[i][];
if(a>=&&a<m&&b>=&&b<n&&vis[a][b]==&&map[a][b]=='.')
{
DFS(a,b);
}
}
}
int main()
{
int i,j,x,y;
while(scanf("%d%d",&n,&m)!=EOF)
{
getchar();
if(m==&&n==)
break;
memset(map,,sizeof(map));
memset(vis,,sizeof(vis));
for(i=; i<m; i++)
{
scanf("%s",map[i]);
}
for(i=; i<m; i++)
{
for(j=; j<n; j++)
{
if(map[i][j]=='@')///只有一个人
{
x=i;
y=j;
}
}
}
num=;
DFS(x,y);
printf("%d\n",num);
}
return ;
}
Red and Black(DFS深搜实现)的更多相关文章
- CodeM美团点评编程大赛初赛B轮 黑白树【DFS深搜+暴力】
[编程题] 黑白树 时间限制:1秒 空间限制:32768K 一棵n个点的有根树,1号点为根,相邻的两个节点之间的距离为1.树上每个节点i对应一个值k[i].每个点都有一个颜色,初始的时候所有点都是白色 ...
- DFS 深搜专题 入门典例 -- 凌宸1642
DFS 深搜专题 入门典例 -- 凌宸1642 深度优先搜索 是一种 枚举所有完整路径以遍历所有情况的搜索方法 ,使用 递归 可以很好的实现 深度优先搜索. 1 最大价值 题目描述 有 n 件物品 ...
- DFS深搜——Red and Black——A Knight's Journey
深搜,从一点向各处搜找到全部能走的地方. Problem Description There is a rectangular room, covered with square tiles. Eac ...
- 【DFS深搜初步】HDOJ-2952 Counting Sheep、NYOJ-27 水池数目
[题目链接:HDOJ-2952] Counting Sheep Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 ...
- poj 2386:Lake Counting(简单DFS深搜)
Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18201 Accepted: 9192 De ...
- UVA 165 Stamps (DFS深搜回溯)
Stamps The government of Nova Mareterrania requires that various legal documents have stamps attac ...
- HDU 1312 Red and Black (深搜)
题目链接 Problem Description There is a rectangular room, covered with square tiles. Each tile is colore ...
- CCF 模拟E DFS深搜
http://115.28.138.223:81/view.page?opid=5 这道题问的很怪. 起点DFS,每一个点还要DFS一次,统计不能到终点的个数 数据量不大这样做也能AC #includ ...
- HDU_1241 Oil Deposits(DFS深搜)
Problem Description The GeoSurvComp geologic survey company is responsible for detecting underground ...
随机推荐
- 执行SQL查询导致磁盘耗尽故障演示
a fellow in IMG wechat group 2 met an error about running out of disk space when using MySQL ...
- mysql学习记录,CASE WHEN THEN ELSE END用法
记mysql,case when then else end用法 用法1:搜索函数 SELECT r.order_no, r.golds, r.pay_tool, , ) ) END AS price ...
- 解决 LLVM 错误提示 may only occur zero or one times!
使用 LLVM 混淆器添加参数进行编译提示如下错误:clang (LLVM option parsing): for the -bcf option: may only occur zero or o ...
- mysql如何配置sql记录
原文链接:http://www.qqdeveloper.com/detail/11/1.html 为什么要记录sql记录 主要目的是为了检测我们的网站安全问题,有效的避免一些sql注入或者是xss攻击 ...
- 四、分离分层的 platform驱动
学习目标: 学习实现platform机制的分层分离,并基于platform机制,编写led设备和驱动程序: 一.分离分层 输入子系统.usb设备比驱动以及platform类型的驱动等都体现出分离分层机 ...
- 嘿,C语言(持续更新中...)
---恢复内容开始--- 上次简单介绍了一下C语言,这次说说数据与计算程序,那么话不多说,进来看看. 第二章 数据与简单的计算程序 一:数据 既然说到了数据,那么说说什么是写数据呢? 表面意 ...
- 对fgets的理解
gets()函数 因为用gets函数输入数组时,只知道数组开始处,不知道数组有多少个元素,输入字符过长,会导致缓冲区溢出,多余字符可能占用未使用的内存,也可能擦掉程序中的其他数据,后续用fgets函数 ...
- Uber优步北京第二、三组奖励政策
优步北京第二.三组: 定义为2015年6月1日至今激活的司机(以优步后台数据显示为准) 滴滴快车单单2.5倍,注册地址:http://www.udache.com/如何注册Uber司机(全国版最新最 ...
- P1563 玩具谜题
P1563 玩具谜题 题目描述 小南有一套可爱的玩具小人, 它们各有不同的职业. 有一天, 这些玩具小人把小南的眼镜藏了起来. 小南发现玩具小人们围成了一个圈,它们有的面朝圈内,有的面朝圈外.如下图: ...
- Android AOSP 编译sdk
首先你要有AOSP 工程. 然后执行下面的命令编译sdk. //运行下面的命令得到编译环境 source build/envsetup.sh lunch sdk make sdk 最后文件输出在哪个位 ...