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)
上一篇 2021年11月16日 上午10:00
下一篇 2021年11月16日 上午10:00


相关推荐

  • 使用 Windows Live Writer[通俗易懂]

    使用 Windows Live Writer[通俗易懂]导航1.安装2.配置3.使用入门4.技巧1.安装绿色版下载地址:http://www.xdowns.com/soft/1/16/2007/Soft_38256.html2.配置以下内容是配置CSDN博客(其他的BLOG配置自己摸索):1.在WindowsLiveWriter,点击菜单“日志/添加日志账户”

    2022年10月7日
    4
  • svn中文语言包安装(最详细步骤)*

    svn中文语言包安装(最详细步骤)*标题 svn 中文语言包安装 最详细步骤 一 查看自己的 SVN 版本 这里省略也可以 同事 1 9 版本的直接在官网下载的语言包也能用 区别对待把 可以先直接在官网下载 不行再去找对应版本语言包 1 打开 SVN 找到关于 最后一个 英文的是 About 我这里是 1 12 2 版本如果跟我一样的 可以直接点击下载 SVN1 12 2 中文语言包也可以复制链接网页直接打开

    2026年3月20日
    4
  • 豆包是否可以本地部署 自主可控环境下运行豆包的技术路径说明

    豆包是否可以本地部署 自主可控环境下运行豆包的技术路径说明

    2026年3月12日
    2
  • 使用随机函数rand()和srand()来产生三个_随机函数怎么按

    使用随机函数rand()和srand()来产生三个_随机函数怎么按srand函数是随机数发生器的初始化函数。原型:voidsrand(unsignedintseed);srand和rand()配合使用产生伪随机数序列。rand函数在产生随机数前,需要系统提供的生

    2022年8月1日
    9
  • Redis主从同步原理-SYNC

    Redis主从同步原理-SYNC和 MySQL 主从复制的原因一样 Redis 虽然读取写入的速度都特别快 但是也会产生读压力特别大的情况 为了分担读压力 Redis 支持主从复制 Redis 的主从结构可以采用一主多从或者级联结构 下图为级联结构 Redis 主从复制可以根据是否是全量分为全量同步和增量同步 1 全量同步 Redis 全量复制一般发生在 Slave 初始化阶段 这时 Slave 需要将 Master 上的所有数据都复制一份 具体步骤

    2026年3月19日
    2
  • 读取DXF格式文件

    读取DXF格式文件读取 DXF 格式文件

    2026年3月19日
    3

发表回复

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

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