hdu 1312 Red and Black

简介: 一个人从@点出发,求他所能到达的'.'的数目,'#'不可走,@本身算1个点。思路:搜索入门题。

Red and Black

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 258   Accepted Submission(s) : 139


Problem 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)


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


Asia 2004, Ehime (Japan), Japan Domestic

题意:一个人从@点出发,求他所能到达的'.'的数目,'#'不可走,@本身算1个点。

思路:搜索入门题。


#include<stdio.h>
#include<string.h>
char as[22][22];
int hash[22][22];
int dx[]={0,0,1,-1};
int dy[]={1,-1,0,0};
int w,h,ans;
void dfs(int x,int y)
{
  int i,xx,yy;
  hash[x][y]=1;
  for(i=0;i<4;i++)
  {
    xx=x+dx[i];
    yy=y+dy[i];
    if(xx>=0&&xx<h&&yy>=0&&yy<w&&as[xx][yy]=='.'&&!hash[xx][yy])
    {
      ans++;
      dfs(xx,yy);
    }
  }
}
int main()
{
  int i,j,x,y;
  while(scanf("%d%d",&w,&h)!=EOF)
  {
    if(w==0&&h==0)
      break;
    for(i=0;i<h;i++)
      scanf("%s",as[i]);
    for(i=0;i<h;i++)
    {
      for(j=0;j<w;j++)
      {
        hash[i][j]=0;
        if(as[i][j]=='@')
        {
          x=i;
          y=j;
        }
      }
    }
    ans=1;
    dfs(x,y);
    printf("%d\n",ans);
  }
  return 0;
}


相关文章
|
网络协议 机器人 C++
KUKA机器人Socket通讯配置方法:技术干货分享
【10月更文挑战第7天】在现代自动化生产线上,KUKA机器人凭借其高效、灵活和精确的特点,成为众多企业的首选。为了实现KUKA机器人与其他设备或系统之间的数据交互,Socket通讯配置显得尤为重要。本文将详细介绍KUKA机器人Socket通讯的配置方法,帮助大家在工作中更好地掌握这一技术。
2683 2
|
人工智能 机器人 API
Dify 构建一个基于 GPT 的 AI 客服系统
Dify 构建一个基于 GPT 的 AI 客服系统
2445 0
|
存储 JSON 安全
云服务器CentOS8.2安装部署Docker一文详解
云服务器CentOS8.2安装部署Docker一文详解
1660 0
云服务器CentOS8.2安装部署Docker一文详解
|
存储 JSON 关系型数据库
谈谈电商系统中的商品模块设计
谈谈电商系统中的商品模块设计
1423 0
|
分布式计算 Ubuntu Java
|
Oracle 关系型数据库 MySQL
oracle 行转列函数 wm_concat(列)
oracle 行转列函数 wm_concat(列)
1326 0
|
2天前
|
监控 网络协议 算法
2026年起SSL证书有效期逐年缩短:企业如何避免“续期风暴”!
SSL证书有效期将从2026年起分三阶段逐步缩短至47天,人工续期难以为继。阿里云“WoSign SSL证书+自动化服务”,支持域名监控、自动托管、云产品/服务器一键部署及智能提醒,助力企业高效应对高频续期挑战。
8072 2
|
11天前
|
域名解析 人工智能 Linux
|
21天前
|
Linux Go 开发工具
手机上跑 DeepSeek 智能体:开源安卓客户端 DSH Go 安装与使用指南
DSH Go 是一款开源安卓客户端,将 DeepSeek Harness 智能体引擎装进手机:免 Root、免 Termux,一键安装即用;支持本机运行(内置 Ubuntu+Node.js)或远程接管电脑会话;具备插话打断、过程折叠、屏幕操控、端到端加密分享等特色功能,真正实现智能体“口袋化”。
18755 64