首页 > 其他分享 >HDU-Red and Black

HDU-Red and Black

时间:2022-12-21 13:32:11浏览次数:46  
标签:HDU sx int tiles Black tile black Red he


 

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

 

 

代码:

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
int dir[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
char map[33][33];
int vis[33][33];
int n,m;
int sum;
#define inf 0xfffffff
void dfs(int x,int y)
{
int sx,sy,i;
sum++;
for(i=0;i<4;i++)
{
sx=x+dir[i][0];
sy=y+dir[i][1];
if(sx>=n ||sy>=m ||sx<0 ||sy<0 ||map[sx][sy]=='#' ||map[sx][sy]=='@')
continue;
map[sx][sy]='@';
dfs(sx,sy);
}
}
int main()
{
int i,j,x,y;
while(scanf("%d%d",&m,&n)!=EOF&&n!=0 &&m!=0)
{
for(i=0;i<n;i++)
scanf("%s",map[i]);
for(i=0;i<n;i++)
for(j=0;j<m;j++)
{
if(map[i][j]=='@')
{
x=i;
y=j;
}
}
sum=0;
dfs(x,y);
printf("%d\n",sum);
}
return 0;
}

 

 然而这道题并不懂,什么广搜!

标签:HDU,sx,int,tiles,Black,tile,black,Red,he
From: https://blog.51cto.com/u_15915810/5959844

相关文章

  • Centos7安装Redis(30秒完成)
    Centos7安装Redis1.1安装redis1)下载wget、gcc、gcc-c++命令yum-yinstallwgetgccgcc-c++2)在线下载rediswgethttp://download.redis.io/releases/redis-4.0.11.tar.gz3......
  • Redis持久化底层原理详解
    Redis持久化由于Redis是一个内存数据库,所谓内存数据库,就是将数据库中的内容保存在内存中,这与传统的MySQL,Oracle等关系型数据库直接将内容保存到硬盘中相比,内存数据库的读......
  • XAMPP里tomcat启动报错:Make sure you have Java JDK or JRE installed and the requir
    ​​参考博客​​**1.运行命令行win+R在cmd中输入regedit出现下面的界面**2.在注册表中添加HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft/JavaDevelopmentKit3.成功界......
  • SharedPreferences存储
    不同于文件存储的方式,SharedPreferences是使用键值对的方式来存储数据的。也就是说,当保存一条数据的时候,需要给这条数据提供一个对应的键,这样在读取数据的时候就可以通过......
  • 什么是Redis持久化,如何理解?
    其实redis就是一种高级的以键值对形式存储数据的数据库,而它的好处就是他可以支持数据的持久化,其实redis之所以会有这样的优点,主要是因为,redis的数据都是存放在内存中的,如......
  • redis—安装以及可视化
    redis是一种非关系型数据库,什么是非关系型数据库,之前我们在mysql专栏也有提到过,这边就不再过多的赘述,忘记了的小伙伴可以再次阅读这篇文章  终于明白了数据库的【关系......
  • Docker高级篇:实战Redis集群!从3主3从变为4主4从
    通过前面两篇,我们学会了三主三从的Redis集群搭建及主从容错切换迁移,随着业务增加,可能会有主从扩容的,所以,本文我们来实战主从扩容PS本系列:《Docker学习系列》教程已经发布......
  • forward和redirect的区别
    Forward:不需要客户端配合,全服务器端动作。只能forward到同servlet服务器的页面。forward后,执行浏览器上的“后退”操作,会回到上一个页面。如果提交表单页面没有跳转,刷新页......
  • Tomcat+redis+nginx配置
    为客户开发的一个绩效系统,采用了javaweb的开发方式,使用了一些springmvc,mybatis之类的框架。相比于oracleebs的二次开发,这种开发更加灵活,虽然和ebs集成的时候遇到一些问......
  • Redis热点大Key的优化过程
    概念介绍热点Key产生的背景用户消费的数据远大于生产的数据(热卖商品、热点新闻、热点评论、热门明星直播)。对于电商网站中,我们经常可以会遇到热门商品的抢购或者秒杀场景以......