HDU 1312 Red and Black

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

看了好多天的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): 2364    Accepted Submission(s): 1551


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
 
#include<iostream>
#include<string.h>
#include<stdio.h>
using namespace std;
int direct[4][2] = { -1, 0, 1, 0, 0, 1, 0, -1 };  /*定义方向, 左右,上下*/

char map[21][21];               /*输入的字符串*/
bool mark[21][21];              /*标记走过的路程*/
bool flag;
int W, H;
int Dx, Dy;            //记录起始位置@,从这里开始进行搜索
int ans;                //记录满足的个数。初始化为1,因为@也包含在内
/*****底下是核心算法,主要是从
上下左右这四个方向进行搜索,注意
满足搜索的条件是不能越界,不能是#,
还有就是没有搜索过的--》主要是靠mark[i][j]
来实现*******/
void DFS( int x, int y )
{
    mark[x][y] = true;
    for( int k = 0; k < 4; k ++ )
    {
        int p = x + direct[k][0];
        int q = y + direct[k][1];
        if( p >= 0 && q >= 0 && p < H && q < W && !mark[p][q] && map[p][q] != '#' )
        {
            ans ++;
            DFS( p, q );
        }
    }
    return;
}

int main()
{
    int i, j, k;
    while( cin >> W >> H && ( W || H ) )   // W -> column, H -> row;
    {
        memset( mark, false, sizeof( mark ) );
        for( i = 0; i < H; i ++ )
            for( j = 0; j < W; j ++ )
            {
                cin >> map[i][j];
                if( map[i][j] == '@' )
                {
                    Dx = i;
                    Dy = j;
                }
            }
        ans = 1;
        DFS( Dx, Dy );
        cout << ans << endl;
    }
}

Red and Black 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 o... 阅读详情

相关推荐

HDU1312 Red and Black(黑红砖块)(dfs入门水题)

题目链接:点击打开链接 题目大意:有一个长方形的房间,铺满了正方形瓷砖。每个瓷砖都是红色或黑色的。一个人站在一块黑色的瓷砖上。从一个瓷砖上,他可以移动到四个相邻(上下左右)的瓷砖中的一个。但是他不能移动到红色的瓷砖,只能在黑色的瓷砖上移动。通过重复上面描述的动作,编写一个程序来计算他能达到的黑瓷砖的数量。 输入:多个数据。第一行给出两个数m,n(0,0代表结束输入);m代表列,n代表行。m,n...

本该如此 1303

hdu1312Red and Black”——DFS与递归的应用

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 b

m0_52380556的博客 476

Red and Black&&http://acm.hdu.edu.cn/showproblem.php?pid=1312

DFS不解释。。 #include #include #include #include #define N 50 using namespace std; int n,m; char map[N][N]; int sum; void dfs(int x,int y) { if(map[x+1][y]=='.') {sum++;map[x+1][y]='#';dfs(x

pursuit的专栏 1275

HDU 1312Red and Black (广度优先搜索)

题目来源:http://acm.hdu.edu.cn/showproblem.php?pid=1312问题和上一篇博客一样,都是图的搜索问题上一篇博客采用深度优先搜索,这次采用广度优先搜索对于这个问题两种搜索算法都能AC,时间空间开销也差不多,实现上也差不多,就是把数据结构改一改,point类的核心算法代码改一改1312Red and BlackTime Limit: 2000/1000 MS...

da_kao_la的博客 517

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 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 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(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

HDU-1312 Red and Black

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...

你呢的博客 751

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

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 分析:简单的BFS。使用广搜,使每个满足添加的点进队列,统计删除的点,有多少个点被删除就有多少个满足题意的点。 我的代码: #include #include #include using namespace std; char map[21][21]; bool visited[21][

徵羽調_xiao賤的专栏 899

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

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 1010 Tempter of the Bone
下一篇: HDU 1879 继续畅通工程
小do在努力
博客等级 码龄15年 58粉丝 161原创
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值