关闭

POJ 1979:Red and Black

标签: poj递归搜索算法
135人阅读 评论(0) 收藏 举报
分类:

基础的深度优先搜索题目

通过对每一个位置的上下左右位置进行递归搜索。


题目描述:

Red and Black
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 31159   Accepted: 17006

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)
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<algorithm>
#include<fstream>
#include<string>

using namespace std;
void solve();
void dfs(int row, int col);
int result = 0;
string grid[10001];
int n=-1, m=-1;
int main(){

	while (true){
		cin >> n >> m;
		if (n == 0 && m == 0)break;

		for (int i = 0; i < m; i++)
			grid[i] = "";
		
		
		for (int i = 0; i < m; i++)
				cin >> grid[i];

		result = 0;
		solve();
		cout << result << endl;


	}
	return 0;
}

void solve(){
	int s_row=0, s_col=0;
	bool isfind = false;
	
	for (int i = 0; i < m; i++)
	{
		for (int j = 0; j < n;j++){
			if (grid[i][j] == '@'){
				s_row = i;
				s_col = j;
				isfind = true;
				break;
			}
			if (isfind)
				break;
	}
		
	}

	dfs(s_row, s_col);
	
	return;
}


void dfs(int row, int col){
	
		result++;
		int x, y;
		grid[row][col]='#';
	
//	int x, y;
	for (int dy=-1;dy<=1;dy++)
	for (int dx = -1; dx <= 1; dx++){
		for (int dy = -1; dy <= 1; dy++){
           //if x,y int the grid
			x=row+ dx;
			y=col+dy;
			if ((dx==0||dy==0)&&x>= 0 && x<m&&y>=0 && y < n&&grid[x][y]=='.')
				dfs(x, y);
		}
	}
	return;
}



0
0
查看评论
发表评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场

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

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

二叉树可视化--Graphviz

大家平时写C程序有没有种把内存里的数据结构全给画出来的冲动呢?数据量小的话,画起来还蛮简单,用viso,我前面的文章都用viso画的。之前写红黑树代码的时候,用的是命令行把整个树打印出来,不过只是一些...
  • ksjay_1943
  • ksjay_1943
  • 2017-02-16 11:32
  • 157

Red and Black(搜索题目)

Red and Black Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %I64d & %I64u Submit   Statu...
  • zq17865815296
  • zq17865815296
  • 2016-02-24 17:02
  • 154

Red and Black(广搜,迷宫)

Red and Black Problem Description There is a rectangular room, covered with square tiles. Each t...
  • zhangzhilai8
  • zhangzhilai8
  • 2013-04-25 09:02
  • 373

POJ 1979 —— 搜索

Red and Black Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 19668   Accepted: 1...
  • zkzxmzk
  • zkzxmzk
  • 2013-10-24 21:50
  • 500

简单广搜:Red and Black

There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A ...
  • duan_1998
  • duan_1998
  • 2017-01-11 17:09
  • 109

POJ 1979 解题报告

这道题是简单的DFS。感觉c++输入比较头疼。 1979 Accepted 728K 0MS G++ 1846B /* ID: thestor1 LA...
  • thestoryofsnow
  • thestoryofsnow
  • 2014-12-03 10:35
  • 360

POJ 1979(递归)

#include #include char map[25][25]; int ans,n,m; bool visited[25][25]; void search (int row,int co...
  • u013946585
  • u013946585
  • 2014-05-12 16:30
  • 284

红黑树(Red Black Tree)

红黑树(Red Black Tree) 介绍 今天我们来介绍另一种平衡二叉树:红黑树(Red Black Tree),红黑树由Rudolf Bayer于1972年发明,当时被称为平衡二叉B树(sy...
  • armlinuxww
  • armlinuxww
  • 2013-12-31 16:23
  • 781

poj 1979 简单深搜

There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A ...
  • qq_35736364
  • qq_35736364
  • 2017-03-01 20:58
  • 53
    个人资料
    • 访问:535次
    • 积分:99
    • 等级:
    • 排名:千里之外
    • 原创:9篇
    • 转载:0篇
    • 译文:0篇
    • 评论:1条
    最新评论