Red and Black

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6905    Accepted Submission(s): 4384

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
 
 
 
 #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<deque>
#include<iostream>
#define maxn 25
using namespace std;
char maze[maxn][maxn];
int w,h;
/*建立方向树*/
struct node
{
int x,y;
}start;
/*搜索方向*/
int dir[][]=
{
{,},
{,-},
{-,},
{,}
};
void bfs()
{
/*入队,出队用*/
deque<node>q;
/*暂存位置*/
node q1,q2;
q.push_back(start);
while(!q.empty())
{
q1=q.front ();
q.pop_front();
for(int i=;i<;i++)
{
q2.x=q1.x+dir[i][];
q2.y=q1.y+dir[i][];
if(q2.x>h||q2.x<||q2.y>w||q2.y<||maze[q2.x][q2.y]=='#'||maze[q2.x][q2.y]=='p') /*return ;*/
; /*啥也不干,就这么一个逗号....*/
else
{
if(maze[q2.x][q2.y]=='.')
maze[q2.x][q2.y]='p'; /*就暂时用P来代表占位置吧!*/
/*入队*/
q.push_back (q2);
}
}
}
}
int main()
{
int i,j,ans;
while(scanf("%d%d",&w,&h),h+w)
{
getchar();
memset(maze,'\0',sizeof(maze));
for(i=;i<=h;i++)
{
for(j=;j<=w;j++)
{
scanf("%c",&maze[i][j]);
if(maze[i][j]=='@')
{
start.x=i;
start.y=j;
maze[i][j]='#';
}
}
getchar();
}
bfs();
ans=;
for(i=;i<=h;i++)
{
for(j=;j<=w;j++)
{ if(maze[i][j]=='p')
ans++;
}
}
printf("%d\n",ans);
}
return ;
}

bfs+匹配

HDUOJ--------(1312)Red and Black的更多相关文章

  1. 小白安装linux(虚拟机)red hat enterprise linux 6

    额,这篇貌似是我名义上的第一篇博客,但是我好像没有第一写他,没事,都一样.(我会假装它是人生中第一篇博客的) 上大学之后,很久之后才发现自己听喜欢linux的,因为感觉很高大上,所以自己自学了很多关于 ...

  2. Unix/Linux环境C编程入门教程(19)Red Hat Entetprise Linux 7.0环境搭建

    位架构,包括英特尔X-86_64.Power和s390.动态定时能力将降低内核内部中断数量,Open vSwitch 2.0功能可调节虚拟机之间的流量.RHEL 7中默认的文件系统是XFS,包含了一个 ...

  3. HDU1312 / POJ1979 / ZOJ2165 Red and Black(红与黑) 解题报告

    题目链接:pid=1312" target="_blank">HDU1312 / POJ1979 / ZOJ2165 Red and Black(红与黑) Red ...

  4. (转)JQuery处理json与ajax返回JSON实例

    son数据是一种经型的实时数据交互的数据存储方法,使用到最多的应该是ajax与json配合使用了,下面我来给大家介绍jquery处理json数据方法. 一.JSON的一些基础知识. JSON中对象通过 ...

  5. Docker学习总结(一)

    <认识Docker> 不定期更新~~~~~~~ 历史区别: 13年之前:网络大多使用"协议栈堆叠"的形式进行开发,需要部署单一专有的服务器进行操作.包括(中间件,运行时 ...

  6. (转)粒子编辑器Particle designer属性的介绍

    转载:http://blog.csdn.net/ym19860303/article/details/9210539 Particle designer粒子编辑器可到这里下载(包含授权码):http: ...

  7. 新年抢红包效果(New Year Red Packet)

    新年抢红包效果(New Year Red Packet) 晓娜的文章(微信公众号:migufe) 2016即将过去,我们将迎来新的一年2017,这里小编提前祝大家新年快乐!万事如意!那我们新年最开心的 ...

  8. POJ 1979 Red and Black (红与黑)

    POJ 1979 Red and Black (红与黑) Time Limit: 1000MS    Memory Limit: 30000K Description 题目描述 There is a ...

  9. Luogu 1312 【NOIP2011】玛雅游戏 (搜索)

    Luogu 1312 [NOIP2011]玛雅游戏 (搜索) Description Mayan puzzle 是最近流行起来的一个游戏.游戏界面是一个7行5列的棋盘,上面堆放着一些方块,方块不能悬空 ...

  10. red hat 7 启动过程(EFI)

    不同版本的Linux系统的启动过程在某些地方是不一样的,现在先来介绍一下red hat 7 的启动过程(EFI). (加电→图形登录界面) 接通电源 按下电源键 EFI固件启动 初始化硬件 从EFI启 ...

随机推荐

  1. 《Windows核心编程》第五章——作业

    #include <windows.h> #include<iostream> #include <tchar.h> using namespace std; ty ...

  2. vc 获得调用者的模块名称

    void ShowCallerModuleName(void* calleraddr ){ HMODULE hCallerModule = NULL; TCHAR szModuleName[MAX_P ...

  3. 将MyEclipse项目导入到Eclipse中

    1.请首先确保你的eclipse是javaee版本的,或者已经安装wtp插件 2.然后修改eclipse工程下的.project文件: 3.在<natures></natures&g ...

  4. Python3.6学习笔记(六)

    WSGI Python Web Server Gateway Interface 规范学习 由于Python的灵活性,提供了多种方式可以作为服务端语言,包括Python编写的服务器(Medusa).P ...

  5. 【BZOJ】【3053】The Closest M Points

    KD-Tree 题目大意:K维空间内,与给定点欧几里得距离最近的 m 个点. KD树啊……还能怎样啊……然而扩展到k维其实并没多么复杂?除了我已经脑补不出建树过程……不过代码好像变化不大>_&g ...

  6. 定义和使用EL函数

    EL为表达式语言,在EL中,允许定义和使用函数.下面将介绍如何定义和使用EL的函数. 1. 定义和使用函数 函数的定义和使用分为以下3个步骤: (1)编写一个Java类,并在该类中编写公用的静态方法, ...

  7. C语言:通过函数指针来完成两个数的加减乘除

    // //  main.c //  Function_pointer // //  Created by mac on 15/8/2. //  Copyright (c) 2015年. All rig ...

  8. [15] 星星(Star)图形的生成算法

    顶点数据的生成 bool YfBuildStarVertices ( Yreal radius, Yreal assistRadius, Yreal height, Yuint slices, YeO ...

  9. 《Small Memory Software:Patterns For System With Limited Memory》读书笔记

    原文地址:http://blog.csdn.net/jinzhuojun/article/details/13297447 虽然摩尔定律让我们的计算机硬件得以以指数速度升级,但反摩尔定律又不断消减这些 ...

  10. hdu3117之矩阵快速幂

    Fibonacci Numbers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...