poj1979 DFS

【题解】POJ 1979 Red and Black(DFS) POJ 1979 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 ... 阅读详情
Red and Black
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 18644 Accepted: 9907

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
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
###.###
...@...
###.###
..#.#..
..#.#..
0 0

Sample Output

45
59
6
13
#include <iostream>
#include<cstdio>

using namespace std;
int dir[4][2]={0,1,0,-1,1,0,-1,0};
int w,h,cnt;
char map[25][25];

bool path(int x,int y)
{
    if(x<0||x>=h||y<0||y>=w)
        return false;
    return map[x][y]=='.';
}

void dfs(int x,int y)
{
    for(int i=0;i<4;i++)
    {
        int xx=x+dir[i][0];
        int yy=y+dir[i][1];
        if(path(xx,yy))
        {
            map[xx][yy]='#';
            cnt++;
            dfs(xx,yy);
        }
    }
}

int main()
{
    while(~scanf("%d%d",&w,&h),w+h)
    {
        for(int i=0;i<h;i++)
            scanf("%s",map[i]);
        cnt=1;
        for(int i=0;i<h;i++)
            for(int j=0;j<w;j++)
                if(map[i][j]=='@')
                {
                    dfs(i,j);
                    break;
                }
        printf("%d\n",cnt);
    }
    return 0;
}

POJ 1979 Red and Black(广搜与深搜两种解答) NULL 博文链接:https://128kj.iteye.com/blog/1703983 立即下载

相关推荐

Windows-Anaconda3+TensorFlow安装教程

测试环境:win10-64位和win7-64位 (系统必须是64位,因为TensorFlow目前只支持64位) 一、安装Anaconda3 1.下载Anaconda3 Anaconda官网链接:link 这里我们选择Windows版本,python3.6-version-64位,如下图所示。 2.下载完成之后,点击安装Anaconda3 注意:安装过程中要选择添加环境变量,如图 待安装完成...

DoctorCuiLab的博客 3086

poj1979 解题报告

// // main.cpp // poj1979 // // Created by 韩雪滢 on 11/28/16. // Copyright © 2016 韩雪滢. All rights reserved. // #include <iostream> using namespace std; class Tile{ public: ...

weixin_33744141的博客 144

AutoLISP 学习 (一)

