HDUOJ--------(1312)Red and Black
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
Asia 2004, Ehime (Japan), Japan Domestic
Recommend
Eddy
1 #include<cstdio>
2 #include<cstdlib>
3 #include<cstring>
4 #include<deque>
5 #include<iostream>
6 #define maxn 25
7 using namespace std;
8 char maze[maxn][maxn];
9 int w,h;
10 /*建立方向树*/
11 struct node
12 {
13 int x,y;
14 }start;
15 /*搜索方向*/
16 int dir[4][2]=
17 {
18 {0,1},
19 {0,-1},
20 {-1,0},
21 {1,0}
22 };
23 void bfs()
24 {
25 /*入队,出队用*/
26 deque<node>q;
27 /*暂存位置*/
28 node q1,q2;
29 q.push_back(start);
30 while(!q.empty())
31 {
32 q1=q.front ();
33 q.pop_front();
34 for(int i=0;i<4;i++)
35 {
36 q2.x=q1.x+dir[i][0];
37 q2.y=q1.y+dir[i][1];
38 if(q2.x>h||q2.x<1||q2.y>w||q2.y<1||maze[q2.x][q2.y]=='#'||maze[q2.x][q2.y]=='p') /*return ;*/
39 ; /*啥也不干,就这么一个逗号....*/
40 else
41 {
42 if(maze[q2.x][q2.y]=='.')
43 maze[q2.x][q2.y]='p'; /*就暂时用P来代表占位置吧!*/
44 /*入队*/
45 q.push_back (q2);
46 }
47 }
48 }
49 }
50 int main()
51 {
52 int i,j,ans;
53 while(scanf("%d%d",&w,&h),h+w)
54 {
55 getchar();
56 memset(maze,' ',sizeof(maze));
57 for(i=1;i<=h;i++)
58 {
59 for(j=1;j<=w;j++)
60 {
61 scanf("%c",&maze[i][j]);
62 if(maze[i][j]=='@')
63 {
64 start.x=i;
65 start.y=j;
66 maze[i][j]='#';
67 }
68 }
69 getchar();
70 }
71 bfs();
72 ans=1;
73 for(i=1;i<=h;i++)
74 {
75 for(j=1;j<=w;j++)
76 {
77
78 if(maze[i][j]=='p')
79 ans++;
80 }
81 }
82 printf("%dn",ans);
83 }
84 return 0;
85 }
bfs+匹配
- [WCF的Binding模型]之三:信道监听器(Channel Listener)
- zookeeper监控告警
- 扩展ToolBarManager、ListView和Grid控件以实现气球式的ToolTip
- Linux同步机制 - 基本概念(死锁,活锁,饿死,优先级反转,护航现象)
- Linux同步机制 - 多线程开发总结
- 谷歌发布升级版语音合成系统,直接从字符合成语音
- 无锁编程 - 大纲
- 无锁编程(一) - Double-checked Locking
- 无锁编程(二) - 原子操作
- 我所理解的Remoting(3):创建CAO Service Factory使接口和实现相互分离
- 无锁编程(三) - 忙等待
- Enterprise Library深入解析与灵活应用(9):个人觉得比较严重的关于CachingCallHandler的Bug
- 无锁编程(四) - CAS与ABA问题
- Linux Kernel CMPXCHG函数分析
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 基于Apriori的数据关联分析 | 工业数据分析 | 冰水数据智能专题 | 4th
- 基于FP树的频繁项挖掘 | 工业数据分析 | 冰水数据智能 | 5th
- ICCV2019 高通Data-Free Quantization论文解读
- VBA解压缩ZIP文件10——解压-动态Huffman
- 海思NNIE之PFPLD训练与量化
- [译] 用 Truffle 插件自动在Etherscan上验证合约代码
- 二层网络上的以太坊智能合约: Optimistic Rollup
- 基于决策树的工业数据分类——数据智能
- Kestrel的ListenAnyIP和ListenLocalhost的区别
- 【为宏正名】什么?我忘了去上“数学必修课”!
- 第6章 Jenkins系统权限划分与授权管理
- Python爬虫新手教程: 知乎文章图片爬取器
- 《重构-代码整洁之道TypeScript版》第4天
- C++基础 杂记(一)
- 一种Cortex-M内核中的精确延时方法(ns级别)