HDU 1312:Red and Black(DFS搜索)

HDU 1312:Red and Black

Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u

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

题解:本题还是DFS搜索(上下左右),只是增加一个计数器,计算可以走的地方的个数。

规定地图中有可通行的位置,也有不可通行的位置,已知起点,求起点的与它相连成一片的部分,在这道题里输出相连的位置的数目。

    从起点开始,遍历每一个到达的点的四个方向(不再是八个),到达一个位置就将这个位置的字符变成不可走的‘#‘,并且计数+1。其实就是计数将可走变成不可走的操作进行了多少次。

这样可以不用担心走过了还会重复。

#include <iostream>
using namespace std;
char a[25][25];
int n,m,total;
int dr[4] = {0,1,0,-1};//行变化
int dc[4] = {1,0,-1,0};//列变化
//上面的原来一直不会用,知道的话非常方便
bool judge(int x,int y)
{
    if(x<1 || x>n || y<1 || y>m)
        return 1;
    if(a[x][y]==‘#‘)
        return 1;
    return 0;
}
void dfs(int r,int c)
{
    total++;
    a[r][c]=‘#‘;               //走过一次,“。”变为“#”,避免重复
    for(int k=0; k<4; k++)
    {
        int lr = r + dr[k];
        int lc = c + dc[k];
        if(judge(lr,lc))
            continue;
        dfs(lr,lc);
    }

}
int main()
{
    while(cin>>m>>n&&m&&n)
    {
        int i,j,x,y;
        total=0;
        for(i=1; i<=n; i++)
            for(j=1; j<=m; j++)
            {
                cin>>a[i][j];
                if(a[i][j]==‘@‘)             //这里必须用变量x,y
                    x=i,y=j;

            }
        dfs(x,y);
        cout<<total<<endl;
    }
    return 0;
}
时间: 2024-08-08 13:50:00

HDU 1312:Red and Black(DFS搜索)的相关文章

HDU 1312 ----- Red and Black 入门搜索 DFS解法

HDU 1312 ----- Red and Black  入门搜索  http://acm.hdu.edu.cn/showproblem.php?pid=1312 /*HDU 1312 ----- Red and Black 入门搜索 */ #include <cstdio> int n, m; //n行m列 int cnt, startx, starty; char mapp[25][25]; /*找一个连通的区域里可以走的块*/ void dfs(int x, int y){ if (x

hdu 1312 Red and Black(dfs)

#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; char mat[50][50]; int n,m; int ans; int op[4][2]={0,1,0,-1,1,0,-1,0}; bool ok(int x,int y) { if(0<=x&&x<n&&0<=y

HDU 1312 Red and Black (搜索)

Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 9795    Accepted Submission(s): 6103 Problem Description There is a rectangular room, covered with square tiles. Each tile is colore

hdu 1312 Red and Black(BFS水题)

Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 9684    Accepted Submission(s): 6021 Problem Description There is a rectangular room, covered with square tiles. Each tile is colore

HDU 1312 Red and Black (dfs)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312 Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 17773    Accepted Submission(s): 10826 Problem Description There is a rectangula

HDU 1312 Red and Black(bfs,dfs均可,个人倾向bfs)

题目代号:HDU 1312 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312 Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 20820    Accepted Submission(s): 12673 Problem Description There i

HDU 1312 Red and Black 第一题搜索!

Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 12935    Accepted Submission(s): 8006 Problem Description There is a rectangular room, covered with square tiles. Each tile is color

HDU 1312 Red and Black(基础bfs或者dfs)

Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 11843    Accepted Submission(s): 7380 Problem Description There is a rectangular room, covered with square tiles. Each tile is color

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

Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 12907    Accepted Submission(s): 7983 Problem Description There is a rectangular room, covered with square tiles. Each tile is colo