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

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) 
The end of the input is indicated by a line consisting of two zeros. 

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

Source

 
神坑的是:xy方向需要换一下
 
 #include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
char a[][];
int n,m;
int res=;
int dx[]={,-,,};
int dy[]={,,,-};
void dfs(int x,int y)
{
res++;
a[x][y]='#';
for(int i=;i<;i++){
int nx=x+dx[i],ny=y+dy[i];
if(nx>=&&nx<n&&ny>=&&ny<m&&a[nx][ny]=='.'){
dfs(nx,ny);
}
}
return ;
}
void solve()
{
for(int i=;i<n;i++){
for(int j=;j<m;j++){
if(a[i][j]=='@'){
dfs(i,j);
}
}
}
}
int main()
{
while(cin>>m>>n&&n!=&&m!=){ res=;
for(int i=;i<n;i++){
for(int j=;j<m;j++){
cin>>a[i][j];
}
}
solve();
cout<<res<<endl;
}
return ;
}

脱离参考书自己再根据自己的理解过一遍:

 #include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>
using namespace std;
int n,m;
char a[][];
int sx,sy,nx,ny;
int dx[]={,,,-};
int dy[]={,-,,};
int res;
void dfs(int x,int y)
{
res++;
a[x][y]='#';
for(int i=;i<;i++){
nx=x+dx[i],ny=y+dy[i];
if(nx>=&&nx<m&&ny>=&&ny<n&&a[nx][ny]=='.'){
dfs(nx,ny);
}
}
}
int main()
{
while(cin>>n>>m&&(n&&m)){
for(int i=;i<m;i++){
for(int j=;j<n;j++){
cin>>a[i][j];
if(a[i][j]=='@'){
sx=i,sy=j;
}
}
}
res=;
dfs(sx,sy);
cout<<res<<endl;
}
return ;
}

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

  1. POJ-1979 Red and Black(DFS)

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

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

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

  4. poj-1979 red and black(搜索)

    Time limit1000 ms Memory limit30000 kB There is a rectangular room, covered with square tiles. Each ...

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

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

  6. POJ 1979 Red and Black (DFS)

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

  7. HDU1312 / POJ1979 / ZOJ2165 Red and Black(红与黑) 解题报告

    题目链接:pid=1312" target="_blank">HDU1312 / POJ1979 / ZOJ2165 Red and Black(红与黑) Red ...

  8. POJ 1979 Red and Black (红与黑)

    POJ 1979 Red and Black (红与黑) Time Limit: 1000MS    Memory Limit: 30000K Description 题目描述 There is a ...

  9. LeetCode Subsets II (DFS)

    题意: 给一个集合,有n个可能相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: 看这个就差不多了.LEETCODE SUBSETS (DFS) class Solution { publ ...

随机推荐

  1. Spring Security的几个重要词

    1.SecurityContextHolder:是安全上下文容器,可以在此得知操作的用户是谁,该用户是否已经被认证,他拥有哪些角色权限…这些都被保存在SecurityContextHolder中. O ...

  2. 【Zookeeper系列】zookeeper面试题(转)

    原文链接:https://segmentfault.com/a/1190000014479433 1.ZooKeeper是什么? ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,是 ...

  3. Q - Girls and Boys

    来源poj1068 In the second year of the university somebody started a study on the romantic relations be ...

  4. poj 2069

    唔. 这道题的火候比较巧妙. 我们是每次找到一个最远的点,然后向那个最远点逼近. 这显然非常合理. #include <cstdlib> #include <cmath> #i ...

  5. jquery和ajax和springmvc

    <script type="text/javascript" src="js/jquery-1.11.1.js"></script> $ ...

  6. 矩阵取数问题(dp,高精)

    题目描述 帅帅经常跟同学玩一个矩阵取数游戏:对于一个给定的n×mn \times mn×m的矩阵,矩阵中的每个元素ai,ja_{i,j}ai,j​均为非负整数.游戏规则如下: 每次取数时须从每行各取走 ...

  7. hadoop伪分布环境快速搭建

    1.首先下载一个完成已经进行简单配置好的镜像文件(hadoop,HBASE,eclipse,jdk环境已经搭建好,tomcat为7.0版本,建议更改为tomcat8.5版本,运行比较稳定). 2安装V ...

  8. python的几个小程序

    ##九九乘法口诀 num1=1 while num1<10: num2=1 while num2<=num1: print(num2,"*",num1,"=& ...

  9. oracle索引分类

    参考文档:https://wenku.baidu.com/view/d4d6ee1ba76e58fafab00336.html https://blog.csdn.net/u010719917/art ...

  10. Vue中computed,methods 和watch

    Vue中的计算属性和方法属性 1.计算属性 computed 模版中可以使用表达式 <div id="example"> {{ message.split('').re ...