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


相关推荐

  • hdu 3037 Saving Beans(组合数学)

    hdu 3037 Saving Beans(组合数学)

    2021年11月29日
    58
  • Linux系统平均负载是如何计算的?[通俗易懂]

    Linux系统平均负载是如何计算的?[通俗易懂]关于负载的计算,它的结果是包含有小数的一个浮点数,内核中是不能使用float变量的,那么这里就采用了一个整型变量的低11位来表示小数部分。那么对于数值1来说,它就是FIXED_1,也就是需要对1进行左移11bit。实际上此时这个整型变量保存的值是1024。cat/proc/loadavg0.430.580.655/701045102那么我们通过cat命令查看负载值如上说是,它显示的是带有两个小数表示的一个浮点数,所以最后在输出这个数值时还需要做一个转换,如果从1024个值中得出这100小数

    2025年11月4日
    2
  • maven中web3sdk引入问题

    maven中web3sdk引入问题

    2021年3月12日
    197
  • oracle存储过程语法

    oracle存储过程语法前两天无意见看见了一个非常适合学习Oracle附上链接:https://blog.csdn.net/yucaifu1989/article/details/15813793Oracle存储过程基本语法存储过程   1CREATEORREPLACEPROCEDURE存储过程名   2IS   3BEGIN   4NULL;   5END; 行1:   CREAT…

    2022年7月17日
    17
  • js数组常用方法「建议收藏」

    js数组常用方法「建议收藏」1.join()(数组转字符串)数组转字符串,方法只接收一个参数:即默认为逗号分隔符()。&lt;script&gt; vararr=[1,2,3,4]; console.log(arr.join());//1,2,3,4 console.log(arr.join(":"));//1:2:3:4 console.log(arr);//[1,2,3,4],原数组不变&l…

    2022年5月18日
    51
  • 八路抢答器单片机c语言程序_八路抢答器单片机c语言程序

    八路抢答器单片机c语言程序_八路抢答器单片机c语言程序该楼层疑似违规已被系统折叠隐藏此楼查看此楼改成开始前抢答蜂鸣器响,红灯亮#include#defineuintunsignedint#defineucharunsignedcharsbitSW1=P1^0;//******sbitSW2=P1^1;//*八*sbitSW3=P1^2;//*路*sbitSW4=P1^3;//*抢*sb…

    2022年10月20日
    2

发表回复

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

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