POJ 1979 Red and Black(DFS 连通块中元素数量)

题意  求矩阵中包含‘@’的‘.‘连通块中元素数量  ‘@‘也看做‘.‘

最基础的dfs了

#include<cstdio>
#include<cstring>
using namespace std;
const int N = 30;
char mat[N][N];
int dx[4] = {0, 0, -1, 1}, dy[4] = { -1, 1, 0, 0};
int ans;

void dfs(int r, int c)
{
    if(mat[r][c] != '.') return;
    mat[r][c] = '#';
    for(int i = 0; i < 4; ++i)
    {
        int x = r + dx[i], y = c + dy[i];
        if(mat[x][y] == '.')
        {
            dfs(x, y);
            ++ans;
        }
    }
}

int main()
{
    int m, n, sx, sy;
    while(scanf("%d%d", &m, &n), m)
    {
        memset(mat, 0, sizeof(mat));
        for(int i = 1; i <= n; ++i)
            scanf("%s", mat[i] + 1);
        for(int i = 1; i <= n; ++i)
            for(int j = 1; j <= m; ++j)
                if(mat[i][j] == '@') sx = i, sy = j;
        ans = 1;
        dfs(sx, sy);
        printf("%d\n", ans);
    }
}

Red and Black

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
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#[email protected]#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
###.###
[email protected]
###.###
..#.#..
..#.#..
0 0

Sample Output

45
59
6
13

时间: 2024-10-17 13:54:10

POJ 1979 Red and Black(DFS 连通块中元素数量)的相关文章

POJ 1979 Red and Black dfs 难度:0

http://poj.org/problem?id=1979 #include <cstdio> #include <cstring> using namespace std; const int maxn = 21; bool vis[maxn][maxn]; char maz[maxn][maxn]; int n,m; const int dx[4] = {1,-1,0,0}; const int dy[4] = {0,0,1,-1}; int ans; bool in(int

poj 1979 Red and Black(dfs)

Red and Black Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 23191   Accepted: 12510 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

POJ 1979 Red and Black (红与黑)

POJ 1979 Red and Black (红与黑) Time Limit: 1000MS    Memory Limit: 30000K 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

POJ 1979 Red and Black【深度优先搜索】

题目链接:http://poj.org/problem?id=1979 题目大意:一个矩形的房间地板被分为w*h个小块,每一个小块不是红的就是黑的,你首先站在一个黑色小块上,你只能朝你的四个方向(上下左右)移动,且不能到达红色的小块上,问你最多能到达多少个小块. 很简单的dfs深度优先搜索 没搜索过一个格子,将该格子设置为红色,之后的搜索就不会再搜索到该格子,就不会造成重复,因为该题有很多数据,记得每次处理数据是初始化各数组及其他数据. 代码如下: #include <iostream> #i

POJ 1979 Red and Black 深度优先搜索上手题

Red and Black Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 21738   Accepted: 11656 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

837. 连通块中点的数量(并查集)

给定一个包含n个点(编号为1~n)的无向图,初始时图中没有边. 现在要进行m个操作,操作共有三种: “C a b”,在点a和点b之间连一条边,a和b可能相等: “Q1 a b”,询问点a和点b是否在同一个连通块中,a和b可能相等: “Q2 a”,询问点a所在连通块中点的数量: 输入格式 第一行输入整数n和m. 接下来m行,每行包含一个操作指令,指令为“C a b”,“Q1 a b”或“Q2 a”中的一种. 输出格式 对于每个询问指令”Q1 a b”,如果a和b在同一个连通块中,则输出“Yes”,

《挑战》2.1 POJ POJ 1979 Red and Black (简单的DFS)

B - Red and Black Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 1979 Description There is a rectangular room, covered with square tiles. Each tile is col

DFS(连通块) HDU 1241 Oil Deposits

题目传送门 1 /* 2 DFS:油田问题,一道经典的DFS求连通块.当初的难题,现在看上去不过如此啊 3 */ 4 /************************************************ 5 Author :Running_Time 6 Created Time :2015-8-4 10:11:11 7 File Name :HDOJ_1241.cpp 8 ************************************************/ 9 10

DFS(连通块) ZOJ 2743 Bubble Shooter

题目传送门 题意:从炮台射出一个球,三个及以上颜色相同的会掉落,问最后会掉落多少个球 分析:先从炮台找一个连通块,然后与顶部连接的连通块都不会掉落,剩下的就是炮台射出后跟随掉落的. #include <bits/stdc++.h> const int N = 100 + 5; char str[N][N]; int H, W, h, w; int ans; bool check(int x, int y) { if (x < 1 || x > H || y < 1 || y