Problem Description
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.

 
Input
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) 

 
Output
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). 
 
Sample Input
6 9
....#.
.....#
......
......
......
......
......
#@...#
.#..#.
11 9
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
###.###
...@...
###.###
..#.#..
..#.#..
0 0
 
Sample Output
45
59
6
13
要小心它的输入
行列是相反的
坑在这里找了好多次TAT
 #include<stdio.h>
#include<iostream>
using namespace std;
char map[][];
int n,m;
int num;
int rx[]={,,,-};
int ry[]={,-,,};
void dfs(int i,int j)
{
map[i][j]='#';
num++;
int t,x,y;
for(t=;t<;t++)
{
x=i+rx[t];
y=j+ry[t];
if(x>= && x<n && y>= && y<m && map[x][y]=='.')
dfs(x,y);
}
}
int main()
{
int i,j;
int si,sj;
while(scanf("%d%d",&m,&n)!=EOF)//原来是行数和列数反了...我真是醉了....
{ if(m==&&n==)break;
for(i=;i<n;i++)
for(j=;j<m;j++)
{
cin>>map[i][j];
if(map[i][j]=='@')
{
si=i;
sj=j;
}
}
num=;
dfs(si,sj);
printf("%d\n",num);
}
return ;
}

HDU 1312 Red and Black (DFS)的更多相关文章

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

  2. HDU 1312 Red and Black (DFS & BFS)

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312 题目大意:有一间矩形房屋,地上铺了红.黑两种颜色的方形瓷砖.你站在其中一块黑色的瓷砖上,只能向相 ...

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

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

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

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

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

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

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

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

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

  9. HDU 1312 Red and Black(最简单也是最经典的搜索)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1312 Red and Black Time Limit: 2000/1000 MS (Java/Oth ...

随机推荐

  1. 怎样在linux或者Unix上检查端口是否在使用

     英文原文链接:https://www.cyberciti.biz/faq/unix-linux-check-if-port-is-in-use-command/ Question 1: 怎样在lin ...

  2. Factory Pattern(工厂模式)

    1.工厂模式简介 工厂模式,专门负责将大量有共同接口的类实例化(用来生产对象).其定义为定义一个用于创建对象的接口,让子类决定实例化那一个类.工厂方法使一个类的实例化延迟到其子类. 工厂模式拥有以下几 ...

  3. Java学习笔记-Thread-线程

    //2015年5月5日16:55:00 //Main package com.alfredsun.thread; public class Main { public static void main ...

  4. C# LocalizationHelpers (搬运)

    using Utils; namespace System.Web.Mvc { /// <summary> /// 本地化帮助类 /// </summary> public s ...

  5. Android studio自动删除没用的资源

    有时候我们添加的一些资源,如图片和一些没用的代码,以及在添加第三方库的时候我们只需要使用其中的一部分功能和一部分资源,那么这个时候如果靠我们手工去怕是非常难做的,尤其是项目大的时候,Android 团 ...

  6. PHP检测文件能否下载

    用php代码检测一个文件是否可以下载,网上没有找到合适的代码,自己实现了一个还挺好用的,分享给有需要的朋友. 基本原理:使用http的HEAD方法,检测报文的头里httpcode是否为200. pub ...

  7. CentOS7 下安装telnet服务

    今天搞了下 Centos 7 下面升级 openssl 和 openssh ,顺便装了下 telnet # 安装 telnet 避免 ssh 无法登录 yum -y install xinetd te ...

  8. iOS UIImageView自适应图片大小

    窗口大小获取: CGRect screenBounds = [ [UIScreenmainScreen]bounds];//返回的是带有状态栏的Rect CGRect rect = [ [UIScre ...

  9. WordPress网站加速优化,一键免费使用七牛CDN插件

    利用wordpress搭建网站是个人建站的主流方案,我曾分享过wordpress网站加速优化必做的十件事,帮助了不少个人站长.今天介绍帮助wordpress网站提升速度至少10倍的免费CDN加速插件: ...

  10. imx6平台qt锯齿原因分析

    gbl_alpha:全局alpha 就是设置整个界面的alpha值 loc_alpha 本地alpha 就是图片里面自带的alpha值 color_key color Key 要把界面透出来,就要设置 ...