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. 【视频分享】Liger UI实战集智建筑project管理系统配商业代码(打印报表、角色式权限管理)

    QQ 2059055336 课程讲师:集思博智 课程分类:.net 适合人群:中级 课时数量:23课时 用到技术:Liger UI框架.AJAX.JSON数据格式的序列化与反序列化.角色的交叉权限管理 ...

  2. strstr实现

    // strstr.c查找完全匹配的子字符串 #include<stdio.h> #include<string.h> char *my_strstr(const char * ...

  3. @JVM中对象的引用类型

    JVM中有四种引用类型:强引用.软引用.弱引用.虚引用   强引用(Stong Reference):是指在程序代码中普遍存在的,类似:Object obj = new Object()这类的引用,只 ...

  4. sql server2005 express和Northwind数据库安装

    最近在学<C#入门经典>的数据库章节时,发现机子上既没有sql server又没书中所说的northwind数据库,想立刻运行下第一个工程DataReading都没法进行.在网上折腾后有了 ...

  5. Linux,Windows和UNIX的进程调度的分析

    摘要 : 本文以Linux ,Unix ,Windows 操作系统为例,分析其进程调度策略,以期对进程调度过程有更深层次的认识     关键词 : 进程调度 优先级 时间片轮转 实时进程 分时技术   ...

  6. WF4.0(1)---WorkFlow简介

    编程编的越久就发现自己以前的语文真的没学好,写个随笔取个名字都需要思考半天,以前工作的时候只是听说过工作流,知道的范围仅限于工作流在OA审批流程中用的比较多,现在自己实实在在的用工作流也做过不少项目, ...

  7. Android生成带图片的二维码

    一.问题描述 在开发中需要将信息转换为二维码存储并要求带有公司的logo,我们知道Google的Zxing开源项目就很好的帮助我们实现条形码.二维码的生成和解析,但带有logo的官网并没有提供demo ...

  8. T-SQL with as 的用法(转) SQL 下的递归查询 SQL2005(CTE) ,SQL2000(Function 递归)

    摘自: http://blog.csdn.net/bluefoxev/article/details/6779794 ------- SQL2005 方法 一.WITH AS的含义     WITH ...

  9. GO语言基础之struct

    结构struct 1. Go 中的struct与C中的struct非常相似,并且Go没有class 2. 使用 type <Name> struct{} 定义结构,名称遵循可见性规则(即首 ...

  10. ftp-ftp权限

    在服务器上创建ftp站点时勾选的是读写权限对所有的用户开放,但是发现有些用户还是只能读取不能写入,后来发现是因为ftp指向的文件夹本身的权限没有打开导致的,解决办法是,设置ftp指向的文件夹的权限为u ...