D - Red and Black

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d
& %I64u

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
 

这题已经不想在吐槽什么了,n,m 和正常的出相反 害我一些细节错 一直WA 还说到底还是自己太弱了

#include<iostream>
using namespace std; #include<string.h>
#define max 25
char map[max][max];int mmin;
long n,m,visited[max][max];
long directions[4][2]={{0,-1},{0,1},{1,0},{-1,0}}; void DFS(int x,int y)
{
int i,mx,my;
for(i=0;i<4;i++)
{
mx=x+directions[i][0];
my=y+directions[i][1];
if(mx>=0&&mx<m&&my>=0&&my<n)
{
if(!visited[mx][my]&&map[mx][my]=='.')
{
visited[mx][my]=1;
mmin++;
DFS(mx,my);
}
}
}
}
int main()
{ int i,j,a,b;
while(cin>>n>>m)
{
if(n==0&&m==0)break;
memset(visited,0,sizeof(visited)); for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cin>>map[i][j];
if(map[i][j]=='@')
{
a=i,b=j;
}
}
}
mmin=1;
visited[a][b]=1;
DFS(a,b);
cout<<mmin<<endl;
}
return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

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

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

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

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

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

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

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

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

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

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

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

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

  8. HDU 1312 Red and Black(经典DFS)

    嗯... 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312 一道很经典的dfs,设置上下左右四个方向,读入时记下起点,然后跑dfs即可...最后答 ...

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

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

  10. hdu 1312:Red and Black(DFS搜索,入门题)

    Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

随机推荐

  1. [Java]_函数传参的疑惑与思考

    问题来源于leetcode上的两道题 Path Sum I && II,分别写了两个dfs. void dfs(TreeNode node , int sum , ArrayList& ...

  2. JS 获取浏览器和屏幕宽高等信息代码

    JS 获取浏览器和屏幕宽高等信息. 网页可见区域宽:document.body.clientWidth  网页可见区域高:document.body.clientHeight  网页可见区域宽:doc ...

  3. centos彻底删除mysql

    yum remove mysql mysql-server mysql-libs compat-mysql51rm -rf /var/lib/mysqlrm /etc/my.cnf查看是否还有mysq ...

  4. Spark官方文档——本地编写并运行scala程序

    快速开始 本文将介绍如何用scala.java.python编写一个spark单击模式的程序. 首先你只需要在一台机器上成功建造Spark:做法: 进入Spark的根目录,输入命令:$ sbt/sbt ...

  5. PHP 只有登陆后才能浏览的简单实现

    1.============================================================= 在你不想让别人直接进入的网页开头加一段代码: session_start ...

  6. 种树 (codevs 1653) 题解

    [问题描述] 一条街的一边有几座房子.因为环保原因居民想要在路边种些树.路边的地区被分割成块,并被编号为1..n.每个块大小为一个单位尺寸并最多可种一棵树.每个居民想在门前种些树并指定了三个号码b,e ...

  7. MVC MVVM Knockout viewmodel 提交 完整过程,包含序列化 JSON 和 字典模型绑定

    //JSON 通用操作------------------------------------------------------------------------------using Syste ...

  8. does not match bootstrap parameter

    问题描述: DBD::mysql object version 2.0419 does not match bootstrap parameter 2.0902 at /usr/libdata/per ...

  9. JAVA里的字符串,String 类简单介绍

    http://www.360doc.com/content/14/1107/23/17130779_423471141.shtml

  10. Python学习教程(learning Python)--3.3.4 Python的if-elif-else语句

    Python的if-elif-else语句用于多种条件判断后选择某个语句块执行.该语句可以利用一系列条件表达式进行检查,并在某个表达式为真的情况下执行相应的代码.需要注意的是,虽然if/elif/el ...