HDU1312-DFS

Red and Black

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 19690    Accepted Submission(s): 11965

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, 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)

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

Sample Output

45

59

6

13

好久之前老大讲过的,忘了写了。。。

#include<bits/stdc++.h>
using namespace std;
int direct [4][2]={-1,0,1,0,0,1,0,-1};                                    //方向
char str[25][25];
bool flag[25][25];
int w,h,ans;
void DFS(int x,int y){
    for(int i=0;i<4;i++){
        int p=x+direct[i][0];
        int q=y+direct[i][1];
        if(p>=0&&q>=0&&p<h&&q<w&&flag[p][q]==0&&str[p][q]==‘.‘){         //判断,至今仍不明白方向为-1的时候怎么还是>=0,还是我太蠢了。。。
            ans++;
            flag[p][q]=1;
            DFS(p,q);                                                    //递归
        }
    }
}
int main(){
    int i,j,k;
    int Dx,Dy;
    while(~scanf("%d%d",&w,&h)){
        if(w==0&&h==0)break;
        memset(flag,0,sizeof(flag));                                    //这个函数通常为新申请的内存做初始化工作
        getchar();
        for(i=0;i<h;i++){
            for(j=0;j<w;j++){
                scanf("%c",&str[i][j]);
            if(str[i][j]==‘@‘){
                Dx=i;
                Dy=j;
            }
            } getchar();
        }
        ans=1;
        flag[Dx][Dy]=1;
        DFS(Dx,Dy);
        printf("%d\n",ans);
    }
    return 0;
}

写下来,等忘的时候再回来看。

啊,我这个菜鸟,好笨。。。┭┮﹏┭┮

时间: 2024-10-04 00:09:56

HDU1312-DFS的相关文章

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 blac

HDU-1312题解(DFS)

HDU-1312-DFS Written by Void-Walker    2020-02-01 09:09:25 1.题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1312 2.题目大意: 有一个矩形房间,房间里有红砖块(‘#’)和黑砖块(‘.’)组成.现在有一个人站在一个@上面,他只能走黑色方块,现在问他最多能经过多少黑色方块.(他的初始位置也算) 3.题目思路: 这道题是一个非常经典的深度优先搜索.我们从他的初始位置开始搜索: for(y

HDU1312 Red and Black

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

HDU1312 / POJ1979 / ZOJ2165 Red and Black(红与黑) 解题报告

题目链接:HDU1312 / POJ1979 / ZOJ2165 Red and Black(红与黑) Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 9902    Accepted Submission(s): 6158 Problem Description There is a rectangular

HDU1312——Red and Black

Red and Black Problem DescriptionThere 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 til

ACM DFS+BFS

一.DFS(深度优先搜索) 1.八皇后问题&N皇后问题 题目:HDU-2553     网址:http://acm.hdu.edu.cn/showproblem.php?pid=2553 #include<iostream> using namespace std; int n,sum; int ans[12],sel[12]; int abs(int n)//求绝对值 { return n>0?n:-n; } void dfs(int h)//确定第i行的皇后的位置 { if(

解救小哈——DFS算法举例

一.问题引入 有一天,小哈一个人去玩迷宫.但是方向感不好的小哈很快就迷路了.小哼得知后便去解救无助的小哈.此时的小哼已经弄清楚了迷宫的地图,现在小哼要以最快的速度去解救小哈.那么,问题来了... 二.问题的分析 首先我们用一个二维数组来存储这个迷宫,刚开始的时候,小哼处于迷宫的入口处(1,1),小哈在(p,q).其实这道题的的本质就在于找从(1,1)到(p,q)的最短路径. 此时摆在小哼面前的路有两条,我们可以先让小哼往右边走,直到走不通的时候再回到这里,再去尝试另外一个方向. 在这里我们规定一

【BZOJ4942】[Noi2017]整数 线段树+DFS(卡过)

[BZOJ4942][Noi2017]整数 题目描述去uoj 题解:如果只有加法,那么直接暴力即可...(因为1的数量最多nlogn个) 先考虑加法,比较显然的做法就是将A二进制分解成log位,然后依次更新这log位,如果最高位依然有进位,那么找到最高位后面的第一个0,将中间的所有1变成0,那个0变成1.这个显然要用到线段树,但是复杂度是nlog2n的,肯定过不去. 于是我在考场上yy了一下,这log位是连续的,我们每次都要花费log的时间去修改一个岂不是很浪费?我们可以先在线段树上找到这段区间

uva1103(dfs)

UVA - 1103 还是没写好,,看的别人的 1 #include <iostream> 2 #include <cstdio> 3 #include <cmath> 4 #include <cstring> 5 #include <algorithm> 6 #include <cstdlib> 7 #include <stack> 8 #include <cctype> 9 #include <str

poj 1088 滑雪 DP(dfs的记忆化搜索)

题目地址:http://poj.org/problem?id=1088 题目大意:给你一个m*n的矩阵 如果其中一个点高于另一个点 那么就可以从高点向下滑 直到没有可以下滑的时候 就得到一条下滑路径 求最大的下滑路径 分析:因为只能从高峰滑到低峰,无后效性,所以每个点都可以找到自己的最长下滑距离(只与自己高度有关).记忆每个点的最长下滑距离,当有另一个点的下滑路径遇到这个点的时候,直接加上这个点的最长下滑距离. dp递推式是,dp[x][y] = max(dp[x][y],dp[x+1][y]+