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. 安装caffe碰到的坑(各种.so未找到)

    ./include/caffe/common.hpp:4:32: fatal error: boost/shared_ptr.hpp: 没有那个文件或目录 所有类似于上面的错误,都可以用如下格式来解决 ...

  2. [转] 数值优化(Numerical Optimization)学习系列-目录

    from:https://blog.csdn.net/fangqingan_java/article/details/48951191 概述数值优化对于最优化问题提供了一种迭代算法思路,通过迭代逐渐接 ...

  3. 185.[USACO Oct08] 挖水井 (第三次考试大整理)

    185. [USACO Oct08] 挖水井 输入文件:water.in   输出文件:water.out   简单对比 时间限制:1 s   内存限制:128 MB 农夫约翰决定给他的N(1< ...

  4. ELK结合logback

    之前ELK的安装可以查看前面一篇博客 下面是我的logback的配置文件,通过logback的appender直接导入logstash <?xml version="1.0" ...

  5. Loadrunner参数化避免重复数据

    1.我们性能测试过程中经常遇到需要创建很多数据来运行测试场景,但是如果数据准备不够多,可能会造成数据不够用,导致场景运行失败,下面简单的例子: 2.我们对用户名分别使用VuserID和lteratio ...

  6. springboot上传文件大小限制的配置

    springboot配置文件: application.properties #配置文件传输 spring.servlet.multipart.enabled =true spring.servlet ...

  7. MongoDB通过JavaDriver执行shell命令,例如创建sharding collection

    Mongodb的java driver本身的接口 void createCollection(String collectionName, CreateCollectionOptions create ...

  8. linux 设置 hugepage

    临时设置 hugepage > /sys/kernel/mm/hugepages/hugepages-16384kB/nr_hugepages 查看是否设置成功 cat /proc/meminf ...

  9. 使用System.ComponentModel.DataAnnotations验证字段数据正确性

    在.NET MVC 中,当页面提交model到Action的时候,自动填充ModelState.使用ModelState.IsValid进行方便快捷的数据验证,其验证也是调用命名空间System.Co ...

  10. GET,POST传值总结

    GET和POST是什么?HTTP协议中的两种发送请求的方法. HTTP是什么?HTTP是基于TCP/IP的关于数据如何在万维网中如何通信的协议. 其实,GET和POST本质上两者没有任何区别.他们都是 ...