HDOJ 1312 DFS&BFS
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
Write a program to count the number of black tiles which he can reach by repeating the moves described above.
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)
....#.
.....#
......
......
......
......
......
#@...#
.#..#.
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#.......#.
.#########.
...........
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
..#.#..
..#.#..
###.###
...@...
###.###
..#.#..
..#.#..
//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的更多相关文章
- DFS/BFS+思维 HDOJ 5325 Crazy Bobo
题目传送门 /* 题意:给一个树,节点上有权值,问最多能找出多少个点满足在树上是连通的并且按照权值排序后相邻的点 在树上的路径权值都小于这两个点 DFS/BFS+思维:按照权值的大小,从小的到大的连有 ...
- 【DFS/BFS】NYOJ-58-最少步数(迷宫最短路径问题)
[题目链接:NYOJ-58] 经典的搜索问题,想必这题用广搜的会比较多,所以我首先使的也是广搜,但其实深搜同样也是可以的. 不考虑剪枝的话,两种方法实践消耗相同,但是深搜相比广搜内存低一点. 我想,因 ...
- ID(dfs+bfs)-hdu-4127-Flood-it!
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4127 题目意思: 给n*n的方格,每个格子有一种颜色(0~5),每次可以选择一种颜色,使得和左上角相 ...
- [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 ...
- HDU 4771 (DFS+BFS)
Problem Description Harry Potter has some precious. For example, his invisible robe, his wand and hi ...
- DFS/BFS视频讲解
视频链接:https://www.bilibili.com/video/av12019553?share_medium=android&share_source=qq&bbid=XZ7 ...
- POJ 3083 -- Children of the Candy Corn(DFS+BFS)TLE
POJ 3083 -- Children of the Candy Corn(DFS+BFS) 题意: 给定一个迷宫,S是起点,E是终点,#是墙不可走,.可以走 1)先输出左转优先时,从S到E的步数 ...
- [LeetCode]695. 岛屿的最大面积(DFS/BFS)、200. 岛屿数量(DFS/BFS待做/并差集待做)
695. 岛屿的最大面积 题目 给定一个包含了一些 0 和 1的非空二维数组 grid , 一个 岛屿 是由四个方向 (水平或垂直) 的 1 (代表土地) 构成的组合.你可以假设二维矩阵的四个边缘都被 ...
- POJ2308连连看dfs+bfs+优化
DFS+BFS+MAP+剪枝 题意: 就是给你一个10*10的连连看状态,然后问你最后能不能全部消没? 思路: 首先要明确这是一个搜索题目,还有就是关键的一点就是连连看这个游戏是 ...
随机推荐
- C#配置文件管理
最近在做项目的过程中用到配置文件,本文简要说明本人在项目过程中是如何使用配置文件的,目的是加深自己对配置文件管理的理解,以便在下次使用时能做到轻松自如. 配置文件,顾名思义,是用户在使用系统或者软件时 ...
- 解决 pathForResource 返回 nil的问题
点击(此处)折叠或打开 NSString* path = [[NSBundle mainBundle] pathForResource:@"sample" ofType:@&quo ...
- Struts2:java.lang.NoSuchFieldException: resourceEntries at java.lang.Class.getDeclaredField(Class.java:1901)
今天在做Struts2的测试用例时候,程序能正常跳转,但是在Console却报了一个错误,如下: java.lang.NoSuchFieldException: resourceEntries at ...
- Oracle创建主外键
-创建表格语法: create table 表名( 字段名1 字段类型(长度) 是否为空, 字段名2 字段类型 是否为空); -增加主键 alt ...
- 洛谷P2320 [HNOI2006]鬼谷子的钱袋
https://www.luogu.org/problem/show?pid=2320#sub 题目描述全是图 数学思维,分治思想 假设总数为n 从n/2+1到n的数都可以用1~n的数+n/2表示出来 ...
- Linux中断技术、门描述符、IDT(中断描述符表)、异常控制技术总结归类
相关学习资料 <深入理解计算机系统(原书第2版)>.pdf http://zh.wikipedia.org/zh/%E4%B8%AD%E6%96%B7 独辟蹊径品内核:Linux内核源代码 ...
- Opencv显示图片并监听鼠标坐标
#include <opencv2/imgproc/imgproc.hpp> #include <opencv2/highgui/highgui.hpp> #include & ...
- 关于Asp.Net Mvc3.0 使用KindEditor4.0 上传图片与文件
http://blog.csdn.net/fyxq14hao/article/details/7245502 今天我们的Asp.Net Mvc 3的项目中,把KindEditor3.9改为 KindE ...
- Android事件机制之一:事件传递和消费
http://www.cnblogs.com/lwbqqyumidi/p/3500997.html 关于Android中的事件机制,用到的地方还是很多的,并且这个知识点还真有点复杂. 在写这篇文章前, ...
- ExtJS入门教程01,Window如此简单,你怎能不会?
这是一系列ExtJS教程,今天的是第一篇,主要介绍ExtJS中Window的基本用法.希望大家能够支持! 来吧,创建一个漂亮的弹出窗 var win = Ext.create("Ext.Wi ...