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


相关推荐

  • 如何学习FPGA「建议收藏」

    如何学习FPGA「建议收藏」PS:笔者强烈建议诸位注册一个EETOP的账号,每天签到或者发贴、回贴就有积分了,里面的资源非常丰富,各种软件、资料都能找到。一、入门首先要掌握HDL(HDL=verilog+VHDL)。第一句话是:还没学数电的先学数电。然后你可以选择verilog或者VHDL,有C语言基础的,建议选择VHDL。因为verilog太像C了,很容易混淆,最后你会发现,你花了大量时间…

    2022年5月3日
    82
  • eureka底层原理「建议收藏」

    eureka底层原理「建议收藏」什么是注册中心全称为:服务注册与发现,rpc远程调用框架核心思想,在于注册中心,使用注册中心管理每个服务与服务之间的依赖关系,这种关系被称为服务治理概念;任何rpc远程调用框架都至少有一个注册中心;服务注册将服务信息注册到注册中心,相当于告诉公司的人,我已经打卡上班了,可以分配工作任务给我了,比如现在我们有一个服务消费者服务A,和两个节点的服务提供者,服务B。服务A和服务B在启动的时候都会向注册中心进行服务注册。服务发现从注册中心获取已注册的服务信息,发现有哪些可以调用的服务可供我使用;相

    2022年10月7日
    6
  • Linux系统定时任务「建议收藏」

    Linux系统定时任务定时任务CrondCrond是linux系统中用来定期执行命令/脚本或指定程序任务的一种服务或软件,一般情况下,我们安装完Centos5/6linux操作系统之后,默认便会启动Crond任务调度服务。Crond服务会定期(默认每分钟检查一次)检查系统中是否有要执行的任务工作,如果有,便会根据其预先设定的定时任务规则自动执行该定时任务工作,这个crond定…

    2022年4月16日
    48
  • cmd查询mysql端口占用,Window通过cmd查看端口占用、相应进程、杀死进程等的命令…「建议收藏」

    cmd查询mysql端口占用,Window通过cmd查看端口占用、相应进程、杀死进程等的命令…「建议收藏」如何查看程序占用的端口一、查看所有进程占用的端口在开始-运行-cmd,输入:netstat–ano可以查看所有进程二、查看占用指定端口的程序当你在用tomcat发布程序时,经常会遇到端口被占用的情况,我们想知道是哪个程序或进程占用了端口,可以用该命令netstat–ano|findstr“指定端口号”二、查看占用指定端口的程序当你在用tomcat发布程序时,经常会遇到端口被占用的情况…

    2022年5月19日
    39
  • Windows命令行route命令使用图解

    Windows命令行route命令使用图解一操作实例查看当前本机的路由表;有三部分,接口列表,IPv4路由表,IPv6路由表;查看0.打头的路由表信息;添加一条添加默认网关地址为192.168.12.1的默认路由;删除前面添加的路由;添加跃点数为7的路由;删除之;添加接口索引为某个值的路由,不知为何失败;下

    2022年7月18日
    29
  • quotient函数_Mid函数

    quotient函数_Mid函数QuotedStr()转载于:https://www.cnblogs.com/ljjphysics/archive/2011/04/25/2028670.html

    2022年10月18日
    6

发表回复

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

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