Red and Black 深搜



B - Red and Black

Crawling in process...
Crawling failed
Time Limit:1000MS    
Memory Limit:30000KB     64bit IO Format:%I64d & %I64u

Submit
Status

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
题意:就是数一下一个@所在的位置可以走多少步的问题。
思路:找到@的地方进行深搜有四个方向,没有剪枝
AC代码:
#include<iostream>
#include<cstdio>
using namespace std;
char map[20][20];
int b[4]={0,1,-1,0};
int bb[4]={1,0,0,-1};
int r,c;
int sx,sy;
int tol;
int ok(int x,int y){
    if(x<0||y<0||x>=r||y>=c||map[x][y]==‘#‘)return 0;
    return 1;
}
void dfs(int x,int y){
    for(int i=0;i<4;i++){
        map[x][y]=‘#‘;
        if(ok(x+b[i],y+bb[i])){
            tol++;
            dfs(x+b[i],y+bb[i]);
        }
    }
}
int main (){
    while(scanf("%d%d",&c,&r)!=EOF&&r!=0&&c!=0){
        getchar();
        for(int i=0;i<r;++i){
                for(int j=0;j<c;j++){
                   scanf("%c",&map[i][j]);//记住此处gets()是不行的,可能输入空格
                   if(map[i][j]==‘@‘){
                    sx=i;
                    sy=j;
                   }
                }
        if(i<r-1){getchar();}
        }
        tol=1;
        dfs(sx,sy);
        printf("%d\n",tol);
    }
    return 0;
}
这倒以前做过的,看见还是发现有点陌生,长时间不敲就忘了怎么弄,好好强化。。

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-07-30 10:17:41

Red and Black 深搜的相关文章

DFS深搜-Red and Black

深搜,从一点向各处搜找到所有能走的地方. 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 and black:这是初学者最常见到的题.对于这题我们所要考虑的就是一个'.'的个数,这个题先要找到@的位置,这个好办,直接: for(int i=0;i<m;i++) { for(int j=0;j<n;j++) { if(map[i][j]=='@') }

Farm Irrigation_深搜_并查集

Farm Irrigation TimeLimit: 2000/1000 MS (Java/Others)  MemoryLimit: 65536/32768 K (Java/Others) 64-bit integer IO format:%I64d Problem Description Benny has a spacious farm land to irrigate. The farm land is a rectangle, and is divided into a lot of

B广搜深搜

<span style="color:#330099;">/* B - 广搜/深搜 基础 Time Limit:1000MS Memory Limit:30000KB 64bit IO Format:%I64d & %I64u Submit Status Description There is a rectangular room, covered with square tiles. Each tile is colored either red or blac

hdoj-1312-Red and Black(深搜)

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

hdu1455 Sticks 深搜 强剪枝

Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6035    Accepted Submission(s): 1704 Problem Description George took sticks of the same length and cut them randomly until all parts becam

HDU1342 Lotto 【深搜】

应用 渗透问题 游戏中会用到. 动态连接 最近共同祖先 等价有限状态机 物理学Hoshen-Kopelman算法:就是对网格中的像素进行分块 Hinley-Milner多态类型推断 Kruskai最小生成树 Fortran等价语句编译 形态学开闭属性 Matlab中关于图像处理的bwlabel函数 渗透问题 一个N×N的矩阵,判断顶部和底部是否连通就是渗透问题. 下图中左侧的矩阵能渗透,右侧矩阵不能渗透. 渗透问题在电学.流体力学.社会交际中都有应用. 在游戏中可能需要生成一张地图,但是作为地图

HDU5723 Abandoned country 最小生成树+深搜回溯法

Description An abandoned country has n(n≤100000) villages which are numbered from 1 to n. Since abandoned for a long time, the roads need to be re-built. There are m(m≤1000000) roads to be re-built, the length of each road is wi(wi≤1000000). Guarante

CodeM美团点评编程大赛初赛B轮 黑白树【DFS深搜+暴力】

[编程题] 黑白树 时间限制:1秒 空间限制:32768K 一棵n个点的有根树,1号点为根,相邻的两个节点之间的距离为1.树上每个节点i对应一个值k[i].每个点都有一个颜色,初始的时候所有点都是白色的. 你需要通过一系列操作使得最终每个点变成黑色.每次操作需要选择一个节点i,i必须是白色的,然后i到根的链上(包括节点i与根)所有与节点i距离小于k[i]的点都会变黑,已经是黑的点保持为黑.问最少使用几次操作能把整棵树变黑. 输入描述: 第一行一个整数n (1 ≤ n ≤ 10^5) 接下来n-1