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. 树莓派4B踩坑指南 - (7)root账户及权限设置

    树莓派启用root账户 树莓派默认用户是pi 密码为raspberry. root账户默认无密码,但是账户锁定. 开启root账户命令: sudo passwd root #执行后提示设置密码,输入2 ...

  2. HD Tune检查硬盘各参数的含义

    01 =Read Error Rate / (底层)数据读取错误率指从磁盘表面读取数据时发生的硬件读取错误的比率,Raw值对于不同的厂商有着不同的体系,单纯看做1个十进制数字是没有任何意义的.以上为W ...

  3. jxl读取设置过数据有效性的xls文件报错

    //在用jxl读入excel时,一直报如下错误: Warning: Cannot read drop down range Unrecognized token 43 Exception in thr ...

  4. Python笔记⑤爬虫

    爬虫的前奏 # 爬虫前奏 # 明确目的 # 找到数据对应的网页 # 分析网页的结果找到数据所在的标签位置 # 模拟HTTP请求,向服务器发送这个请求,获取到服务器返回给我们的HTML # 用正则表达式 ...

  5. SpringBoot启动使用elasticsearch启动异常:Received message from unsupported version:[2.0.0] minimal compatible

    SpringBoot启动使用elasticsearch启动异常:Received message from unsupported version:[2.0.0] minimal compatible ...

  6. jqGird错误“decimalSeparator”的解决办法

    在使用jqGrid的过程中,突然出现以下错误: jquery.jqGrid.js:15016 Uncaught TypeError: Cannot read property 'decimalSepa ...

  7. SpringMVC 配置文件详解

    HandlerMapping    处理器映射 HTTP请求被DispatcherServlet拦截后,会调用HandlerMapping来处理,HandlerMapping根据 url<=&g ...

  8. Java日期时间API系列10-----Jdk8中java.time包中的新的日期时间API类的DateTimeFormatter

    1.DateTimeFormatter final修饰,线程安全,用于打印和解析日期-时间对象的格式化程序. 创建DateTimeFormatter: DateTimeFormatter dateTi ...

  9. Kubernetes集群部署及简单命令行操作

    三个阶段部署docker:https://www.cnblogs.com/rdchenxi/p/10381631.html 环境准备 [root@master ~]# hostnamectl set- ...

  10. SELinux永久关闭

    目录 SELinux永久关闭 参考 SELinux三种模式 永久关闭方法 SELinux永久关闭