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. post文件的html

    <HTML> <HEAD> <META NAME="GENERATOR" Content="Microsoft Visual Studio& ...

  2. C++:友元运算符重载函数

    运算符重载函数:实现对象之间进行算数运算,(实际上是对象的属性之间做运算),包括+(加号).-(减号).*./.=.++.--.-(负号).+(正号) 运算符重载函数分为:普通友元运算符重载函数.成员 ...

  3. JDBC 通过PreparedStatement 对数据库进行增删改查

    1 插入数据 public boolean ChaRu3(User user){ boolean flag=true; Connection conn=null; PreparedStatement ...

  4. 纯净版xp系统在局域网共享需要密码如何解决

    纯净版xp系统在局域网共享需要密码怎么办?这是近来不少朋友都向小编反馈的问题.他们表示每次共享者更改密码后其他人都需要再重新输入密码,感觉十分的麻烦.下面是系统城小编给大家整理的一些有关XP系统局域网 ...

  5. C#读写txt文件的两种方法介绍[转]

    C#读写txt文件的两种方法介绍 1.添加命名空间 System.IO; System.Text; 2.文件的读取 (1).使用FileStream类进行文件的读取,并将它转换成char数组,然后输出 ...

  6. js实现trim() JS去掉首尾空格 JS去掉两头空格

    function trimStr(str){ return str.replace(/(^\s*)|(\s*$)/g,""); } 用的时候就是直接 var 变量=trimStr( ...

  7. 锐浪报表 导出 PDF ANSI码 乱码 问题解决

    锐浪 报表 导出PDF时如果 ANSI 码 打勾了会乱码,能将这个选项默认不打勾吗 //在报表导出事件脚本里写脚本,可实现导出控制Sender.AsE2PDFOption.AnsiTextMode=0 ...

  8. Linux服务器权限管理之sudo高级应用

    Sudo是允许系统管理员让普通用户执行一些或者全部的root命令的一个工具,减少了root用户的登陆和管理时间,提高了安全性,Sudo不是对shell的一个代替,它是面向每个命令的. Linux系统的 ...

  9. Oracle设置为自启动

    Oracle设置为自启动 学习了:http://blog.itpub.net/31015730/viewspace-2148412/ oracle自带dbstart命令,主要进行系统启动设置: 在/e ...

  10. JNDI配置c3p0连接池

    JNDI是什么呢? 就是java命名和文件夹接口.是SUN公司提供的一种标准的Java命名系统接口. 不好理解?简单说呢.他就是一个资源,放在tomcat里面的一个资源,今天我们就把数据库连接池放到t ...