HDU 1312:Red and Black

Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u

 

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
题解:本题还是DFS搜索(上下左右),只是增加一个计数器,计算可以走的地方的个数。
      规定地图中有可通行的位置,也有不可通行的位置,已知起点,求起点的与它相连成一片的部分,在这道题里输出相连的位置的数目。
    从起点开始,遍历每一个到达的点的四个方向(不再是八个),到达一个位置就将这个位置的字符变成不可走的'#',并且计数+1。其实就是计数将可走变成不可走的操作进行了多少次。
这样可以不用担心走过了还会重复。
 
 AC
代码:如果你看过我的上一篇你一定会懂
#include<cstdio>
#include<cstring>
char pic[][];
int m,n,total;
int idx[][]; void dfs(int r,int c,int id)
{
if(r<||r>=m||c<||c>=n)
return;
if(idx[r][c]==||pic[r][c]!='.')
return;
idx[r][c]=id;
total++;
for(int dr=-; dr<=; dr++)
for(int dc=-; dc<=; dc++)
if(dr==||dc==)
dfs(r+dr,c+dc,id);
}
int main()
{
int i,j;
while(scanf("%d%d",&n,&m)==&&m&&n)
{
for(i =; i<m; i++)
scanf("%s",pic[i]);
memset(idx,,sizeof(idx));
total=;
for(i=; i<m; i++)
for(j=; j<n; j++)
{
if(pic[i][j]=='@')
{
pic[i][j]='.';
dfs(i,j,);
}
}
printf("%d\n",total);
}
return ;
}
 
 
 
这个是我在其他博客上看得到的方法,用#填充,可以一试!
 
#include <iostream>
using namespace std;
char a[][];
int n,m,total;
int dr[] = {,,,-};//行变化
int dc[] = {,,-,};//列变化
//上面的原来一直不会用,知道的话非常方便
bool judge(int x,int y)
{
if(x< || x>n || y< || y>m)
return ;
if(a[x][y]=='#')
return ;
return ;
}
void dfs(int r,int c)
{
total++;
a[r][c]='#'; //走过一次,“。”变为“#”,避免重复
for(int k=; k<; k++)
{
int lr = r + dr[k];
int lc = c + dc[k];
if(judge(lr,lc))
continue;
dfs(lr,lc);
} }
int main()
{
while(cin>>m>>n&&m&&n)
{
int i,j,x,y;
total=;
for(i=; i<=n; i++)
for(j=; j<=m; j++)
{
cin>>a[i][j];
if(a[i][j]=='@') //这里必须用变量x,y
x=i,y=j; }
dfs(x,y);
cout<<total<<endl;
}
return ;
}
 

HDU 1312:Red and Black(DFS搜索)的更多相关文章

  1. HDU 1312 Red and Black --- 入门搜索 DFS解法

    HDU 1312 题目大意: 一个地图里面有三种元素,分别为"@",".","#",其中@为人的起始位置,"#"可以想象 ...

  2. HDU 1312 Red and Black --- 入门搜索 BFS解法

    HDU 1312 题目大意: 一个地图里面有三种元素,分别为"@",".","#",其中@为人的起始位置,"#"可以想象 ...

  3. HDU 1312 Red and Black(DFS,板子题,详解,零基础教你代码实现DFS)

    Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  4. HDU 1312 Red and Black DFS(深度优先搜索) 和 BFS(广度优先搜索)

    Red and Black Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...

  5. HDU 1312 Red and Black (DFS & BFS)

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312 题目大意:有一间矩形房屋,地上铺了红.黑两种颜色的方形瓷砖.你站在其中一块黑色的瓷砖上,只能向相 ...

  6. HDU 1312 Red and Black (DFS)

    Problem Description There is a rectangular room, covered with square tiles. Each tile is colored eit ...

  7. hdu 1312:Red and Black(DFS搜索,入门题)

    Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  8. 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 ...

  9. HDU 1312 Red and Black(最简单也是最经典的搜索)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1312 Red and Black Time Limit: 2000/1000 MS (Java/Oth ...

随机推荐

  1. spj 设计

    在数据库中使用SELECT INTO 语句从SP.J.P表中创建一个新表J_P_SPJ新表J_P_QTY中的属性列包括:工程号 (JNO).工程名(JNAME).零件号(PNO).零件名(PNAME) ...

  2. 环境搭建之maven分布式系统

    shoppingmall项目: 一.父工程  shoppingmall-parent  (packaging为pom) 此是本项目的顶级工程,所有子工程都应该继承他,主要可承担以下任务: a.集中定义 ...

  3. 9个Java初始化和回收的面试题

    1.Java中是如何区分重载方法的? 通过重载方法的参数类型和顺序来进行区分的. 注意:若参数类型和顺序均相同时,不管参数名是否相同,编译器均会报错,提示方法已经被定义.且不能根据返回值类型来区分,如 ...

  4. grep 查找当前文件夹下所有文件内内容 并显示文件名

    grep -H comm * 显示结果 rclog:recommend/ysclick,1,2 rclog:recommend/utagclick,1,8 .................... t ...

  5. ubuntu忘记密码

    Do these two things just to make sure: mount -o remount,rw / This first part remounts the root parti ...

  6. nginx 代理概念理解

    学习nginx http://www.nginx.cn/nginx-how-to nginx官方定义: Nginx 是一个高性能的 Web 和反向代理服务器. 首先明确一个概念 反向代理 反向代理(R ...

  7. EF 5.0 和 EF4.0 语法区别

    // 实现对数据库的添加功能,添加实现EF框架的引用 40 41 public T AddEntity(T entity) 42 43 { 44 45 //EF4.0的写法 添加实体 46 47 // ...

  8. ORACLE功能GREATEST功能说明具体实例

    1           语法 GREATEST(expr_1, expr_2, ...expr_n) 2           说明 GREATEST(expr_1, expr_2, ...expr_n ...

  9. [转] weak_ptr解决shared_ptr环状引用所引起的内存泄漏

    http://blog.csdn.net/liuzhi1218/article/details/6993135 循环引用: 引用计数是一种便利的内存管理机制,但它有一个很大的缺点,那就是不能管理循环引 ...

  10. 多线程(NSThread、NSOperation、GCD)编程浅谈

    一.基本概念 进程:一个具有一定独立功能的程序关于某个数据集合的一次运行活动.可以理解成一个运行中的应用程序.线程:程序执行流的最小单元,线程是进程中的一个实体.同步:只能在当前线程按先后顺序依次执行 ...