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


相关推荐

  • p6spy简介_p6教程

    p6spy简介_p6教程在公司项目中运用了这项技术,一开始不清楚这是干啥用的,在网上查找资料有所一定的了解,但是应该不够全面,希望可以评论指出。p6spy是数据库动态监控的一种框架,它可以使得数据库数据无缝拦截和操作,而不必对现有应用程序的代码作任何修改。P6Spy分发包包括P6Log,它是一个可记录任何Java应用程序的所有JDBC事务的应用程序。其配置完成使用时,可以进行数据访问性能的监测。下面我们来看一下…

    2022年9月28日
    3
  • ORM常用操作

    一般操作专业官网文档必会13条查询<1>all():查询所有结果<2>filter(**kwargs):它包含了与所给筛选条件相匹配的对象<3>

    2022年3月29日
    41
  • int型转换为long型遇到的一个小问题

    int型转换为long型遇到的一个小问题LeetCode上有一道题:给出一个数n,求(0,n)之间素数的个数。然后我采用埃拉托斯特尼筛法在每次找到一个素数时,将能被素数整除的数排除掉。但是,在进行int类型转换的时候会报:java.lang.ArrayIndexOutOfBoundsException代码如下:publicintcountPrimes(intn){boolea…

    2022年6月4日
    55
  • 图像质量评价方法PSNR+SSIM&&评估指标SROCC,PLCC

    图像质量评价方法PSNR+SSIM&&评估指标SROCC,PLCCupdate:2018-04-07今天发现ssim的计算里面有高斯模糊,为了快速计算,先对每个小块进行计算,然后计算所有块的平均值。可以参考源代码实现,而且代码实现有近似的在里面!matlab中中图

    2022年8月3日
    17
  • Idea激活码永久有效Idea2017.2.6激活码教程-持续更新,一步到位[通俗易懂]

    Idea激活码永久有效Idea2017.2.6激活码教程-持续更新,一步到位[通俗易懂]Idea激活码永久有效2017.2.6激活码教程-Windows版永久激活-持续更新,Idea激活码2017.2.6成功激活

    2022年6月17日
    71
  • Docker镜像相关命令

    Docker镜像相关命令1、查看docker版本 docker version 2、列出本地主机上的镜像: docker images REPOSITORY:表示镜像的仓库源 TAG:镜像的标签 IMAGE ID:镜像ID CREATED:镜像创建时间 SIZE:镜像大小3、查询镜像 docker search 镜像名称…

    2022年6月13日
    30

发表回复

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

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