Time limit1000 ms

Memory limit30000 kB

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 题意:红色和黑色的地砖,只能走黑色的地砖,问最多可以走几块地砖
题解:dfs
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <vector>
#include <list>
using namespace std;
#define PI 3.14159265358979323846264338327950
#define INF 0x3f3f3f3f3f3f3f3f; char a[][];
int vis[][];
int m,n,st,en,sum; void dfs(int x,int y)
{
a[x][y]='#';
sum++;
if(x->= && a[x-][y]=='.')
dfs(x-,y);
if(x+<n && a[x+][y]=='.')
dfs(x+,y);
if(y->= && a[x][y-]=='.')
dfs(x,y-);
if(y+<m && a[x][y+]=='.')
dfs(x,y+);
} int main()
{
while(scanf("%d %d",&m,&n) && (m||n))
{
sum=;
int i,j;
memset(vis,,sizeof(vis));
for( i=;i<n;i++)
for(j=;j<m;j++)
{
cin>>a[i][j];
if(a[i][j]=='@')
{
st=i;
en=j;
}
}
int x=st,y=en;
a[x][y]='#';
if(x->= && a[x-][y]=='.')
dfs(x-,y);
if(x+<n && a[x+][y]=='.')
dfs(x+,y);
if(y->= && a[x][y-]=='.')
dfs(x,y-);
if(y+<m && a[x][y+]=='.')
dfs(x,y+);
printf("%d\n",sum);
}
}

poj-1979 red and black(搜索)的更多相关文章

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

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

  2. POJ 1979 Red and Black 四方向棋盘搜索

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

  3. OpenJudge/Poj 1979 Red and Black / OpenJudge 2816 红与黑

    1.链接地址: http://bailian.openjudge.cn/practice/1979 http://poj.org/problem?id=1979 2.题目: 总时间限制: 1000ms ...

  4. poj 1979 Red and Black(dfs)

    题目链接:http://poj.org/problem?id=1979 思路分析:使用DFS解决,与迷宫问题相似:迷宫由于搜索方向只往左或右一个方向,往上或下一个方向,不会出现重复搜索: 在该问题中往 ...

  5. poj 1979 Red and Black 题解《挑战程序设计竞赛》

    地址 http://poj.org/problem?id=1979 Description There is a rectangular room, covered with square tiles ...

  6. POJ 1979 Red and Black dfs 难度:0

    http://poj.org/problem?id=1979 #include <cstdio> #include <cstring> using namespace std; ...

  7. POJ 1979 Red and Black (zoj 2165) DFS

    传送门: poj:http://poj.org/problem?id=1979 zoj:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problem ...

  8. HDOJ 1312 (POJ 1979) Red and Black

    Problem Description There is a rectangular room, covered with square tiles. Each tile is colored eit ...

  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 Red and Black (DFS)

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

随机推荐

  1. SpringBoot---Web开发

    一.概述 1.SpringBoot提供了spring-boot-starter-web为 web开发 予以支持: 2.spring-boot-starter-web提供了 内嵌的Tomcat 以及 S ...

  2. (转)io各层次性能汇总及运行速度对比

    io各层次性能汇总:以上图片可以清晰的解释io的运行效率 守护进程:持续保持运行着的程序 进程:放在内存中运行的程序 程序:代码文件,php,java

  3. ssas 为绑定指定的大小太小,导致一个或多个列值被截断

    错误信息:ssas 为绑定指定的大小太小,导致一个或多个列值被截断 如果更改了某个维度或是事实表的字段长度,在处理CUBE时提示此错误,我们要做以下更新: 1.刷新数据源视图. 2.打开多维数据集,查 ...

  4. Java开发笔记(九十七)利用Runnable启动线程

    前面介绍了线程的基本用法,按理说足够一般的场合使用了,只是每次开辟新线程,都得单独定义专门的线程类,着实开销不小.注意到新线程内部真正需要开发者重写的仅有run方法,其实就是一段代码块,分线程启动之后 ...

  5. RHEL/CentOS 7.X 安装笔记

    配置vim # 配置 vim 编辑器 yum install vim rm /usr/bin/vi ln -s /usr/bin/vim /usr/bin/vi vi /etc/vimrc # 开启语 ...

  6. python-day1作业(感谢视频老师留的作业)

    __author__ = 'zht' #!/usr/bin/env python # -*- coding: utf-8 -*- ''' #努力学习每一天 ''' #尝试次数计数器 tries = 0 ...

  7. js的加密和解密

    最近在研究js的加密和解密的问题,上网上搜出来很多方法,不过不知道到底哪一个会比较管用.这里是今天找到的一些关于base64加密解密的js代码,已经经过试验,可以使用,不过网上很多加密解密的工具,这种 ...

  8. 关于学习Lisp的一点思考

    以前读<黑客与画家>,其中对Lisp语言的赞美和推崇,让我燃起学习Lisp语言的强烈冲动,但很快发现在实际工作中应用的场景很少,出于功利心最终放弃了.直到上周未在家里读完了<大教堂与 ...

  9. SAP CRM和C4C的客户主数据修改历史记录查询

    SAP CRM 随便修改一个字段,比如给Search Term维护值"webpack": Change History assignment block里显示出了这条修改记录: 根 ...

  10. Codeforces Round #321 (Div. 2) C Kefa and Park(深搜)

    dfs一遍,维护当前连续遇到的喵的数量,然后剪枝,每个统计孩子数量判断是不是叶子结点. #include<bits/stdc++.h> using namespace std; ; int ...