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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • python调用c++动态库_python登陆mt4

    python调用c++动态库_python登陆mt4广告关闭腾讯云11.11云上盛惠,精选热门产品助力上云,云服务器首年88元起,买的越多返的越多,最高返5000元!运行平台:windowspython版本:python3.6ide:sublimetext其他工具:chrome浏览器0、写在前面的话本文是基于基础版上做的修改,如果没有阅读基础版,请移步python爬虫抓取智联招聘(基础版)在基础版中,构造url时使用了urllib…

    2022年8月15日
    9
  • 全局平均池化(全局池化代替全连接层)

    全局平均池化是在论文NetworkinNetwork中提出的,原文中全局平均池化的作用和优点:思想:对于输出的每一个通道的特征图的所有像素计算一个平均值,经过全局平均池化之后就得到一个维度==类别数的特征向量,然后直接输入到softmax层作用:代替全连接层,可接受任意尺寸的图像优点:1)可以更好的将类别与最后一个卷积层的特征图对应起来(每一个通道对应一种…

    2022年4月18日
    222
  • 机房收费系统————导出到Excel

    机房收费系统————导出到Excel机房收费系统————导出到Excel

    2022年4月24日
    45
  • clone一个react项目怎么运行[通俗易懂]

    首先当你从git上面clone一个项目的时候怎么让项目跑起来,首先看项目目录结构,找到README.md上面有项目运行的步骤,如果没有可以看package.json文件,找到scripts上面有dev所以跑起来项目就使用npmrundev有start就使用npmstart但是要先安装项目依赖使用npminstall依赖下载完成就可以使用npmrundev/npm…

    2022年4月15日
    35
  • C#–winform界面美化[通俗易懂]

    C#–winform界面美化[通俗易懂]1、工控上位机界面总结(参考贴:https://blog.csdn.net/zqrhzyj/article/details/76638948)一般的工控界面分成三部分:(1)、标题菜单部分,即项目名称、界面菜单等(2)、数据显示及按钮等部分,即图形显示区,可以显示工艺流程图,采集到的相关数据信息、控制按钮等。(3)、尾部部分,可以添加公司的相关信息等。…

    2022年5月28日
    38
  • 图标不对齐_font manager

    图标不对齐_font manager采用如下方式可解决首先是外层:text-align:center;display:table;之后里面的i元素:vertical-align:middle;display:table-cell;

    2025年5月24日
    4

发表回复

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

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