博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu-1312
阅读量:5330 次
发布时间:2019-06-14

本文共 2078 字,大约阅读时间需要 6 分钟。

Red and Black

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 19524    Accepted Submission(s): 11879

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
....#.
.....#
......
......
......
......
......
#@...#
.#..#. 
 

 

Sample Output
 
45
 

 

题意:

@为起点,可上下左右走,不能走#,问可以走多少.

 

简单DFS

 

附AC代码:

1 #include
2 using namespace std; 3 4 int ans,sx,sy,n,m; 5 char ma[30][30]; 6 int a[4][2]={
1,0,-1,0,0,1,0,-1}; 7 8 void dfs(int x,int y){ 9 ans++;10 ma[x][y]='#';11 for(int i=0;i<4;i++){12 int tx=x+a[i][0];13 int ty=y+a[i][1];14 if(tx
=0&&ty
=0&&ma[tx][ty]=='.'){15 dfs(tx,ty);16 }17 }18 return ;19 }20 21 int main(){22 while(cin>>n>>m){23 if(n==0&&m==0)24 break;25 ans=0;26 for(int i=0;i
>ma[i][j];29 if(ma[i][j]=='@'){30 sx=i;31 sy=j;32 //cout<
<<" "<
<

 

转载于:https://www.cnblogs.com/Kiven5197/p/6613193.html

你可能感兴趣的文章
泛型子类_属性类型_重写方法类型
查看>>
eclipse-将同一个文件分屏显示
查看>>
mysql5.x升级至mysql5.7后导入之前数据库date出错的解决方法!
查看>>
对闭包的理解
查看>>
练习10-1 使用递归函数计算1到n之和(10 分
查看>>
Oracle MySQL yaSSL 不明细节缓冲区溢出漏洞2
查看>>
windows编程ASCII问题
查看>>
.net webService代理类
查看>>
Code Snippet
查看>>
Node.js Express项目搭建
查看>>
zoj 1232 Adventure of Super Mario
查看>>
1201 网页基础--JavaScript(DOM)
查看>>
组合数学 UVa 11538 Chess Queen
查看>>
oracle job
查看>>
Redis常用命令
查看>>
XML学习笔记(二)-- DTD格式规范
查看>>
IOS开发学习笔记026-UITableView的使用
查看>>
[转载]电脑小绝技
查看>>
windos系统定时执行批处理文件(bat文件)
查看>>
thinkphp如何实现伪静态
查看>>