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

    Hub(中心) 中心页是用户进入应用的入口点.中心页在丰富的平移视图中显示内容,这样用户一眼就能看见新鲜有趣的内容,从而吸引他们深入了解你的应用中的更多内容.中心显示不同的内容类别,每个类别映射到应用 ...

  2. 05-Tom猫(UIImageView的简单运用)

    ViewController.h文件中: @interface ViewController : UIViewController @property (weak, nonatomic) IBOutl ...

  3. PHP中的Array

    PHP中的数组是一个有序映射(1对1的关系 key->value).Array是一个综合体:可表示数组.字典.集合等. key可以是int或string.value可以是任意类型. key如下情 ...

  4. centos下安装php环境

    centos下安装php环境 安装apache yum install httpd-devel 启动apache /etc/init.d/httpd start 安装mysql yum install ...

  5. WIN10 64位下VS2015 MFC直接添加 halcon 12的CPP文件实现视觉检测

    近段时间开始接触halcon,但是在VS2015里面使用,无论是配置还是生产EXE文件,都不如意. 加上网上的教程很多,经过多次测试,其实有很多地方无需修改,如果修改的太多也失去了直接添加封装的意义. ...

  6. about the pageload and page init event

    Page_Init The Page_Init event is the first to occur when an ASP.NET page is executed. This is where ...

  7. pure css兼容IE

    <!--[if lte IE 8]> <link rel="stylesheet" href="pure/0.5.0/grids-responsive- ...

  8. Python超简单的HTTP服务器

    Python超简单的HTTP服务器 安装了python就可以 python -m SimpleHTTPServer 执行这一个命令即可实现一个HTTP服务器,将当前目录设为HTTP服务目录,可以通过h ...

  9. java中的排序

    排序是数据结构中重要的一个部分,也是在实际开发中最易遇到的问题之一,当然了,你也可以不考虑这些排序的算法,直接把要排序的数据insert到数据库中,用数据库的order by再select一下,也能产 ...

  10. Environment 类

    提供有关当前环境和平台的信息以及操作它们的方法. 此类不能被继承. using System; using System.Collections; using System.Collections.G ...