题目链接:

pid=1312" target="_blank">HDU1312 / POJ1979 / ZOJ2165 Red and Black(红与黑)

Red and Black

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

Total Submission(s): 9902    Accepted Submission(s): 6158

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

题意:

有一个长方形的房间布满了正方形的瓷砖,瓷砖要么红色要么黑色。一男子站在当中一块黑色瓷砖上,可向上下左右四个方向移动。但不能移动到红色瓷砖上,问他可到达的黑色瓷砖数量。

分析:

DFS搜索。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; int dir[4][2] = {{-1, 0},{0, 1},{1, 0},{0, -1}};
int cnt, W, H;
char mp[21][21];
bool vis[21][21];
void dfs(int x, int y)
{
vis[x][y] = true;
for(int i = 0; i < 4; i++)
{
int tx = x + dir[i][0];
int ty = y + dir[i][1];
if(tx >= 1 && tx <= H && ty >= 1 && ty <= W && !vis[tx][ty] && mp[tx][ty] == '.')
{
cnt++;
dfs(tx, ty);
}
}
}
int main()
{
char c;
int x, y;
while(scanf("%d%d", &W, &H), W, H)
{
scanf("%c", &c);
for(int i = 1; i <= H; i++)
{
for(int j = 1; j <= W; j++)
{
scanf("%c", &mp[i][j]);
if(mp[i][j] == '@')
{
x = i;
y = j;
}
}
scanf("%c", &c);
}
cnt = 1;
memset(vis, false, sizeof(vis));
dfs(x, y);
printf("%d\n", cnt);
}
return 0;
}

HDU1312 / POJ1979 / ZOJ2165 Red and Black(红与黑) 解题报告的更多相关文章

  1. codeforces 399B. Red and Blue Balls 解题报告

    题目链接:http://codeforces.com/problemset/problem/399/B 题目意思:给出 n 个只由 R 和 B 组成的字符串(由上到下排列,相当于栈),问最多可以操作多 ...

  2. HDU 4303 Hourai Jeweled 解题报告

    HDU 4303 Hourai Jeweled 解题报告 评测地址: http://acm.hdu.edu.cn/showproblem.php?pid=4303 评测地址: https://xoj. ...

  3. LeetCode: Sort Colors 解题报告

    Sort ColorsGiven an array with n objects colored red, white or blue, sort them so that objects of th ...

  4. 【LeetCode】147. Insertion Sort List 解题报告(Python)

    [LeetCode]147. Insertion Sort List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...

  5. CH Round #56 - 国庆节欢乐赛解题报告

    最近CH上的比赛很多,在此会全部写出解题报告,与大家交流一下解题方法与技巧. T1 魔幻森林 描述 Cortana来到了一片魔幻森林,这片森林可以被视作一个N*M的矩阵,矩阵中的每个位置上都长着一棵树 ...

  6. 二模13day1解题报告

    二模13day1解题报告 T1.发射站(station) N个发射站,每个发射站有高度hi,发射信号强度vi,每个发射站的信号只会被左和右第一个比他高的收到.现在求收到信号最强的发射站. 我用了时间复 ...

  7. BZOJ 1051 最受欢迎的牛 解题报告

    题目直接摆在这里! 1051: [HAOI2006]受欢迎的牛 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 4438  Solved: 2353[S ...

  8. 习题:codevs 2822 爱在心中 解题报告

    这次的解题报告是有关tarjan算法的一道思维量比较大的题目(真的是原创文章,希望管理员不要再把文章移出首页). 这道题蒟蒻以前做过,但是今天由于要复习tarjan算法,于是就看到codevs分类强联 ...

  9. 习题:codevs 1035 火车停留解题报告

    本蒟蒻又来写解题报告了.这次的题目是codevs 1035 火车停留. 题目大意就是给m个火车的到达时间.停留时间和车载货物的价值,车站有n个车道,而火车停留一次车站就会从车载货物价值中获得1%的利润 ...

随机推荐

  1. devenv.exe 编译Solution

    Build https://docs.microsoft.com/en-us/visualstudio/ide/reference/build-devenv-exe Builds a solution ...

  2. PowerShell攻防进阶篇:nishang工具用法详解

    PowerShell攻防进阶篇:nishang工具用法详解 导语:nishang,PowerShell下并肩Empire,Powersploit的神器. 开始之前,先放出个下载地址! 下载地址:htt ...

  3. poj 1018(枚举+贪心)

                                                                              通讯系统 We have received an o ...

  4. JavaScript:Browser 对象

    ylbtech-JavaScript:Browser 对象 1.  Window 对象返回顶部 1. Window 对象 Window 对象 Window 对象表示浏览器中打开的窗口. 如果文档包含框 ...

  5. Springboot 之 引入Thymeleaf

    转自:https://segmentfault.com/a/1190000011149325 前言 Spring-boot-starter-web集成了Tomcat以及Spring MVC,会自动配置 ...

  6. 随时随地日志Debug

    对于一个应用程序而言,Log必不可少,但是有些时候仅仅想看下输出,如果加log的话就显得比较麻烦,这个时候就用到了Debug.WriteLine("测试下,你好,非常棒,牛叉!") ...

  7. 【Linux】YUM Repositories for CentOS, RHEL & Fedora Systems

    这里是官方wiki:https://wiki.centos.org/AdditionalResources/Repositories 一.简介 YUM(Yellowdog Updater Modifi ...

  8. IT业常见职位英语缩写全攻略及详解

    现在中国人流行起英文名字,连职位也跟着作秀,什么CEO.COO.CFO.CTO.CIO啦,那CEO.COO.CFO.CTO.CIO到底是什么意思呢?总被这些概念搞晕,这可不是搞IT的应该犯的错误哦,好 ...

  9. 个人网站html5雪花飘落代码JS特效下载

    如何给自己的网站/页面添加雪花代码.特效呢?有的网站配合自己的主题模板添加雪花飘落效果挺好看的.特别是与冬天季节相关的主题,很多的博客空间都加了雪花的效果.在网上搜索了几种雪花效果,做了简单的修改,在 ...

  10. Android 消息队列机制

    在非UI线程使用Handler进行线程通信时,一般都需要进行3个步骤: 创建Looper Looper.prepar() 创建Handler 启动消息循环Looper.loop() 通过这3步,基本就 ...