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


相关推荐

  • Centos 7 Mysql 配置文件位置

    Centos 7 Mysql 配置文件位置一、Mysql的配置my.cnf位置1)、使用命令:psaux|grepmysql|grep’my.cnf’如果没有没有输出内容则是使用默认配置位置二、默认配置my.cnf位置使用命令:mysql–help|grep’my.cnf’/etc/my.cnf、/etc/mysql/my.cnf、/usr/local/etc/my.cnf、~/….

    2022年5月13日
    58
  • 计算机网络谢希仁第八版 课后答案第七版课后答案

    计算机网络谢希仁第八版 课后答案第七版课后答案谢希仁计算机网络第七版课后答案第一章概述1-01计算机网络向用户可以提供那些服务?答:连通性和共享1-02简述分组交换的要点。答:(1)报文分组,加首部(2)经路由器储存转发(3)在目的地合并1-03试从多个方面比较电路交换、报文交换和分组交换的主要优缺点。答:(1)电路交换:端对端通信质量因约定了通信资源获得可靠保障,对连续传送大量数据效率高。(2)报文交换:无须预约传输带…

    2022年6月17日
    23
  • python 2021 4月激活码【在线破解激活】

    python 2021 4月激活码【在线破解激活】,https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月16日
    59
  • 数组求和方法汇总_用函数的方法对输入的数组求和

    数组求和方法汇总_用函数的方法对输入的数组求和vararr=[1,2,3,4,5,6];测试时我不想过度使用全局变量影响命名空间,所以没使用未声明变量。而是直接通过私有作用域设置静态私有变量,也可以用其他设计模式来限定变量作用域。因为数组对象的迭代方法也是一种遍历,所以也可以借助用来实现求和。一、利用数组对象的各迭代方法:1.array.every()查询是否有所有项都匹配的方法:1(function(){…

    2022年9月28日
    0
  • height:100vh的应用

    height:100vh的应用今天改移动端页面样式的时候因为height:100vh,导致我想超出部分滚动页面的效果没有做出来。就查查这玩意是啥意思。别人解释的height:100vhvh就是当前屏幕可见高度的1%,也就是说height:100vh==height:100%;但是当元素没有内容时候,设置height:100%,该元素不会被撑开,此时高度为0,但是设置height:100vh,该元素会被撑开屏幕高…

    2022年5月27日
    31
  • exe免杀加壳工具包_grep过滤不想要的

    exe免杀加壳工具包_grep过滤不想要的简介该工具是由Arks7使用Go语言开发的一个免杀生成器模板,目前可以过国内主流杀毒。GitHub地址:https://github.com/Arks7/Go_Bypass用法使用CobaltStrike生成payload,输出格式为Raw,4.3版本需要勾选X64,如图:将生成的文件放在Go_Bypass项目目录下,然后执行goenv-wGOPROXY=https://goproxy.io,direct配置代理,否则编译报错。然后运行gorunmain.go,使用默认配置一路回车即

    2022年8月20日
    5

发表回复

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

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