Red and Black

Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 50913   Accepted: 27001

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 题意:@是搜索起点,#不能走,.可以走,.走过一次后会变为#,问从@开始在棋盘上一共可以走几步(@起点算一步)
#include<iostream>
#include<string.h>
#include<string>
#include<algorithm>
using namespace std;
int dir[][]={{,-},{,},{,},{-,}};
string a[];
int n,m,cnt;
int check(int x,int y)
{
if(x>=&&x<n&&y>=&&y<m&&a[x][y]!='#')
return ;
else
return ;
}
void dfs(int x,int y)
{
if(check(x,y)==)
return ;
else
{
a[x][y]='#';
cnt++;
for(int i=;i<;i++)
{
int dx,dy;
dx=x+dir[i][];
dy=y+dir[i][];
dfs(dx,dy);
}
}
}
int main()
{
while(cin>>m>>n&&n&&m)
{
for(int i=;i<n;i++)
cin>>a[i];
cnt=;
for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
{
if(a[i][j]=='@')
{
dfs(i,j);
break;
}
}
}
cout<<cnt<<endl;
}
return ;
}

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. OpenJudge/Poj 1979 Red and Black / OpenJudge 2816 红与黑

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

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

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

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

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

  5. poj 1979 Red and Black(dfs)

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

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

  7. HDOJ 1312 (POJ 1979) Red and Black

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

  8. poj 1979 Red and Black(dfs水题)

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

  9. POJ 1979 Red and Black (DFS)

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

随机推荐

  1. 针对sklearn.svm中的"dual_coef_"理解

    1.决策函数的表达式 公式: 其中: 2.SVM经过训练后,所得到的"dual_coef_" 其实"dual_coef_"就是"ai*yi" ...

  2. 《Interest Rate Risk Modeling》阅读笔记——第八章:基于 LIBOR 模型用互换和利率期权进行对冲

    目录 第八章:基于 LIBOR 模型用互换和利率期权进行对冲 思维导图 推导浮息债在重置日(reset date)的价格 第八章:基于 LIBOR 模型用互换和利率期权进行对冲 思维导图 推导浮息债在 ...

  3. hyfhaha大事记——luogu

    成就墙 AK CSP-J 初赛 AK CSP-J 复赛 CSP- J 一等奖 CSP-S 一等奖 大事记 2017-09-20 13:54 注册洛谷账号 之后洛谷一直处于沉沦状态 2018 2018- ...

  4. SQL查询效率注意事项 2011.12.27

    一.查询条件精确,针对有参数传入情况 二.SQL逻辑执行顺序 FROM-->JOIN-->WHERE-->GROUP-->HAVING-->DISTINCT-->O ...

  5. 笔记-redis-订阅系统

    笔记-redis-订阅系统 1.      发布/订阅pub/sub 1.1.  基本命令 PUBLISH channel message #将信息发送到指定的频道. SUBSCRIBE channe ...

  6. 16 JavaScript计时事件&显示时钟

    计时事件:JavaScript设定一定的时间间隔之后来执行代码 window.setInterval("JavaScript function",millisecons):间隔指定 ...

  7. navicat12破解详细教程

    以管理员身份运行此注册机: 运行注册机 打开注册机后,1) Patch勾选Backup.Host和Navicat v12,然后点击Patch按钮: 默认勾选 找到Navicat Premium 12安 ...

  8. 【剑指Offer面试编程题】 题目1350:二叉树的深度--九度OJ

    题目描述: 输入一棵二叉树,求该树的深度.从根结点到叶结点依次经过的结点(含根.叶结点)形成树的一条路径,最长路径的长度为树的深度. 输入: 第一行输入有n,n表示结点数,结点号从1到n.根结点为1. ...

  9. springboot 跨域

    参考: https://blog.csdn.net/qq779446849/article/details/53102925 https://blog.csdn.net/wo541075754/art ...

  10. 记一次安装体验:pwn工具

    几天前删了JDK,结果和VM在一个目录中,结果VM全没了,重安走起 记载一下安装虚拟机出现的问题,官网一个字....慢,于是找了百度网盘,据说win10版本太低没法用,我就下了vw15.5.0(建立在 ...