Red and Black

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 14311    Accepted Submission(s): 8870

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
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
char map[500][500];
int n,m,ans,vis[500][500];
void dfs(int x,int y)
{
if(x<0||x>=m||y<0||y>=n||map[x][y]=='#'||vis[x][y])
return ;
vis[x][y]=1;
ans++;
dfs(x+1,y);
dfs(x,y+1);
dfs(x-1,y);
dfs(x,y-1);
}
int main()
{
while(scanf("%d%d",&n,&m),n||m)
{
int ex,ey;
memset(map,'\0',sizeof(map));
for(int i=0;i<m;i++)
{
scanf("%s",&map[i]);
for(int j=0;j<n;j++)
{
if(map[i][j]=='@')
{
ex=i;ey=j;map[i][j]='.';
}
}
}
ans=0;
memset(vis,0,sizeof(vis));
dfs(ex,ey);
printf("%d\n",ans);
}
return 0;
}

hdoj--1312--Red and Black(dfs)的更多相关文章

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

  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(DFS搜索,入门题)

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

  4. Red and Black---hdu1312(dfs)

    2015-04-07http://acm.hdu.edu.cn/showproblem.php?pid=1312 Sample Input 6 9....#......#............... ...

  5. Poj1979 Red and Black (DFS)

    Red and Black Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 47466   Accepted: 25523 D ...

  6. HDU1312 Red and Black(DFS) 2016-07-24 13:49 64人阅读 评论(0) 收藏

    Red and Black Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total ...

  7. POJ-1979 Red and Black(DFS)

    题目链接:http://poj.org/problem?id=1979 深度优先搜索非递归写法 #include <cstdio> #include <stack> using ...

  8. HDU 1312 Red and Black(bfs)

    Red and Black Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Descr ...

  9. POJ 1979 Red and Black (DFS)

    Description There is a rectangular room, covered with square tiles. Each tile is colored either red ...

  10. poj-1979 && hdoj - 1312 Red and Black (简单dfs)

    http://poj.org/problem?id=1979 基础搜索. #include <iostream> #include <cstdio> #include < ...

随机推荐

  1. 微信企业号开发:微信用户信息和web网页的session的关系

         微信企业号的用户是须要验证的,因此能关注企业号的用户事实上就是已经通过验证的用户.但企业应用中打开一个网页,在这个网页中怎样依据微信用户的信息创建web应用中最长使用的session呢?微信 ...

  2. MongoDB之Java測试代码(DAO层)

    MongoInit.java是数据库初始化及连接类 MongoUtils.java是对mongodb的各种操作方法 MongoInit.java package com.wlwcloud.datate ...

  3. class.forName的官方使用方法说明

    原文地址:http://yanwushu.sinaapp.com/class_forname/ 使用jdbc方式链接数据库时会常常看到这句代码:Class.forName(String classNa ...

  4. 棋盘覆盖问题python3实现

    在2^k*2^k个方格组成的棋盘中,有一个方格被占用,用下图的4种L型骨牌覆盖全部棋盘上的其余全部方格,不能重叠. 代码例如以下: def chess(tr,tc,pr,pc,size): globa ...

  5. Compiler Warning (level 2) CS0436

    https://docs.microsoft.com/en-us/dotnet/csharp/misc/cs0436 // CS0436_a.cs // compile with: /target:l ...

  6. rsync来传输文件(可断点续传)

    scp传文件的话如果出错就得重新来过, 用rsync可以实现断点上传的功能   大概就是这样用:  rsync -P --rsh=ssh home.tar 192.168.205.34:/home/h ...

  7. cookie、sessionStorage和localStorage

    title: cookie.sessionStorage和localStorage toc: false date: 2018-09-25 16:49:57 cookie 由于HTTP协议是无状态的, ...

  8. php——get与post方法(转)

    file_get_contents版本: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 <?php /**  * 发送p ...

  9. Python3基础笔记---线程与进程

    参考博客:Py西游攻关之多线程(threading模块) 一.并发与并行的区别 并发:交替做不同事的能力并行:同时做不同事的能力 行话解释:并发:不同代码块交替执行的性能并行:不同代码块同时执行的性能 ...

  10. NodeJS学习笔记 (9)网络服务-https(ok)

    模块概览 这个模块的重要性,基本不用强调了.在网络安全问题日益严峻的今天,网站采用HTTPS是个必然的趋势. 在nodejs中,提供了 https 这个模块来完成 HTTPS 相关功能.从官方文档来看 ...