Poj1979 Red and Black (DFS)
| Time Limit: 1000MS | Memory Limit: 30000K | |
| Total Submissions: 47466 | Accepted: 25523 |
Description
Write a program to count the number of black tiles which he can reach by repeating the moves described above.
Input
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
Sample Input
6 9
....#.
.....#
......
......
......
......
......
#@...#
.#..#.
11 9
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
###.###
...@...
###.###
..#.#..
..#.#..
0 0
Sample Output
45
59
6
13
Source
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
char a[][];
int n,m;
int res=;
int dx[]={,-,,};
int dy[]={,,,-};
void dfs(int x,int y)
{
res++;
a[x][y]='#';
for(int i=;i<;i++){
int nx=x+dx[i],ny=y+dy[i];
if(nx>=&&nx<n&&ny>=&&ny<m&&a[nx][ny]=='.'){
dfs(nx,ny);
}
}
return ;
}
void solve()
{
for(int i=;i<n;i++){
for(int j=;j<m;j++){
if(a[i][j]=='@'){
dfs(i,j);
}
}
}
}
int main()
{
while(cin>>m>>n&&n!=&&m!=){ res=;
for(int i=;i<n;i++){
for(int j=;j<m;j++){
cin>>a[i][j];
}
}
solve();
cout<<res<<endl;
}
return ;
}
脱离参考书自己再根据自己的理解过一遍:
#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>
using namespace std;
int n,m;
char a[][];
int sx,sy,nx,ny;
int dx[]={,,,-};
int dy[]={,-,,};
int res;
void dfs(int x,int y)
{
res++;
a[x][y]='#';
for(int i=;i<;i++){
nx=x+dx[i],ny=y+dy[i];
if(nx>=&&nx<m&&ny>=&&ny<n&&a[nx][ny]=='.'){
dfs(nx,ny);
}
}
}
int main()
{
while(cin>>n>>m&&(n&&m)){
for(int i=;i<m;i++){
for(int j=;j<n;j++){
cin>>a[i][j];
if(a[i][j]=='@'){
sx=i,sy=j;
}
}
}
res=;
dfs(sx,sy);
cout<<res<<endl;
}
return ;
}
Poj1979 Red and Black (DFS)的更多相关文章
- POJ-1979 Red and Black(DFS)
题目链接:http://poj.org/problem?id=1979 深度优先搜索非递归写法 #include <cstdio> #include <stack> using ...
- HDU 1312 Red and Black (dfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312 Red and Black Time Limit: 2000/1000 MS (Java/Oth ...
- HDU1312 Red and Black(DFS) 2016-07-24 13:49 64人阅读 评论(0) 收藏
Red and Black Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total ...
- poj-1979 red and black(搜索)
Time limit1000 ms Memory limit30000 kB There is a rectangular room, covered with square tiles. Each ...
- Red and Black---hdu1312(dfs)
2015-04-07http://acm.hdu.edu.cn/showproblem.php?pid=1312 Sample Input 6 9....#......#............... ...
- POJ 1979 Red and Black (DFS)
Description There is a rectangular room, covered with square tiles. Each tile is colored either red ...
- HDU1312 / POJ1979 / ZOJ2165 Red and Black(红与黑) 解题报告
题目链接:pid=1312" target="_blank">HDU1312 / POJ1979 / ZOJ2165 Red and Black(红与黑) Red ...
- POJ 1979 Red and Black (红与黑)
POJ 1979 Red and Black (红与黑) Time Limit: 1000MS Memory Limit: 30000K Description 题目描述 There is a ...
- LeetCode Subsets II (DFS)
题意: 给一个集合,有n个可能相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: 看这个就差不多了.LEETCODE SUBSETS (DFS) class Solution { publ ...
随机推荐
- JS中常用的Math方法
1.min()和max()方法 Math.min()用于确定一组数值中的最小值.Math.max()用于确定一组数值中的最大值. alert(Math.min(2,4,3,6,3,8,0,1,3)); ...
- MySql 查询银行卡号打码
),")) end as card_number from das.staff_base; case 函数 CASE WHEN 条件1 THEN 结果1 WHEN 条件2 THE ...
- x264_param_default分析
{ /* 开辟内存空间*/ memset( param, 0, sizeof( x264_param_t ) ); /* CPU自动检测 */ par ...
- Git-简单的利用SourceTree提交代码(转载)
http://www.cnblogs.com/shenyangxiaohuo/p/5239723.html
- AWS事故总结,几招教你规避风险
版权声明:本文由王煜奕原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/90687001488360802 来源:腾云阁 ht ...
- MySQL 5.7新特性之generated column
MySQL 5.7引入了generated column,这篇文章简单地介绍了generated column的使用方法和注意事项,为读者了解MySQL 5.7提供一个快速的.完整的教程.这篇文章围绕 ...
- js 读秒
<input type="button" value=" 获取验证码 " class="verification right" &qu ...
- web 接口测试入门
在此之前先简单的介绍一下基本概念:我们想要打开一个网站,首先是需要往浏览器的地址的URL输入框架中输入网地址.当我敲下回车后,通过HTTP协议,将网址传送到域名解析服务器,域名解析服务器根据网址找到对 ...
- PHP中递归的实现(附例子)
递归函数是一种调用自己的函数.写递归函数时要小心,因为可能会无穷递归下去.必须确保有充分的方法来终止递归. 一:使用参数引用完成递归函数.操作的是同一块内存地址. <?php $i=1; fun ...
- 22.1 、react生命周期(一)
在每个react组件中都有以下几个生命周期方法~我们需要在不同阶段进行讨论 组件生命周期概述 1.初始化 在组件初始化阶段会执行 constructor static getDerivedStateF ...