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

    已知n,求n中取k(k<=n)个数组成的m(m<=n)个的集合的排列数. 于是,可以枚举选出k个数及枚举m个集合.这个很明显是二类斯特林数.而集合有序,则乘上m! #include < ...

  2. OSGI项目中获取文件路径

    假设想依据给定的文件名创建一个File实例,你可能会这么写: File file = new File(当前类.class.getResource("config").toURI( ...

  3. lambda的函数式接口

    函数式接口就是只包含一个抽象方法的接口A(不包括默认抽象方法,但包括继承来的方法):这个接口用来作为一个可变作用的方法B的参数.函数式接口的抽象方法的参数类型和返回值就是一套签名,这个签名叫做函数描述 ...

  4. 从头认识java-15.3 使用HashSet须要注意的地方

    这一章节我们来讨论一下使用Set的各种实现须要注意的地方. Set接口的经常使用实现类有:HashSet.TreeSet,LinkedHashSet 1.HashSet 大家对于HashSet的印象都 ...

  5. JTextArea 加入滚动条

    JTextArea texA; JScrollPane scroll; TextEdit(String name){ super(name); init(); } void init(){ this. ...

  6. 前端模块化 | 解读JS模块化开发中的 require、import 和 export

    本篇分为两个部分 第一部分:总结了ES6出现之前,在当时现有的运行环境中,实现"模块"的方式: 第二部分:总结了ES6出现后,module成为ES6标准,客户端实现模块化的解决方案 ...

  7. 10 quick tips for Redis

    Redis is hot in the tech community right now. It's come a long way from being a small personal proje ...

  8. POJ 3320 Jessica's Reading Problem (尺取法,时间复杂度O(n logn))

    题目: 解法:定义左索引和右索引 1.先让右索引往右移,直到得到所有知识点为止: 2.然后让左索引向右移,直到刚刚能够得到所有知识点: 3.用右索引减去左索引更新答案,因为这是满足要求的子串. 4.不 ...

  9. swift语言点评十二-Subscripts

    Classes, structures, and enumerations can define subscripts, which are shortcuts for accessing the m ...

  10. 路飞学城Python-Day10(practise)

    作业:现要求你写一个简单的员工信息增删该查程序,需求如下:当然此表在文件存储时可以这样表示1,Alex Li,22,13651054608,IT,2013-04-012,Jack Wang,28,13 ...