Red and Black

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7337    Accepted Submission(s): 4591

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
 
广度搜索,需要用到队列:
 //bfs
#include<stdio.h>
int map[][];
int queue[][];//队列
int sx[]={,,,-};
int sy[]={,,-,};
int h,w;
int ans;
void bfs(int x,int y)
{
int rear=,front=;
queue[rear][]=x;//enter queue
queue[rear][]=y;
rear++;
while(front<rear)
{
for(int t=;t<;t++)
{
x=queue[front][]+sx[t];
y=queue[front][]+sy[t];
if(map[x][y]=='.'&&x>=&&x<=w&&y>=&&y<=h)
{
queue[rear][]=x;
queue[rear][]=y;
map[x][y]='#';
rear++;
ans++;
}
}
front++;
} }
int main()
{
int i,j;
int x,y;
while(scanf("%d%d",&h,&w)!=EOF)
{
if(!h&&!w)break;
ans=;
for(i=;i<=w;i++)
{
getchar();
for(j=;j<=h;j++)
{
scanf("%c",&map[i][j]);
if(map[i][j]=='@')
x=i,y=j;
}
}
bfs(x,y);
printf("%d\n",++ans);
}
return ;
}

深度搜索:

 #include<stdio.h>
int map[][];
int t,n,m;
int sx[]={,,-,};
int sy[]={-,,,};
void dfs(int h,int l)//深度搜索
{
int i,hx,hy;
t++;
map[h][l]='@';
for(i=;i<;i++)
{
hx=h+sx[i];
hy=l+sy[i];
if(hx>=&&hx<=m&&hy>=&&hy<=n&&map[hx][hy]=='.')
dfs(hx,hy);
}
}
int main()
{
int a,b,x,y;
while(scanf("%d%d",&n,&m)!=EOF)
{
if(n==&&m==)break;
for(a=;a<=m;a++)
{
getchar();
for(b=;b<=n;b++)
{
scanf("%c",&map[a][b]);
if(map[a][b]=='@')
{
x=a;
y=b;
}
}
}
t=;
dfs(x,y);
printf("%d\n",t);
}
return ;
}
 

HDOJ 1312 DFS&BFS的更多相关文章

  1. DFS/BFS+思维 HDOJ 5325 Crazy Bobo

    题目传送门 /* 题意:给一个树,节点上有权值,问最多能找出多少个点满足在树上是连通的并且按照权值排序后相邻的点 在树上的路径权值都小于这两个点 DFS/BFS+思维:按照权值的大小,从小的到大的连有 ...

  2. 【DFS/BFS】NYOJ-58-最少步数(迷宫最短路径问题)

    [题目链接:NYOJ-58] 经典的搜索问题,想必这题用广搜的会比较多,所以我首先使的也是广搜,但其实深搜同样也是可以的. 不考虑剪枝的话,两种方法实践消耗相同,但是深搜相比广搜内存低一点. 我想,因 ...

  3. ID(dfs+bfs)-hdu-4127-Flood-it!

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4127 题目意思: 给n*n的方格,每个格子有一种颜色(0~5),每次可以选择一种颜色,使得和左上角相 ...

  4. [LeetCode] 130. Surrounded Regions_Medium tag: DFS/BFS

    Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. A reg ...

  5. HDU 4771 (DFS+BFS)

    Problem Description Harry Potter has some precious. For example, his invisible robe, his wand and hi ...

  6. DFS/BFS视频讲解

    视频链接:https://www.bilibili.com/video/av12019553?share_medium=android&share_source=qq&bbid=XZ7 ...

  7. POJ 3083 -- Children of the Candy Corn(DFS+BFS)TLE

    POJ 3083 -- Children of the Candy Corn(DFS+BFS) 题意: 给定一个迷宫,S是起点,E是终点,#是墙不可走,.可以走 1)先输出左转优先时,从S到E的步数 ...

  8. [LeetCode]695. 岛屿的最大面积(DFS/BFS)、200. 岛屿数量(DFS/BFS待做/并差集待做)

    695. 岛屿的最大面积 题目 给定一个包含了一些 0 和 1的非空二维数组 grid , 一个 岛屿 是由四个方向 (水平或垂直) 的 1 (代表土地) 构成的组合.你可以假设二维矩阵的四个边缘都被 ...

  9. POJ2308连连看dfs+bfs+优化

    DFS+BFS+MAP+剪枝 题意:       就是给你一个10*10的连连看状态,然后问你最后能不能全部消没? 思路:      首先要明确这是一个搜索题目,还有就是关键的一点就是连连看这个游戏是 ...

随机推荐

  1. iOS -- 生成有logo的二维码

    - (void)createLogoImage { NSArray *filter = [CIFilter filterNamesInCategory:kCICategoryBuiltIn]; NSL ...

  2. 【转】div居中代码 DIV水平居中显示CSS代码

    原文地址:http://www.divcss5.com/rumen/r622.shtml 如何使用CSS让DIV居中显示,让div水平居中有哪些CSS样式呢? 需要的主要css代码有两个,一个为tex ...

  3. 另一套Oracle SQL练习题,更新参考答案

    题干: create table student( sno ) primary key, sname ), sage ), ssex ) ); create table teacher( tno ) ...

  4. JSP基本原理

    JSP的基本原理: jsp的本质是servlet.jsp通过在标准的HTML页面中嵌入java代码,其静态的部分无需Java程序控制,只有那些需要从数据库读取或需要 动态生成的的页面内容,才使用Jav ...

  5. poj 1061 扩展欧几里得解同余方程(求最小非负整数解)

    题目可以转化成求关于t的同余方程的最小非负数解: x+m*t≡y+n*t (mod L) 该方程又可以转化成: k*L+(n-m)*t=x-y 利用扩展欧几里得可以解决这个问题: eg:对于方程ax+ ...

  6. Linux /proc、/dev Principle

    目录 . /proc简介 . 内核机制相关 . 进程信息 . 硬件设备相关 . 系统信息 . /dev简介 . 内存相关 1. /proc简介 在linux的根目录下有一个/proc目录,/proc文 ...

  7. Codeforces 567D One-Dimensional Battle Ships

    传送门 D. One-Dimensional Battle Ships time limit per test 1 second memory limit per test 256 megabytes ...

  8. HTTPS-能否避免流量劫持

    流量劫持是什么? EtherDream在一篇科普文章<>中详细介绍了流量劫持途径和方式. 流量劫持是一种古老的攻击方式,比如早已见惯的广告弹窗等,很多人已经对此麻木,并认为流量劫持不会造成 ...

  9. pthread 学习系列 case2-- pthread_mutex_t

    许多互斥对象 如果放置了过多的互斥对象,代码就没有什么并发性可言,运行起来也比单线程解决方案慢.如果放置了过少的互斥对象,代码将出现奇怪和令人尴尬的错误.幸运的是,有一个中间立场.首先,互斥对象是用于 ...

  10. [名词解释]Constant Amortized Time

    http://stackoverflow.com/questions/200384/constant-amortized-time 分摊常量时间: Amortised time explained i ...