关闭

DFS入门__poj1979

标签: dfspoj1979
240人阅读 评论(0) 收藏 举报
分类:
Red and Black
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 26944   Accepted: 14637

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
/*题目大意:在一个矩形房间里,里面填满着红砖与黑砖,每次走动只能走黑砖位置,而不能走红砖,
 *			可以上下左右四个方向走动,现在初始位置在黑砖位置上,试问从此黑砖位置开始能够到达的砖的数量是多少
 *算法分析:从起始位置开始四个方向进行dfs,若遇到红砖位置则将此砖块换为黑砖。换砖的次数即为到达的黑砖数量 

*/

#include <iostream>
#include <cstdio>
using namespace std;

char a[25][25];
int n, m;
int dir[4][2] = {0, 1, 1, 0, 0, -1, -1, 0};				//四个方向数组 

void dfs(int x, int y, int &res) {
	a[x][y] = '#';
	res ++ ;
	for (int i = 0; i<4; i++) {
		int nx = x + dir[i][0];
		int ny = y + dir[i][1];
		if (nx>=0 && nx<n && ny>=0 && ny<m && a[nx][ny] == '.')
			dfs(nx, ny, res);
	}
}

int main() {
	
	while (cin >> m >> n && (n+m)) {
		
		memset(a, 0, sizeof(a));
		for (int i = 0; i<n; i++) {
			for (int j = 0; j<m; j++) {
				cin >> a[i][j];
			}
		}
		int res = 0;
		for (int i = 0; i<n; i++) {
			for (int j = 0; j<m; j++) {
				if (a[i][j] == '@') 
					dfs(i, j, res);
			}
		}
		cout << res << endl;
	}
	return 0;
}


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

DFS搜索基础题

作为一个大渣,初学算法和数据结构,刚学完链表,便接触了搜索,深知其重要,潜心修炼数天,终于从DFS找到点 感觉了然BFS还刚开始(流泪流泪),简直是一跪再跪,一晕再晕。近几日,做了一些DFS的基础题...
  • qq_32036091
  • qq_32036091
  • 2015-11-29 23:08
  • 1399

搜索入门(DFS)最简单的部分和问题

深度优先搜索是一种搜索思路,它从某个状态开始,不断地转移状态直到找到结果或无法转移,然后回退到前一步的状态,继续转移至其他状态,如此不断重复,直至找到最终解。一般采用递归方式。
  • stack_queue
  • stack_queue
  • 2016-11-28 22:44
  • 379

poj入门水题--深度搜索(dfs)题 1011,含各种剪枝,比较经典

深度搜索(dfs)题 1011,含各种剪枝,比较经典
  • qq_17246605
  • qq_17246605
  • 2016-12-25 20:49
  • 825

DFS深度优先搜索(入门)

DFS入门(递归写法) 这两天在学习深度优先搜索(DFS),感觉DFS比BFS难,一开始主要是标记搞不清楚。DFS在回溯时要取消原先的标记,而BFS不存在回溯也就不存在取消标记这一问题。DFS可以用...
  • Coding_Or_Dead
  • Coding_Or_Dead
  • 2016-08-26 11:06
  • 2934

DFS——poj1979(深度优先搜索 )

Description There is a rectangular room, covered with square tiles. Each tile is colored either red...
  • qq_33732363
  • qq_33732363
  • 2016-11-20 15:11
  • 79

POJ1979(DFS深度优先搜索)之 Red and Black

Description There is a rectangular room, covered with square tiles. Each tile is colored either re...
  • ESESZB
  • ESESZB
  • 2016-10-24 11:12
  • 188

BFS和DFS算法原理(通俗易懂版)

DFS 算法 思想:一直往深处走,直到找到解或者走不下去为止 BFS算法 DFS:使用栈保存未被检测的结点,结点按照深度优先的次序被访问并依次被压入栈中,并以相反的次序出栈进行新的检测。...
  • u011437229
  • u011437229
  • 2016-11-16 17:25
  • 19506

详解ACM基础算法—DFS深度优先搜索算法 HIT杨朔

详解深度优先搜索(DFS)及例题
  • HITyangshuo
  • HITyangshuo
  • 2017-03-09 09:31
  • 2586

DFS采用栈实现非递归

DFS核心类: package com.dfs; import java.util.Stack; /** * 图的DFS 非递归遍历算法 * @author Administrator * ...
  • d06110902002
  • d06110902002
  • 2016-09-27 20:34
  • 1193

DFS与BFS的区别、用法、详解?

写在最前的三点: 1、所谓图的遍历就是按照某种次序访问图的每一顶点一次仅且一次。 2、实现bfs和dfs都需要解决的一个问题就是如何存储图。一般有两种方法:邻接矩阵和邻接表。这里为简单起 见,均...
  • dreamzuora
  • dreamzuora
  • 2016-04-12 21:02
  • 14094
    个人资料
    • 访问:70903次
    • 积分:2283
    • 等级:
    • 排名:第18970名
    • 原创:162篇
    • 转载:6篇
    • 译文:0篇
    • 评论:6条
    博客专栏