AutoLISP基本语法结构、语法 以括号组成表达式,左右括号一定要成对,单、双引号也要成对,例如: (defun c:hello() (princ "\nHello world!") (prin1) ) 表达式格式:(函数名 操作数 操作数 …),例如: (setq p1 (list 0 0)) (setq p2 (list 10 10)) (setq p3 (list 20 20)) ;;; CAD中画直线,经过p1 p2 p3 (command "LINE" p1 p2 p3 ""

m0_54575622的博客 2123

POJ1979 红与黑 DFS

这道题是我学了dfs之后用来强化这个思想的。就连我的水平也觉得这道题很水。 问题描述: 就像是小时候玩的吃豆子的游戏,上下左右走,然后统计一共能吃到多少个豆子。 算法思想: 其实跟POJ2386差不多,甚至更简单一些,因为这个题基本就是不需要遍历图,只是在起始点周围的四个地方开始运行DFS即可了。 能够正常AC,不知道有没有更快捷简便的方法

Oh233的专栏 3868

Dify实战应用指南(上传需求稿生成测试用例)

摘要: Dify是一款开源大语言模型应用开发平台,通过低代码/无代码方式支持AI应用开发。平台提供五种核心应用类型,包括聊天助手、文本生成、Agent、ChatFlow和Workflow,适用于不同场景。本文以ChatFlow为例,演示如何上传需求稿自动生成测试用例。通过条件分支、文档提取器和LLM设置,实现从需求文档到测试用例的自动化转换,显著提升测试效率并降低人工误差。该方案适用于需要快速生成测试用例的软件开发场景,尤其适合非技术人员使用。

Linda的自动化测试之旅 2744

集成库仑计移动电源设计:从原理到实战,实现精准电量管理

在电池供电设备中,精准的电量计量是提升用户体验和产品可靠性的核心技术。传统基于电压估算的方法因受温度、负载和老化影响误差较大,而库仑计技术通过直接测量充放电电流并对时间积分,实现了电荷量的直接计量,从根本上提升了精度。这项技术的核心价值在于为设备提供了真实、可靠的电量状态数据,是智能电池管理系统的基础。其应用场景广泛,从消费电子如移动电源、智能穿戴,到工业与汽车领域如储能系统、新能源汽车的电池管理都不可或缺。本文以移动电源为例,深入解析了集成库仑计方案的硬件设计要点,特别是围绕高精度采样电阻与差分放大器的电

weixin_33453726的博客 551

poj1979 BFS/DFS

#include #include #include using namespace std; struct node { int x; int y; node(){} node(int xx,int yy):x(xx),y(yy){} }; int n,m; char g[20][20]; int visit[20][20]; int ans; int di

不要挡住我的阳光 429

华为Watch的ECG功能技术分析

华为Watch的ECG(心电图)功能通过硬件传感器、算法优化和医疗认证的结合,实现了对心脏电活动的监测和房颤(AFib)等心律失常的预警。采用医疗级ECG模拟前端芯片(AFE),如TI(德州仪器)的ADS1292R等,支持低噪声、高采样率(通常≥200Hz)。结合PPG(光学心率)数据交叉验证,提高检测可靠性(如ECG和PPG心率不一致时触发警告)。通过RR间期变异性判断心律是否规则,识别房颤(AFib)的典型特征(如不规则不规则心律)。结合加速度计数据,识别手臂运动导致的信号失真,动态调整算法参数。

xu_wenming的博客 6444

poj1979 Red and Black(简单dfs)

Red and BlackTime Limit: 1000MS Memory Limit: 30000KTotal Submissions: 41888 Accepted: 22685DescriptionThere is a rectangular room, covered with square tiles. Each tile is colored either red or black....

码非 239

STM32入门教程:智能储物柜控制

在这个例子中,我们将使用一个4x4的储物柜,共有16个储物格。我们了解了硬件准备、储物柜电路设计、STM32的基本配置、储物柜控制代码的编写以及测试和调试等方面的知识。根据我们的电路设计,我们需要将一些引脚配置为输出,并将其他引脚配置为输入。在本教程中,我们将使用STM32微控制器来实现一个基本的智能储物柜控制系统。在主函数中,我们将不断循环读取传感器数据,并根据数据的变化来控制储物柜的开关和显示相关信息。这个函数将读取人体感应传感器、温湿度传感器和光照传感器的数据,并将数据存储到相应的变量中。

m0_75244442的博客 662

《YOLO多模态创新改进专栏目录 》全网独家创新,多模态融合改进教程,包含早期融合、中期融合、后期融合、损失函数改进、二次创新模块、独家创新等几百种创新点改进,答疑群提供完整项目,永久更新中

🔥《YOLO多模态创新改进专栏》重磅来袭!🚀 本专栏汇集最新顶会顶刊创新点,提供1000+篇YOLO多模态改进方案(涵盖v8-v26),包含: 多模态融合策略(早期/中期/后期) 损失函数优化 独家创新模块 详细改进步骤 ✨特色服务: ✔️ 配套完整可运行项目 ✔️ 专业答疑群支持(含非计算机专业指导) ✔️ 持续更新至2026年 ✔️ 适用目标检测/OBB/分割/分类等多任务 💡适合人群: 科研人员 | 研究生 | 需发SCI/CCF论文者 环境要求宽松,新手友好,助力快速产出科研成果! 📈早订优

Ai缝合怪、助力小伙伴高效发论文 747

POJ1979(DFS)

POJ1979(DFS) Red and Black 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 ti

shui2104的博客 323

基于STM32的嵌入式平台实现EtherCAT主站

本文将介绍如何在基于STM32的嵌入式平台上构建EtherCAT主站,并提供相应的源代码。使用SOEM库提供的API函数,我们可以实现与EtherCAT从站设备的实时通信和数据交换。在STM32的代码中,使用SOEM库提供的API函数初始化EtherCAT主站。在SOEM库中,我们可以找到EtherCAT主站的实现代码和相关的API函数。使用SOEM库提供的API函数实现EtherCAT主站的功能,包括发送和接收EtherCAT数据帧、处理从站设备的状态等。确保连接正常并且设备能够相互通信。

CodeSageX的博客 2834

POJ-1979 深度优先搜索DFS

POJ-1979这道题用到的是深度优先搜索(DFS),题目本身很好理解,但是里面有个陷阱啊。。 个人理解: 这道题首先应该在输入时找出(@)所在位置并且从此处开始进行深度优先搜索遍历,在运用DFS时,要注意将已经遍历的点更新为“#”。遍历时从上下左右四个方向进行遍历,直到遇到下一个“.”。具体看代码中的注释。 C++代码: #include #include #defi

MainTain_的博客 675

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

POJ 1979 (DFS、BFS入门 、递归回溯 )

DFS(深度优先搜索)和BFS(广度优先搜索)是两种搜索策略,属于基础的常用算法。 DFS顾名思义 先优先搜索深处的,可比喻为 一条路一直往下走,走到不能走的时候,原路返回,返回到有路可走,在继续走,如此循环。 看下图 DFS: 由此图我们可知,DFS一次走一条路,直到无路可走,并且,不走已经走过的路,那么我们如何让计算机知道这条路已经走过呢? 一般用二维数组,将走过的位置标记为 1,...

GD_ONE的博客 729

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

POJ - 1979 (dfs)

POJ - 1979 (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 ad...

weixin_44498746的博客 342

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

PoJ1979 Red and Black (DFS)

题目链接:http://poj.org/problem?id=1979 题意:   有一个由正方形的黑色砖块和红色砖块组成的矩形广场,一个人站在广场上的某处,他可以选择往四周(上下左右)的黑色砖块上走,问他一共可以走多少个砖块(加上他本身站立的砖块).   "."为黑色砖块   "#"为红色砖块   "@"为人的起始位置 思路:   利用DFS 从人的起始位置开始搜索,搜索...

dimaishu7553的博客 121

Red and Black//POJ - 1979//dfs

Red and Black//POJ - 1979//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 ...

salty_fishman的博客 133

POj 1979 Red and Black(容易理解的dfs)

#include<stdio.h> #include<stdlib.h> #include<string.h> int map[21][21]; int m = 0, n = 0; int num = 0; int d[4][2] = { {-1,0},{0,-1},{1,0},{0,1} }; void dfs(int x, int y) { int i = 0, j = 0, dx = 0, dy = 0; num++; map[x][y] = '#'; fo

谦卑 435

poj 1979 Red and Black(DFS)

传送门:http://poj.org/problem?id=1979 昨天晚上做了这个题,死活没做对,当时就想这么水的题怎么能错呢?今天一看,这个题输入行和列的顺序和以前做的题正好颠倒了(我一直记得之前都是先输入行,再输入列,这个题先输入的列),恍然大悟 #include char map[21][21]; int sum,n,m; int next[4][2] = {{0,1},{1,0}

梦幻的蔷薇色 378

POJ 1979 DFS

题目链接:http://poj.org/problem?id=1979 #include<cstring> #include<iostream> using namespace std; int n=0,h=0,sum=0; char aa[21][21]; void DFS(int p,int q) { if(aa[p][q]=='.'&a...

weixin_30532759的博客 128

poj1979 Red and Black(dfs判断联通分量)

Red and Black Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 19925   Accepted: 10677 Description There is a rectangular room, covered with square tiles. Each

twobqn123的专栏 884

Red and Black/POJ 1979/DFS

Red and Black/POJ 1979/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 fo...

weixin_45600829的博客 146

【poj】1979--Red and Black (DFS)

题目链接:点击查看 第一次写DFS的文章,也是才开始做DFS的题,这道题花了我不少时间,第一遍是根据做的上一个DFS题poj的1088题的标答改出来的,因此有一些不必要的操作。然后,晚上想了想DFS的实现过程,用程序跑了一遍,发现跟我想的实现过程差不多。主要就是递归和遍历(可能吧)。删去了不必要的计步数组只用visit。另外还学到了如何简便的实现方向移动。 另外POJ好像不资瓷bits/std...

Dicer_的博客 330

DFS相关专题练习——POJ 1979, POJ3009, C++ 代码实现

POJ 1979:Red and Black http://poj.org/problem?id=1979 一道简单的DFS入门题,主要需要注意的地方就是输入输出的方式:输入是 scanf 按行读入而非按字符读入;输出需要针对多个输入时给出结果,使用while循环来实现。 #include &amp;lt;iostream&amp;gt; #include &amp;lt;cstdio&amp;gt; using name...

Choly_chen的博客 411
上一篇: zoj1008 dfs
下一篇: zoj1649 BFS
捡石子的小孩
博客等级 码龄14年 51粉丝 117原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值