A - Red and Black

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.

InputThe 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)
OutputFor 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

题目大意:“#”相当于不能走的陷阱或墙壁,“.”是可以走的路。从@点出发,统计所能到达的地点总数

 //并查集解决
#include<iostream>
using namespace std; const int h = ;
char map[h][h];
int key[h*h];
int rrank[h*h];
int n,m,dx,dy; int find(int a){
return a==key[a]? a : key[a]=find(key[a]);
} void key_union(int a,int c){
int fa = find(a);
int fc = find(c);
if(rrank[fa]>rrank[fc])
key[fc] = fa;
else{
key[fa] = fc;
if(rrank[fa]==rrank[fc])
rrank[fc]++;
}
} int num(int a){
int k = find(a);
int ans = ;
for(int i=;i<=m;i++)
for(int j=;j<=n;j++)
if(find(i*n+j)==k)
ans++; return ans;
} int main()
{
while(scanf("%d %d",&n,&m)!=EOF){
if(n==&&m==) break;
for(int i=;i<=m;i++){
cin.get();
for(int j=;j<=n;j++){
scanf("%c",&map[i][j]);
if(map[i][j]!='#') key[i*n+j] = i*n+j;
else key[i*n+j] = ;
if(map[i][j]=='@'){//找到@的坐标
dx = i;
dy = j;
map[i][j] = '.';
}
}
} for(int i=;i<m;i++){
for(int j=;j<n;j++){
if(key[i*n+j]){
if(key[i*n+j+])
key_union(i*n+j,i*n+j+);
if(key[i*n+n+j])
key_union(i*n+n+j,i*n+j);
}
}
if(key[i*n+n])
if(key[i*n+*n])
key_union(i*n+*n,i*n+n);
}
for(int i=;i<n;i++)
if(key[m*n+i])
if(key[m*n+i+])
key_union(m*n+i,m*n+i+); int ans = num(dx*n+dy);
printf("%d\n",ans);
}
}

 //DFS解决
#include<iostream>
using namespace std; int mov[][] = {-,,,,,-,,};
int sum,w,h;
char s[][]; void dfs(int x,int y){
sum++;//计数
s[x][y] = '#';
for(int i=;i<;++i){//四个方向前进
int tx = x+mov[i][];
int ty = y+mov[i][]; if(s[tx][ty]=='.' && tx>= && tx<h && ty>= && ty<w)
dfs(tx,ty);//判断该点可行后进入dfs
}
} int main()
{
int x,y;
while(scanf("%d %d",&w,&h)!=EOF){
if(w==&&h==) break;
for(int i=;i<h;i++){
cin.get();
for(int j=;j<w;j++){
scanf("%c",&s[i][j]);
if(s[i][j]=='@'){//起点
x = i;
y = j;
}
}
}
sum = ;
dfs(x,y);
printf("%d\n",sum);
}
return ;
}
 //BFS解决
#include<bits/stdc++.h>
using namespace std; char room[][];
int dir[][] = { //左上角的坐标是(0,0)
{-, }, //向左
{, -}, //向上
{, }, //向右
{, -} //向下
}; int Wx, Hy, num;
#define check(x, y)(x<Wx && x>=0 && y>=0 && y<Hy) //是否在room中
struct node{int x, y}; void BFS(int dx, int dy){
num = ;
queue<node> q;
node start, next;
start.x = dx;
start.y = dy;
q.push(start);//插入队列 while(!q.empty()){//直到队列为空
start = q.front();//取队首元素,即此轮循环的出发点
q.pop();//删除队首元素(以取出) for(int i=; i<; i++){//往左上右下四个方向逐一搜索
next.x = start.x + dir[i][];
next.y = start.y + dir[i][];
if(check(next.x, next.y) && room[next.x][next.y]=='.'){
room[next.x][next.y] = '#';//标记已经走过
num ++;//计数
q.push(next);//判断此点可行之后,插入队列,待循环判断
}
}
}
} int main(){
int x, y, dx, dy;
while(~scanf("%d %d",&Wx, &Hy)){
if(Wx== && Hy==)
break;
for(y=; y<Hy; y++){
for(x=; x<Wx; x++){
scanf("%d",&room[x][y]);
if(room[x][y] == '@'){//找到起点坐标
dx = x;
dy = y;
}
}
}
num = ;//初始化
BFS(dx, dy);
printf("%d\n",num);
}
return ;
}

												

DFS/BFS-A - Red and Black的更多相关文章

  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的连连看状态,然后问你最后能不能全部消没? 思路:      首先要明确这是一个搜索题目,还有就是关键的一点就是连连看这个游戏是 ...

  10. Red and Black(BFS or DFS) 分类: dfs bfs 2015-07-05 22:52 2人阅读 评论(0) 收藏

    Description There is a rectangular room, covered with square tiles. Each tile is colored either red ...

随机推荐

  1. C#实现读取IPv6 UDP Socket数据,再发送出去

    C#实现读取IPv6 UDP Socket数据,再发送出去. 不知为何,黑框点一下就停止刷新了,再点一下,就继续刷新了. using System; using System.Collections. ...

  2. Unity5.5.6 升级到 2018.4.1 打包出现的问题 : Gradle version 2.10 is required. Current version is 5.1.1

    起因:最近要在googleplay上架新游戏,而谷歌要求新上架的应用要支持64位,鉴于老版本的unity不支持打包64位apk,所以决定升级unity版本到2018.4.1, 但打包过程中出现了几个问 ...

  3. Objective-C编程 — 并行编程

    多线程 线程的基本概念 线程 (thread)是进程(process)A 内假想的持有 CPU 使用权的执行单位.一般情况下,一个进程 只有一个线程,但也可以创建多个线程并在进程中并行执行.应用在执行 ...

  4. Linux运维--12.手动部署Rabbit集群

    1.安装rabbit组件 10.100.2.51 controller1 10.100.2.52 controller2 10.100.2.53 controller3 #每个节点 yum insta ...

  5. 会话技术中的Cookie与session

    关于会话技术 会话:一次会话中包含多次请求和响应. 一次会话:浏览器第一次给服务器资源发送请求,会话建立,直到有一方断开为止 功能:在一次会话的范围内的多次请求间,共享数据 方式: 客户端会话技术:C ...

  6. MySQL存储过程和游标

    一.存储过程 什么是存储过程,为什么要使用存储过程以及如何使用存储过程,并且介绍创建和使用存储过程的基本语法. 什么是存储过程: 存储过程可以说是一个记录集,它是由一些T-SQL语句组成的代码块,这些 ...

  7. [VB.NET Tips]创建匿名类型列表

    在调用一些Web API时经常要发送或接收一些数据,在构造Json时可能要创建一些类. 很多都是在调用相关方法才使用到这些类,那使用匿名类型是个不错的选择.如果要传些表结构数据时,就要创建List. ...

  8. Java虚拟机——JVM

    一.JVM整体架构 1.JVM(Java虚拟机):指以软件的方式模拟具有完整硬件系统功能.运行在一个完全隔离环境中的完整计算机系统,是物理机的软件实现.常用的虚拟机有VMWare.Virtual Bo ...

  9. JS 重载父页面

    <script language=javascript> window.onload=function(){ //刷新父页面 window.opener.location.reload() ...

  10. 处理方法返回值void

    1.默认响应效果:根据请求url寻找相应页面 1.1.配置的视图解析器 <!--配置视图解析器--> <bean id="internalResourceViewResol ...