HDU-1312 Red and Black

AI权益加码!Claude Code、Cursor等20+工具免费用! 购周边限时加赠Coding Plan Lite,畅享主流AI工具!学习进阶更高效! 阅读详情

HDU-1312 Red and Black

Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 65536/32768 K (Java/Others)

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
6 9
....#.
.....#
......
......
......
......
......
#@...#
.#..#.
11 9
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
###.###
...@...
###.###
..#.#..
..#.#..
0 0
Sample Output
45
59
6
13


题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=1312

分析

题意:就是给你一个地图,分为红色和黑色两种瓦片,再给出你现在的位置,然后开始走,每次只能走上下左右四个方向,而且还只能走黑色的地板,问他能走的黑色地板的数量

思路:首先呢,这是一个很简单的深搜,套用模板就可以了,走过之后避免重复走,给走过的地板标记成红色就可以了,最后输出总数。

代码
#include <iostream>
#include <stdio.h>
using namespace std;
int xy[4][2]={1,0,0,-1,0,1,-1,0};
char dt[20][20];
int num;
int m,n;
void DFS(int x,int y)
{
    int X,Y;
    if (dt[x][y]!='.'||x>=n||y>=m||x<0||y<0)
        return;
    dt[x][y]='#';
    num++;
    for(int i=0;i<4;i++)
    {
        X=x+xy[i][0];
        Y=y+xy[i][1];
        DFS(X,Y);
    }
}
int main()
{
    while (~scanf("%d %d",&m,&n),m!=0&&n!=0)
    {
        num=0;
        for (int i=0;i<n;i++)
            for (int j=0;j<m;j++)
               cin>>dt[i][j];
        for (int i=0;i<n;i++)
        {
            for (int j=0;j<m;j++)
                if (dt[i][j]=='@')
                {
                    dt[i][j]='.';
                    DFS(i,j);
                }
        }
        cout<<num<<endl;
    }
    return 0;
}
HDU 1312 Red and Black 看了好多天的DFS,基本上的程序都是要借助解题报告才可以实现 但是这一题,确实依照自己的思路,完全靠自己写出来的,或许比较简单吧,所以特此发表下文章,犒劳一下自己,再接再厉 http://acm.hdu.edu.cn/showproblem.php?pid=1312 Red 阅读详情

相关推荐

HDU 1312 Red and Black(经典搜索,DFS&BFS三种方式)

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

AC_Dreameng 4845

HDU T1312 Red and Black

                          HDU T1312 Red and Black 题解:     因为是遍历所有可以走的点,所以不需要回溯。需要注意的是写完要测试最小值     例如:1 1                @     答案应该为1,本身也算一个。   Dfs代码 #include&lt;cstdio&gt; #include&lt;iostrea...

Mr__Charles的博客 205

hdu 1312 Red and Black

hdu  1312  Red and Black                 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312 DFS水 题目大意:一个人被困在一个迷宫里,问他的生存空间。 题目分析:DFS就好了。 code: #include #include char mp[26][26],vis[26][26]; int m

slicer的技术交流栈 912

Hdu 1312 Red and black(BFS)

HDU 1312 Red and black#include<iostream> #include<cstring> #include<algorithm> #include<queue> using namespace std; char map[25][25]; int flag[25][25];//标记 已经走过的路 int step[4][2] = { { 1, 0 }, { 0, 1 },

勇于尝试,却要三思后行。 402

hdu1312 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 adjacen

503

HDU1312——Red and Black

http://acm.hdu.edu.cn/showproblem.php?pid=1312 这倒题用的是广搜:广搜用的是队列。就是从一个点有四个方向,所以把四个可能都放进队列里。 广搜也是状态改变。就像是泼水的感觉。 题意:就是从@位置最多能走多少步。明显的广搜 #include<stdio.h> #include<cstring> #inclu...

aozhe0658的博客 124

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): 13111    Accepted Submission(s): 8127 Problem Description There is a re

dxx_111的博客 711

hdu 1312 Red And Black

hdu 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

代码改变世界 954

hdu_1312 Red and Black

原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312 分析:简单的BFS。使用广搜,使每个满足添加的点进队列,统计删除的点,有多少个点被删除就有多少个满足题意的点。 我的代码: #include #include #include using namespace std; char map[21][21]; bool visited[21][

徵羽調_xiao賤的专栏 899

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): 29940Accepted Submission(s): 18118 Problem Description There is a rectan...

h2017010687的博客 183

hdu 1312 Red and Black

题目链接: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): 11962    Accepte

GrEedWish_X的专栏 499

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): 2364    Accepted Submission(s): 1551 Problem Description There is a recta

其实我是C先生--的博客 579

dfs求解HDU 1312 Red and Black

dfs求解HDU 1312 Red and Black 搜索水题 有一个长方形的房间,上面铺着方瓷砖。每个贴图都是红色或黑色。一个人站在一块黑瓷砖上。从一个贴图,他可以移动到四个相邻贴图中的一个。但是他不能在红色的贴图上移动,只能在黑色的贴图上移动。 写一个程序来计算他通过重复上面描述的动作可以达到的黑色方块的数量。 输入 输入由多个数据集组成。数据集以包含两个正整数W和H的一行开始;W和H分别为x方向和y方向的瓦片数量。W和H不超过20。 数据集中共有H行,每行包含W个字符。每个字符都表示一个贴图的颜色,

zhang_xiao__的博客 2297

hdu-1312 Red and Black(dfs)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312 题目大意:点是路,#是墙,输出从起始位置能走多少步 题解:dfs函数两个参数x,y记录当前坐标。从起始位置开始深搜,如果当前位置是‘#’则不进行下一步搜索,如果是 。 则把sum+1,并且将该位置 置为 ‘#’ 进行下一步深搜(搜索上下左右的坐标点) AC代码: #include &l...

qq_37744588的博客 228

杭电 hdu 1312 Red and Black

/* THE PROGRAM IS MADE BY PYY */ /*----------------------------------------------------------------------------// Copyright (c) 2011 panyanyany All rights reserved. URL : http://acm.hdu.edu.cn/show...

网站开发初中级代码参考-转载 94
上一篇: HDU-1242 Rescue
下一篇: HDU-1372 Knight Moves
tjial
博客等级 码龄9年 26粉丝 149原创
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值