Red and Black

2人阅读 评论(0) 收藏 举报
分类:

G
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) 
The end of the input is indicated by a line consisting of two zeros. 
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

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define MAX	20;
int n, m, sum;
int dir[4][2] = {-1, 0, 0, 1, 1, 0, 0, -1};
int vis[20][20] ;
char mps[20][20];
bool check (int x, int y) {
	if (x >= 0 && x < m && y >= 0 && y < n)
		return true;
	else
		return false;
}
void dfs (int x, int y) {
	sum++;
	vis[x][y] = 1;
	int i, x1, y1;
	for (i = 0; i < 4; i++) {
		x1 = x + dir[i][0];
		y1 = y + dir[i][1];
		if (check(x1, y1) && vis[x1][y1] == 0) {
			dfs(x1, y1);
		}
	}
	return ;
}
int main () {
	int i, j, x ,y;
	while (scanf("%d%d", &n, &m) && n != 0 && m != 0) { 
		sum = 0;
		memset(vis, 0, sizeof(vis));
		memset(mps, 0,sizeof(mps)); 
		for (i = 0; i < m; i++) {
			scanf("%s", mps[i]);
		}
		for (i = 0; i < m; i++) {
			for(j = 0; j < n; j++) {
				if (mps[i][j] == '#') {
					vis[i][j] = 1;
				}
				if (mps[i][j] == '@') {
					x = i;
					y = j;
				}
			}
		}
		dfs(x, y);
		cout << sum << endl;
	}
	return 0;
}

查看评论

北大ACM poj1979 Red and Black

#includechar map[21][21]; int c; int n,m; void count(char map[21][21],int i,int j){ map[i][j]='#...
  • gubojun123
  • gubojun123
  • 2012-12-08 11:06:31
  • 1266

HDU 1312 Red and Black(经典搜索,DFS&BFS三种方式)

Red and Black Problem Description There is a rectangular room, covered with square tiles. Each...
  • hurmishine
  • hurmishine
  • 2016-03-18 23:19:49
  • 1592

北大ACM1979 - Red and Black (广度优先搜索)

1.1      算法分析 这是一道很简单的广度优先搜索问题,我们以原始位置开始向四个方向搜索,如果是黑色的地砖,则将地砖标记为已经搜索过,然后搜索该黑色砖的四个临近地砖;如果该地砖是红色的或者...
  • Gykimo
  • Gykimo
  • 2013-01-06 18:19:26
  • 1407

POJ 1979 Red and Black(红与黑)

原文DescriptionThere is a rectangular room, covered with square tiles. Each tile is colored either red...
  • NoMasp
  • NoMasp
  • 2015-12-09 20:13:23
  • 3277

杭电 HDU 1312 Red and Black(超级简单dfs)

Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...
  • lsgqjh
  • lsgqjh
  • 2015-07-31 10:22:43
  • 753

杭电ACM1312——Red and Black~~广搜

这一题,简单的广搜或者深搜都可以搞定,时间复杂度都差不多。 我用的是广搜。题目的意思是:@是一个人的起始位置,#不可以走,. 可以走,求出可以走的位置的个数。 一开始没有用结构体来存储坐标,直接用的是...
  • qq_25425023
  • qq_25425023
  • 2015-05-21 22:44:13
  • 406

红黑树—Red Black Tree

红黑树是具有以下五条性质的二叉查找树: 1、每个结点要么是红的要么是黑的。 2、根结点是黑的。 3、每个叶结点(叶结点即指树尾端NIL指针或NULL结点)都是黑的。 4、如果一个结点是红的...
  • yuyixinye
  • yuyixinye
  • 2015-03-28 21:47:10
  • 725

红黑树(RED-BLACK TREE)的插入和删除

红黑树的左旋和右旋
  • flyapy
  • flyapy
  • 2014-07-12 16:06:45
  • 389

疯狂Java学习笔记(41)----------Red-Black Trees(红黑树)

目录(?)[+] 一红黑树简介二树的旋转三红黑树的插入操作四红黑树的删除五补充 Java 实现的红黑树 图 6 Java 红黑树的示意 红黑树和平衡二叉树 一:红黑树简介 红黑树,一种二叉...
  • u011225629
  • u011225629
  • 2015-05-20 21:48:18
  • 1119

杭电(hdu)ACM 1312 Red and Black

Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...
  • IT142546355
  • IT142546355
  • 2015-08-13 08:31:17
  • 597
    个人资料
    持之以恒
    等级:
    访问量: 126
    积分: 173
    排名: 107万+
    文章存档