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. 学习Sparql

    一 . gstore--一种开源图数据库系统 https://www.docin.com/p-1951514687.html 二 . 使用 SPARQL 查询 RDF 数据 https://www.i ...

  2. Jedis客户端即redis中的pipeline批量操作

    关注公众号:CoderBuff,回复"redis"获取<Redis5.x入门教程>完整版PDF. <Redis5.x入门教程>目录 第一章 · 准备工作 第 ...

  3. WinBox软路由的自定义IP设定

             软路由ros(MIKRTIK)安装简单步骤 由于本主也是刚刚接触软路由这个硬件方面的知识.所以也是略知皮毛而已,今天通过网上的学习,然后自己总结了一下怎么在软路由中设定一个自定义IP ...

  4. JS循环解决任意日期间的间隔天数

    用JS循环解决任意日期间的间隔天数,并求截止日期是周几 y1=1900 m1=1 d1=1 y2=2000 m2=5 d2=3 days=0 ydays=0 mdays=0 ddays=d2-d1 f ...

  5. Deepin下将Caps映射为Control_L键

    xmodmap -e 'clear Lock' -e 'keycode 0x42 = Control_L'

  6. 关于软件 TELEGRAM(电报) 的说明

    PLUS是TELEGRAM(电报)的三方客户端. 而 电报 是一款即时通讯软件.功能全面,安全性好,但在国内无法直接访问. 相关安全性介绍请看这里:https://www.anquanke.com/p ...

  7. 开源工作流管理系统节点接收人设置“指定节点处理人”系列讲解

    关键字: 驰骋工作流程快速开发平台 工作流程管理系统 工作流引擎 asp.net工作流引擎 java工作流引擎. 开发者表单  拖拽式表单 工作流系统CCBPM节点访问规则接收人规则 适配数据库: o ...

  8. java工作流系统表单自动 获取数据

    关键词:工作流快速开发平台  工作流流设计  业务流程管理   asp.net 开源工作流  bpm工作流系统  java工作流主流框架  自定义工作流引擎 表单设计器  流程设计器 什么是数据自动获 ...

  9. 安卓接入融云IM连接不上??

    融云初始化失败?融云连接失败??连接回调方法不执行???不可能的,就那么几行代码,怎么会有错. 1.项目gradle里添加融云maven仓库 maven {url "https://dl.b ...

  10. AndroidStudio报错:GradleSyncIssues-Could not install Gradle distribution from...

    场景 在使用Android Studio 新建 App项目后,编译时提示: GradleSyncIssues-Could not install Gradle distribution from... ...