POJ 1979 Red and Black

POJ 1979 Red and Black

Red and Black
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 21482   Accepted: 11488

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

Source

思路 :简单的深搜
#include<iostream>
#include<string.h>
#include<stdio.h>
using namespace std;
int r,c;
char a[200][200];
int vis[200][200];
int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
int sum;

void dfs(int i,int j)
{
             
  //if(a[i][j]=='#'||(vis[i][j])||(i<0||j<0||(i>r-1||j>c-1)))
  //return;
  //else 
  //{
      a[i][j]='#';  
      sum++;                                           
  for(int k = 0;k < 4;k++)
  {
         int x = i+dir[k][0];
         int y = j+dir[k][1];
         if(a[x][y]=='.'&&!vis[x][y]&&x>=0&&y>=0&&x<=r-1&&y<=c-1)  
        {
           dfs(x,y);
           vis[x][y]=1;
         }
  } 
  //} 
  
  
}
int main()
{
   int i,j,x,y;
   while(scanf("%d%d",&c,&r),r||c) 
   { 
          sum=0;
          memset(vis,0,sizeof(vis));
          memset(a,0,sizeof(a));
                                   
      for(i=0;i<r;i++)
      {
           getchar();
        for(j=0;j<c;j++)
        {
             a[i][j] = getchar();
             if(a[i][j]=='@')
             {
                x=i;
                y=j;                
             }          
        }
       } 
       //count=0;
       dfs(x,y); 
       cout<<sum<<endl;                               
   }         
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/119108.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • gbdt算法_dlt算法

    gbdt算法_dlt算法GBDT算法参数详解

    2022年10月11日
    3
  • CAS认证失败「建议收藏」

    CAS认证失败「建议收藏」如果是配置域名的,CAS这个服务器要能够ping得通这个域名

    2022年6月18日
    49
  • Nexus 3的搭建和简单使用介绍

    搭建Nexus 3私服一、简介nexus 私服间于本地仓库和中央仓库直接。1、有两种安装方式:使用tomcat启动,Tgz使用自带的Jetty启动 ,zip包(推荐使用)2、下载地址 : Nexus oss3、环境准备: jdk8+ + maven3+二、安装步骤1、windos上安装– nexus 2.x 版本1、将bin添加到环境变量中,nexus2、修改/bin…

    2022年2月27日
    55
  • SpringCloud与Dubbo的比较

    SpringCloud与Dubbo的比较Dubbo一、dubbo简介Dubbo是阿里巴巴公司开源的一个高性能优秀的服务框架,使得应用可通过高性能的RPC实现服务的输出和输入功能,可以和Spring框架无缝集成。Dubbo是一款高性能、轻量级的开源JavaRPC框架,它提供了三大核心能力:面向接口的远程方法调用,智能容错和负载均衡,以及服务自动注册和发现。现已发展成为Apache的顶级孵化开源项目,详见官网:…

    2022年6月14日
    28
  • Hello,C++(4)继承

    Hello,C++(4)继承

    2021年7月6日
    60
  • c# 进度条的使用(例子)[通俗易懂]

    c# 进度条的使用(例子)[通俗易懂]2012-05-2320:21在用c#做WinFrom开发的过程中。我们经常需要用到进度条(ProgressBar)用于显示进度信息。这时候我们可能就需要用到多线程,如果不采用多线程控制进度条,窗口很容易假死(无法适时看到进度信息)。下面我就简单结合一个我写的例子给大家做一个介绍。首先看一下程序界面第一步:设计界面不说了…注意需要引用usingSyst

    2025年6月12日
    3

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号