poj1979【基础bfs/dfs】
挑战习题搜索-1
题意:
给定起点,然后求一个可以到达的数量,位置“.”都可以走。每次应该是上下左右都可以走。
思路:
这题应该DFS更好写,但是BFS也可以写吧。
好久没写了…
dfs挫代码……..
#include<cstdio>
#include<iostream>
#include<math.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define eps 1e-8
typedef __int64 LL;
const int N=25;
int n,m;
char ma[N][N];
int dx[4]={0,0,-1,1};
int dy[4]={1,-1,0,0};
int ans;
bool Judge(int x,int y)
{
if(x<0||y<0||x>=n||y>=m||ma[x][y]=='#')
return 0;
return 1;
}
void dfs(int x,int y)
{
ma[x][y]='#';//到了就标记,避免重复;
ans++; //计数++;
for(int i=0;i<4;i++)
{
int xx=x+dx[i];
int yy=y+dy[i];
if(Judge(xx,yy))//如果符合就继续搜索;
dfs(xx,yy);
}
}
int main()
{
while(~scanf("%d%d",&m,&n))
{
if(!n&&!m) break;
int x,y;
for(int i=0;i<n;i++)
{
scanf("%s",ma[i]);
for(int j=0;j<m;j++)
{
if(ma[i][j]=='@')//起点;
{
x=i;
y=j;
}
}
}
ans=0;
dfs(x,y);//从起点开始搜索;
printf("%d\n",ans);
}
return 0;
}
bfs挫code………..
//利用bfs将可以到达的点塞进队列并标记,当出队列的时间就计数。
#include<cstdio>
#include<iostream>
#include<queue>
#include<math.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define eps 1e-8
typedef __int64 LL;
const int N=25;
struct asd{
int x,y;
};
asd q[N*N];
int n,m;
char ma[N][N];
int dx[4]={0,0,-1,1};
int dy[4]={1,-1,0,0};
int ans;
bool Judge(int x,int y)
{
if(x<0||y<0||x>=n||y>=m||ma[x][y]=='#')
return 0;
return 1;
}
queue<asd>que; //建议还是要多写手写队列
void bfs(int x,int y)
{
while(!que.empty())
que.pop();
asd now,next;
now.x=x;
now.y=y;
ma[x][y]='#'; //标记
que.push(now);
while(!que.empty())
{
asd now;
now=que.front();que.pop(); //出队列
ans++;
for(int i=0;i<4;i++)
{
int xx=now.x+dx[i];
int yy=now.y+dy[i];
if(Judge(xx,yy))
{
next.x=xx;
next.y=yy;
ma[next.x][next.y]='#'; //标记
que.push(next);
}
}
}
}
int main()
{
while(~scanf("%d%d",&m,&n))
{
if(!n&&!m) break;
int x,y;
for(int i=0;i<n;i++)
{
scanf("%s",ma[i]);
for(int j=0;j<m;j++)
{
if(ma[i][j]=='@')
{
x=i;
y=j;
}
}
}
ans=0;
bfs(x,y);
printf("%d\n",ans);
}
return 0;
}
poj1979【基础bfs/dfs】的更多相关文章
- 基础BFS+DFS poj3083
//满基础的一道题 //最短路径肯定是BFS. //然后靠右,靠左,就DFS啦 //根据前一个状态推出下一个状态,举靠左的例子,如果一开始是上的话,那么他的接下来依次就是 左,上 , 右 , 下 // ...
- POJ 2227 The Wedding Juicer (优先级队列+bfs+dfs)
思路描述来自:http://hi.baidu.com/perfectcai_/item/701f2efa460cedcb0dd1c820也可以参考黑书P89的积水. 题意:Farmer John有一个 ...
- 邻结矩阵的建立和 BFS,DFS;;
邻结矩阵比较简单,, 它的BFS,DFS, 两种遍历也比较简单,一个用队列, 一个用数组即可!!!但是邻接矩阵极其浪费空间,尤其是当它是一个稀疏矩阵的时候!!!-------------------- ...
- Collect More Jewels(hdu1044)(BFS+DFS)
Collect More Jewels Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- Cleaning Robot (bfs+dfs)
Cleaning Robot (bfs+dfs) Here, we want to solve path planning for a mobile robot cleaning a rectangu ...
- LeetCode:BFS/DFS
BFS/DFS 在树专题和回溯算法中其实已经涉及到了BFS和DFS算法,这里单独提出再进一步学习一下 BFS 广度优先遍历 Breadth-First-Search 这部分的内容也主要是学习了labu ...
- 数据结构基础(21) --DFS与BFS
DFS 从图中某个顶点V0 出发,访问此顶点,然后依次从V0的各个未被访问的邻接点出发深度优先搜索遍历图,直至图中所有和V0有路径相通的顶点都被访问到(使用堆栈). //使用邻接矩阵存储的无向图的深度 ...
- Leetcode题目200.岛屿数量(BFS+DFS+并查集-中等)
题目描述: 给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计算岛屿的数量.一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的.你可以假设网格的四个边均被水包围. 示例 ...
- 图的基本遍历算法的实现(BFS & DFS)复习
#include <stdio.h> #define INF 32767 typedef struct MGraph{ ]; ][]; int ver_num, edge_num; }MG ...
随机推荐
- window7_64安装STAF
1. 安装包下载 从http://sourceforge.net/projects/staf/files/staf/V3.4.17/下载所需安装包,有Windows.Linux.Solar ...
- Java 递归解决 "仅仅能两数相乘的计算器计算x^y" 问题
/** * 求一个数的乘方 * 求x^y,y是一个正整数. 设计算器仅仅能计算两数相乘,不能一次计算n个数相乘. * 知:2^5=(2^2)^2*2; 2^6=(2^2)^3=((4)^2)*4; 2 ...
- SQL模糊查询碰到空值怎么办?
作者:iamlaosong SQL查询语句用%来做模糊查询.程序中一般要求用户输入部分信息,依据这个信息进行模糊查询. 比如用户输入340104,以下这条语句就是查询昨天客户代码为340104开头的全 ...
- BZOJ 3732 Network 最小瓶颈路
题目大意:给出一个无向边,非常多询问,问x,y两地之间的最长路最短是多少. 思路:乍一看好像是二分啊. 的确这个题二分能够做.可是时间会慢非常多,有的题直接就T掉(NOIP2013货车运输). 事实上 ...
- 线程、SMP、微内核
- attribute constructor&destructor
attribute constructor&destructor 在看openwrt里libnl-tiny这个库的时候,遇到了C里面的构造函数这个概念. static void __init ...
- const *char p和char const *p
const *char p和char const *p,const char*p的区别 char*const p——p必须初始化,且不能指向别处,即p是指针常量: char const*p——p指向的 ...
- Thread Runnable 区别
[线程的并发与并行] 在单CPU系统中,系统调度在某一时刻只能让一个线程运行,虽然这种调试机制有多种形式(大多数是时间片轮巡为主),但无论如何,要通过不断切换需要运行的线程让其运行的方式就叫并发(co ...
- compile java sources
Information:javac 1.8.0_91 was used to compile java sources D:\myjdk\bin\java "-javaagent:C:\Pr ...
- spring-jar包详解整理(大合集)
转:https://blog.csdn.net/weisong530624687/article/details/50888094 spring.jar 是包含有完整发布模块的单个jar 包.但是不包 ...