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. time时间库使用示例

    time时间库主要有以下几个方法 1. 生成struct_time ,然后就可以很方便的获取到年月日,时分秒等信息 time.localtime() 2. 生成时间戳 time.time() 3. 将 ...

  2. DevOps之持续集成SonarQube代码质量扫描

    一.SonarQube介绍       SonarQube是一个用于代码质量检测管理的开放平台,可以集成不同的检测工具,代码分析工具,以及持续集成工具.SonarQube 并不是简单地把不同的代码检查 ...

  3. Jenkins-ssh远程执行nohup- java无法退出

    一,初步 #执行方式 ssh 192.168.2.103 " nohup java -jar /home/a/ipf/ight/feedback/ixxxedback-platform-1. ...

  4. git windows下换行符问题

    不同操系统下的换行符 CR回车 LF换行 Windows/Dos CRLF \r\n Linux/Unix LF \n MacOS CR \r 1.执行git config --get core.au ...

  5. ExcelUtils

    本ExcelUtils工具类是用poi写的,仅用于线下从excel文件中读取数据.如果生产环境要用的话,建议切换到阿里的easyexcel. 引入poi.jar: <!-- https://mv ...

  6. 配置文件:android:inputType参数类型说明

    输入字符 android:inputType="none"  --输入普通字符  android:inputType="text" --输入普通字符  andr ...

  7. R 常用代码段

    #用来根据不同的细胞来源重新画TSNE图cell_source <-colnames(immune_nobatch) cell_type <- strsplit(cell_source,s ...

  8. css使用1

    一.引入css的三种方式 一.CSS(Cascading Style Sheet):层叠样式表 二.CSS样式由两个组成部分:选择器和声明.声明又包括属性和属性值.每个声明之后用分号结束 语法结构 选 ...

  9. sqli-lab(14)

    POST型的 双注入 0X01随便测试一下 在password输入"会报错  "#就不报错了 那么应该是“”的闭合 但是没有回显的值 只有报错的信息 那我们是不是该考虑从报错的语句 ...

  10. 20165220课程设计个人报告——Part4-Cortex M4模块

    个人报告: 20165220 葛宇豪 1.个人贡献 a.实验环境搭建 b.代码分析与理解 2.设计中遇到的问题以及解决方案 问题1:mdk5每次编译之前都会直接闪退 刚开始以为是环境问题,后来上网查资 ...