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+匹配
- 触发器在渗透中的利用
- Codeforces 626B Cards(模拟+规律)
- Android 5.0屏幕录制漏洞(CVE-2015-3878)威胁预警
- Codeforces 626A Robot Sequence(模拟)
- BZOJ 2038: [2009国家集训队]小Z的袜子(hose)【莫队算法裸题&&学习笔记】
- [接口测试 - 基础篇] 01 你应该了解的协议基础
- Uva - 12050 Palindrome Numbers【数论】
- [接口测试 - http.client篇] 14 源码初探及其工作机制分析
- 51Nod 1277 字符串中的最大值(KMP,裸题)
- Codeforces Round #345 (Div. 2)【A.模拟,B,暴力,C,STL,容斥原理】
- 07.移动先行之谁主沉浮----控件之轮流轰炸——布局类控件
- BZOJ 1411&&Vijos 1544 : [ZJOI2009]硬币游戏【递推,快速幂】
- UVa 10341 - Solve It【经典二分,单调性求解】
- UVa 11461 - Square Numbers【数学,暴力】
- 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 数组属性和方法