Red and Black

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 20799    Accepted Submission(s): 12664

Problem 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)
 
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
 
Recommend
Eddy   |   We have carefully selected several similar problems for you:  1253 1240 1072 1181 1175 
就是求包括'@'在内的一个连同块里有几个方块就OK了,DFS;
代码如下:

Problem : 1312 ( Red and Black )     Judge Status : Accepted

RunId : 21281946    Language : G++    Author : hnustwanghe

Code Render Status : Rendered By HDOJ G++ Code Render Version 0.01 Beta

#include<iostream>
#include<cstring>
#include<cstdio>

using namespace std;
const int N = 20 + 5;
char mat[N][N];
bool visit[N][N];
int n,m;
const int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};

void DFS(int x,int y){
if(x<0 || x>=n || y<0 || y>=m || visit[x][y] || mat[x][y]!='.') return ;
visit[x][y] = true;
for(int d=0;d<4;d++){
int newx = x + dir[d][0];
int newy = y + dir[d][1];
DFS(newx,newy);
}
}
int main(){
while(scanf("%d %d",&m,&n)==2 && (n||m)){
int x=0,y=0;
for(int i=0;i<n;i++){
scanf("%s",mat[i]);
for(int j=0;j<m;j++)
if(mat[i][j]=='@')
x = i,y = j;
}
memset(visit,0,sizeof(visit));
mat[x][y]='.';
DFS(x,y);
int cnt=0;
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
if(mat[i][j]=='.' && visit[i][j])
cnt++;
printf("%d\n",cnt);
}
}

#include<iostream>
#include<cstring>
#include<cstdio>

using namespace
std;
const int
N = 20 + 5;
char
mat[N][N];
bool
visit[N][N];
int
n,m;
const int
dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}}; void DFS(int x,int y){
if(
x<0 || x>=n || y<0 || y>=m || visit[x][y] || mat[x][y]!='.') return ;
visit[x][y] = true;
for(int
d=0;d<4;d++){
int
newx = x + dir[d][0];
int
newy = y + dir[d][1];
DFS(newx,newy);
}
}
int main(){
while(
scanf("%d %d",&m,&n)==2 && (n||m)){
int
x=0,y=0;
for(int
i=0;i<n;i++){
scanf("%s",mat[i]);
for(int
j=0;j<m;j++)
if(
mat[i][j]=='@')
x = i,y = j;
}

memset(visit,0,sizeof(visit));
mat[x][y]='.';
DFS(x,y);
int
cnt=0;
for(int
i=0;i<n;i++)
for(int
j=0;j<m;j++)
if(
mat[i][j]=='.' && visit[i][j])
cnt++;
printf("%d\n",cnt);
}
}




 

搜索专题: HDU1312Red and Black的更多相关文章

  1. HDU(搜索专题) 1000 N皇后问题(深度优先搜索DFS)解题报告

    前几天一直在忙一些事情,所以一直没来得及开始这个搜索专题的训练,今天做了下这个专题的第一题,皇后问题在我没有开始接受Axie的算法低强度训练前,就早有耳闻了,但一直不知道是什么类型的题目,今天一看,原 ...

  2. NOIP2018提高组金牌训练营——搜索专题

    NOIP2018提高组金牌训练营——搜索专题 1416 两点 福克斯在玩一款手机解迷游戏,这个游戏叫做”两点”.基础级别的时候是在一个n×m单元上玩的.像这样: 每一个单元有包含一个有色点.我们将用不 ...

  3. 搜索专题:Balloons

    搜索专题:Balloons 这道题一看与时间有关,第一想到的就是BFS,定义一个状态,包含每一个状态的剩余气球数,已经进行的时间和每一个志愿者上一次吹气球的时间: 每一次状态转换时,检查是否有没有使用 ...

  4. 2014 UESTC暑前集训搜索专题解题报告

    A.解救小Q BFS.每次到达一个状态时看是否是在传送阵的一点上,是则传送到另一点即可. 代码: #include <iostream> #include <cstdio> # ...

  5. 【PHP高效搜索专题(2)】sphinx&coreseek在PHP程序中的应用实例

    PHP可以通过三种途径来调用sphinx 通过Sphinx官方提供的API接口(接口有Python,Java,Php三种版本) 通过安装SphinxSE,然后创建一个中介sphinxSE类型的表,再通 ...

  6. 【PHP高效搜索专题(1)】sphinx&Coreseek的介绍与安装

    我们已经知道mysql中带有"%keyword%"条件的sql是不走索引的,而不走索引的sql在大数据量+大并发量的时候,不仅效率极慢还很有可能让数据库崩溃.那我们如何通过某些关键 ...

  7. 2015 UESTC 搜索专题F题 Eight Puzzle 爆搜

    Eight Puzzle Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/61 ...

  8. 2015 UESTC 搜索专题B题 邱老师降临小行星 记忆化搜索

    邱老师降临小行星 Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/61 Des ...

  9. 蓝桥杯dfs搜索专题

    2018激光样式 #include<bits/stdc++.h> using namespace std; /* dfs(i) 第i个激光机器 有两种选择:vis[i-1] == 0 时 ...

随机推荐

  1. python+selenium封装UI自动化框架

    seleinum框架 框架的思想:  解决我们测试过程中的问题:大量的重复步骤,用自动化来实现    1)配置和程序的分离    2)测试数据和程序的分离    3)不懂编程的人员可以方便使用:使用的 ...

  2. UNIX标准C - socket套接字

    一.计算机网络 1.计算机网络的功能 a.数据通信 b.资源共享 c.提高系统的可靠性 d.分布式网络处理和负载均匀. 2.计算机网络的组成 1.通信子网:由网卡.线缆.集线器.中继器.交换器.路由器 ...

  3. go语言系列--输出正弦函数

    实验所用到的标准库和包 库与包之间的理解可以类比成:数据库种的库和表 库名 作用 image 常见图形格式的访问及生成 log 日志记录库 math 数学库 os 操作系统平台不依赖平台操作封装 查看 ...

  4. 前端iPhone X适配总结

    屏幕尺寸 垂直方向上,iPhone X的显示宽度与iPhone 6,iPhone 7 和 iPhone 8 的 4.7 英寸一样,但是比4.7英寸的显示屏高145pt. 安全区域 安全区域指的是一个可 ...

  5. Java中String类中常用的方法

    1.字符串与字符数组的转换 用toCharArray()方法将字符串变为字符数组 String str = "abcdef"; char c[] = str.tocharArray ...

  6. Python的datetime与Decimal数据进行json序列化的简单说明

    我们在Python的json.JSONEncoder类中可以查看Python数据序列化为JSON格式的数据时数据类型的对应关系: class JSONEncoder(object): "&q ...

  7. 常见iPhone设备尺寸及分辨率(持续更新)

    开发中常用的px与pt区别 px就是表示pixel,像素,是屏幕上显示数据的最基本的点: pt就是point,是印刷行业常用单位,等于1/72英寸. px全称为pixel,是一个点,它不是自然界的长度 ...

  8. 配置Log4j(很详细)【转】

    来自: http://www.blogjava.net/zJun/archive/2006/06/28/55511.html Log4J的配置文件(Configuration File)就是用来设置记 ...

  9. Linux_OpenSSH远程连接

    目录 目录 SSH server SSH服务属性 SSH协议执行原理 SSH连接方式 ssh Commands scp commands sftp commands SSH在生产环境中的使用 系统间备 ...

  10. Python学习之==>日志模块

    一.logging模块介绍 logging是Python中自带的标准模块,是Python中用来操作日志的模块. 1.控制台输出日志 import logging logging.basicConfig ...