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

相关推荐

  • 需求分析文档

    需求分析文档1.引言1.1编写目的:作为软件系统开发技术协议的参考依据,为双方提供参考。根据游戏特点,对被开发软件系统的主要功能、性能进行完整描述,为软件开发者进行详细设计和编程提供基础。为软件提供测试和验收

    2022年8月6日
  • 游戏数值策划

    游戏数值策划游戏数值-拆解方法篇大家好,我是Alice,一名喜欢捣腾数字的游戏数值策划12345~o(* ̄▽ ̄*)ブ工作中我会经常拆解游戏数值,通过数值理解市面上优秀游戏的设计思路。今天想在这里跟大家分享一下我研究游戏数值的流程。如果各位有什么好方法,也请在评论区留言,期待看到你的想法。我平时拆解数值的流程主要分为六个阶段:准备阶段 数据收集 分析数据规律 提出猜想 根据数据验证猜想 拆解的应用不过在介绍具体流程之前,我想和你们先聊一下,数值拆解的目的。我认为数值拆解的目的大概可以分

    2022年5月5日
  • FPN详述

    FPN详述详细讲解特征金字塔(FPN)这个网络的结构。

    2022年6月6日
  • docker五种特性_k8s docker 关系

    docker五种特性_k8s docker 关系DockerDocker网络–link自定义网络网络连通Docker网络Docker是如何进行网络通讯的?查看本机的网卡和ip地址docker0类似与路由器ip地址一样(x.x.x.1)Dockerdocker run -d -P –name=tomcat03 tomcat:8.0查看容器ip地址docker exec -it a72d4ae634da ip addr1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noque

    2022年8月9日
  • python中yield的用法详解——最简单,最清晰的解释

    python中yield的用法详解——最简单,最清晰的解释首先我要吐槽一下,看程序的过程中遇见了yield这个关键字,然后百度的时候,发现没有一个能简单的让我懂的,讲起来真TM的都是头头是道,什么参数,什么传递的,还口口声声说自己的教程是最简单的,最浅显易懂的,我就想问没有有考虑过读者的感受。接下来是正题:首先,如果你还没有对yield有个初步分认识,那么你先把yield看做“return”,这个是直观的,它首先是个return,普通的retur…

    2022年7月23日
  • 5分钟商学院之个人篇–演讲能力和沟通能力

    1.演讲能力1.1认知台阶影响力是成为领导的必要条件,而演讲是实现影响力最重要的方法之一经常有人说,全天下最难的两件事:一是把钱从别人的口袋里掏出来,二是把想法塞到别人的脑海中去。这个说法不

    2021年12月30日

发表回复

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

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