HDU1312:Red and Black(DFS)

题目描述

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.

输入

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

解题思路:深搜的方法解决,题目意思就是从@开始找.并与@连通,碰到#等于碰到了墙,题目很简单,@可以向四个方向上、下、左、右走,所以 用四个坐标标记出来,然后,再一一遍历,递归调用寻找,用一个30*30的数组标识此点有没有走过,避免走重复

程序代码:
#include <cstdio>
#include <cstring>
using namespace std;
int n,m,cot;
char map[30][30];
int to[4][2] = {{1,0},{0,1},{-1,0},{0,-1}};

void dfs(int i,int j)
{
    cot++;
    map[i][j] = ‘#‘;
    for(int k = 0; k<4; k++)
    {
        int x = i+to[k][0];
        int y = j+to[k][1];
        if(x<n && y<m && x>=0 && y>=0 && map[x][y] == ‘.‘)
            dfs(x,y);
    }
    return;
}

int main()
{
    int i,j,fi,fj;
    while(~scanf("%d%d%*c",&m,&n)&&m&&n)
    {
        for(i = 0; i<n; i++)
        {
            for(j = 0; j<m; j++)
            {
                scanf("%c",&map[i][j]);
                if(map[i][j] == ‘@‘)
                {
                    fi = i;
                    fj = j;
                }
            }
            getchar();
        }
         cot= 0;
        dfs(fi,fj);
        printf("%d\n",cot);
    }

    return 0;
}
				
时间: 2024-12-13 04:26:24

HDU1312:Red and Black(DFS)的相关文章

HDU1312: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

POJ 3321:Apple Tree(dfs序+树状数组)

题目大意:对树进行m次操作,有两类操作,一种是改变一个点的权值(将0变为1,1变为0),另一种为查询以x为根节点的子树点权值之和,开始时所有点权值为1. 分析: 对树进行dfs,将树变为序列,记录每个点进栈出栈的时间戳作为其对应区间的左右端点,那么其子节点对应区间必在该区间内,对这段区间求和即可,用树状数组进行修改与求和. 代码: program apple; type point=^node; node=record x:longint; next:point; end; var a:arra

hdu1312(Red and Black)

点击打开杭电1312 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

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

I - Red and Black DFS

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 til

Codeforces 348B:Apple Tree(DFS+LCM+思维)

http://codeforces.com/contest/348/problem/B 题意:给一棵树,每个叶子结点有w[i]个苹果,每个子树的苹果数量为该子树所有叶子结点苹果数量之和,要使得每个结点的各个子树苹果数量相等,求至少需要拿走的苹果数量. 思路:一开始以为只要使得所有子树之和相同就行了. 1 void dfs(int u, int fa) { 2 int num = 0, mi = INF; 3 for(int i = head[u]; ~i; i = edge[i].nxt) {

搜索算法:深度优先搜索(DFS)

关于深搜的介绍,在网上有很多,不再赘述.主要以题目形式实例讲解. POJ - 1321 (http://poj.org/problem?id=1321) 题目大意:给出一个棋盘,棋子不能同行同列,求放棋子的可行方案数. 题目思路:给的数据非常小,n<=8,非常简单的一道深搜题.需要放k行,按行递增递归,找到行中可以放的点,然后往下搜,直到把棋子放完,方案数就加一.只要设置一个col[]数组保存已经放过的列,找到一个点,col[i]置为1,返回上一层是col[i]置回0. 代码:(http://p

HDU - 1312 : Red and Black

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 til

数据结构(15):图 深度优先遍历(DFS)

/*-----------------------------------------------*/ /* 邻接矩阵的DFS */ // 基于 数据结构(14) 中的邻接矩阵的结构 #include <iostream> using namespace std; typedef char VertexType; typedef int EdgeType; const int MAXVEX = 100; const int INFINITY = 65535; typedef struct {