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. java应用监控工具

    http://hao.jobbole.com/category/java/java-monitoring/

  2. 【netcore基础】ConcurrentDictionary 使用字符串作为key给代码加锁且使用EF事物防止并发调用数据混乱的问题

    业务场景需要锁住指定的字符串下的代码,防止并发创建多个订单 这里我们使用 ConcurrentDictionary 首先初始化一个字典 private static readonly Concurre ...

  3. 防止vue文件中的样式出现‘污染’情况(html5 scoped特性)

    近期在项目中出现了vue样式污染的情况: 一个页面刚进去时样式不正常,刷新之后,样式才才达到预期那样 在vue中,如果把样式写在vue文件的 style中,可能会出现样式污染的情况,这是要把写样式的标 ...

  4. 支持多文件上传,预览,拖拽,基于bootstrap的上传插件fileinput 的ajax异步上传(转载)

    首先需要导入一些js和css文件 <link href="__PUBLIC__/CSS/bootstrap.css" rel="stylesheet"&g ...

  5. elk-(七)

    最终架构确定为  logs--->blieb--->redis/kafka--->logstash--->es--->kibana 注意:  geoip下载地址: wge ...

  6. 补充:ajax post 方式请求

    1. 什么是ajax Ajax: asynchronous  javascript  and  xml (异步js和xml) 其是可以与服务器进行(异步/同步)交互的技术之一. ajax的语言载体是j ...

  7. 利用excel模板,将数据填充到excel中

    package com.excel;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundExce ...

  8. day20 二十、加密模块、操作配置文件、操作shell命令、xml模块

    一.加密模块 1.hashlib模块:加密 ①有解密的加密方式 ②无解密的加密方式:碰撞检查 -- 1)不同数据加密后的结果一定不一致 -- 2)相同数据的加密结果一定是一致的 import hash ...

  9. 算法基础_递归_求杨辉三角第m行第n个数字

    问题描述: 算法基础_递归_求杨辉三角第m行第n个数字(m,n都从0开始) 解题源代码(这里打印出的是杨辉三角某一层的所有数字,没用大数,所以有上限,这里只写基本逻辑,要符合题意的话,把循环去掉就好) ...

  10. MongoDB分片集群原理、搭建及测试详解

    随着技术的发展,目前数据库系统对于海量数据的存储和高效访问海量数据要求越来越高,MongoDB分片机制就是为了解决海量数据的存储和高效海量数据访问而生. MongoDB分片集群由mongos路由进程( ...