dfs 队列
题目来源 poj 1562
Description
Input
Output
Sample Input
1 1
*
3 5
*@*@*
**@**
*@*@*
1 8
@@****@*
5 5
****@
*@@*@
*@**@
@@@*@
@@**@
0 0
Sample Output
0
1
2
2 ac代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
#define Swap(a,b,t) t=a,a=b,b=t
#define Mem0(x) memset(x,0,sizeof(x))
#define Mem1(x) memset(x,-1,sizeof(x))
#define MemX(x) memset(x,0x3f,sizeof(x));
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f;
const double eps=1e-12;
struct s{
int x,y;
char c;
}map[110][110];
const int dir[8][2]={{1,0},{1,-1},{1,1},{0,1},{0,-1},{-1,-1},{-1,0},{-1,1}};
queue<s> q;
int n,m,ans;
void dfs(int x,int y)
{
s p,next;
while (!q.empty()){
p=q.front();
q.pop();
for (int k=0;k<8;k++){
next.x=p.x+dir[k][0];
next.y=p.y+dir[k][1];
if (map[next.x][next.y].c=='@'){
q.push(map[next.x][next.y]);
map[next.x][next.y].c='*';
}
}
}
return ;
}
int main()
{
while (cin>>n>>m&&n&&m){
ans=0;
while (!q.empty())
q.pop();
for (int i=1;i<=n;i++){
for (int j=1;j<=m;j++){
cin>>map[i][j].c;
if (map[i][j].c=='@'){
map[i][j].x=i;
map[i][j].y=j;
}
}
}
for (int i=1;i<=n;i++){
for (int j=1;j<=m;j++){
if (map[i][j].c=='@'){
q.push(map[i][j]);
map[i][j].c='*';
ans++;
dfs(i,j);
}
}
}
printf("%d\n",ans);
}
return 0;
}
Red and Black
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 26317 Accepted Submission(s): 15909
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)
....#.
.....#
......
......
......
......
......
#@...#
.#..#.
11 9
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
###.###
...@...
###.###
..#.#..
..#.#..
0 0
ac代码:
#include<iostream> //hdu 1312
#include<cstdio>
#include<cstring>
#include<string>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
#define Swap(a,b,t) t=a,a=b,b=t
#define Mem0(x) memset(x,0,sizeof(x))
#define Mem1(x) memset(x,-1,sizeof(x))
#define MemX(x) memset(x,0x3f,sizeof(x));
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f;
const double eps=1e-12;
int n,m,ans;
char temp[25];
const int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
struct s{
int x,y;
char c;
}map[25][25];
queue <s> q;
void dfs(int x,int y)
{
s p,next;
while (!q.empty()){
p=q.front();
q.pop();
for (int k=0;k<4;k++){
next.x=p.x+dir[k][0];
next.y=p.y+dir[k][1];
if (map[next.x][next.y].c=='.'&&next.x<=n&&next.y<=m){
ans++;
q.push(map[next.x][next.y]);
map[next.x][next.y].c='#';
}
}
}
return ;
}
int main()
{
while (cin>>m>>n&&n&&m){
while (!q.empty())
q.pop();
int start_x,start_y;
ans=1;
for(int i=1;i<=n;i++){
for (int j=1;j<=m;j++){
cin>>map[i][j].c;
if (map[i][j].c=='.'){
map[i][j].x=i;
map[i][j].y=j;
}
else if (map[i][j].c=='@'){
map[i][j].x=start_x=i;
map[i][j].y=start_y=j;
q.push(map[i][j]);
}
}
}
dfs(start_x,start_y);
printf("%d\n",ans);
}
return 0;
}
dfs 队列的更多相关文章
- poj 3321
题目链接 题意:一开始1-n都有苹果,Q查询以x为根下存在多少. 树状数组+DFS+队列转换 这题纠结了2天,一开始一点思路都没有,看大神都是吧树状数组转换成队列来做 看了好久都不知道怎么转换的, 解 ...
- 最短路 spfa 算法 && 链式前向星存图
推荐博客 https://i.cnblogs.com/EditPosts.aspx?opt=1 http://blog.csdn.net/mcdonnell_douglas/article/deta ...
- hdu4135容斥原理 组合遍历
容斥原理实现的关键在于:组合遍历,即如何遍历2^n种组合. 容斥原理的三种写法: DFS 队列数组 位数组 #include<stdio.h> #include<iostream&g ...
- POJ 2227 The Wedding Juicer (优先级队列+bfs+dfs)
思路描述来自:http://hi.baidu.com/perfectcai_/item/701f2efa460cedcb0dd1c820也可以参考黑书P89的积水. 题意:Farmer John有一个 ...
- Uva 10305 - Ordering Tasks 拓扑排序基础水题 队列和dfs实现
今天刚学的拓扑排序,大概搞懂后发现这题是赤裸裸的水题. 于是按自己想法敲了一遍,用queue做的,也就是Kahn算法,复杂度o(V+E),调完交上去,WA了... 于是检查了一遍又交了一发,还是WA. ...
- 对比dfs与bfs的存储机制以及bfs与队列的关系
dfs由于是利用递归进行遍历,所以每种情况在时空上不会出现冲突,所以可以利用数组将每种情况的各个元素的值进行存储(即存储当前位) 而bfs由于并不是利用递归,不能将每种情况的值进行不冲突地存储,但由于 ...
- 【BZOJ2783】[JLOI2012]树 DFS+栈+队列
[BZOJ2783][JLOI2012]树 Description 在这个问题中,给定一个值S和一棵树.在树的每个节点有一个正整数,问有多少条路径的节点总和达到S.路径中节点的深度必须是升序的.假设节 ...
- DFS、栈、双向队列:CF264A- Escape from Stones
题目: Squirrel Liss liv Escape from Stonesed in a forest peacefully, but unexpected trouble happens. S ...
- 51nod1153(dfs/单调队列)
题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1153 题意:中文题诶- 思路:一个比较简单的方法是dfs隐式 ...
随机推荐
- CMake是用于生成make文件的跨平台编译文件
参考: https://www.cnblogs.com/hbccdf/p/introduction_of_cmake.html https://elloop.github.io/tools/2016- ...
- 自动备份软件 —— Syncovery 7.98s Pro/Enterprise
SynCovery自动备份软件原名Super Flexible Synchronizer,是目前功能最为强大的实时自动备份工具,连FTP.WebDAV等全部支持!最近从V6开始改用比较好记.易懂的新名 ...
- NET平台微服务
.NET平台微服务项目汇集 最近博客园出现了一篇文章<微服务时代之2017年五军之战:Net PHP谁先死>,掀起了一波撕逼,作者只是从一个使用者的角度来指点江山,这个姿势是不对的.. ...
- Jenkins报错Caused: java.io.IOException: Cannot run program "sh" (in directory "D:\Jenkins\Jenkins_home\workspace\jmeter_test"): CreateProcess error=2, 系统找不到指定的文件。
想在本地执行我的python文件,我本地搭建了一个Jenkins,使用了execute shell来运行我的脚本,发现报错 [jmeter_test] $ sh -xe D:\tomcat\apach ...
- Django 模型中DateField字段
DateField¶ class DateField([auto_now=False, auto_now_add=False, **options])¶ 这是一个使用Python的datetime.d ...
- 汇编试验十四:访问CMOS RAM
CMOS RAM 芯片的特征: 包含一个时钟和一个有128个存储单元的RAM存储器. 该芯片靠电池供电.所以,关机后其内部的时钟仍可正常工作,RAM中的信息不丢失. 128个字节的RAM中,内部时钟占 ...
- 4springboot:日志(下)
1.指定配置 位置: 给类路径下放上每个日志框架自己的配置文件即可: SpringBoot就不使用他默认配置的了 使用什么日志则配置什么文件以及注意文件名 自定义: <?xml version ...
- 【洛谷P2279】[HNOI2003]消防局的设立
消防局的设立 题目链接 贪心:每次取出深度最大的节点,若没有被覆盖到,要想覆盖它, 最优的做法显然是将它的爷爷设为消防局 (因为该节点深度为最大,选兄弟.父亲所覆盖的节点,选了爷爷后都能够覆盖) 用优 ...
- New Language Features in C# 6
Source:https://github.com/dotnet/roslyn/wiki/New-Language-Features-in-C%23-6 This document describes ...
- mysql update 子查询锁表问题
mysql在Update带有子查询的时候,子查询的表会锁住,导致该表无法使用.比如 update A set comments = (select count(1) from B where id = ...