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 ...
随机推荐
- struts开发<在eclipse中配置struts. 一>
1.获取struts的jar包 1.1首先在http://struts.apache.org/download.cgi#struts23163这里下载 struts的文件包(选择struts-2.3. ...
- 使用虚拟机VM运行Linux版OpenERP
Table of Contents 下载.安装vmware player --免费 从百度云下载 镜像文件 建立vm 运行vm 访问openerp 更新代码至最新 下载.安装vmwar ...
- Android手机输入法按键监听-dispatchKeyEvent
近期在项目开发中遇到一个关于手机输入键盘的坑.特来记录下. 应用场景: 项目中有一个界面是用viewpaper加三个fragment写的,当中viewpaper被我屏蔽了左右滑动,上面有三个点击按钮, ...
- 九度OJ #1437 To Fill or Not to Fil
题目描写叙述: With highways available, driving a car from Hangzhou to any other city is easy. But since th ...
- SpringBoot学习之@SpringBootApplication注解
下面是我们经常见到SpringBoot启动类代码: @SpringBootApplicationpublic class DemoApplication extends SpringBootServl ...
- (转)gcc学习笔记
1.gcc -Wall hello.c -o hello //编译源文件,显示警告信息 2../a.out //运行程序 3.gcc -Wall calc.c /usr/lib/libm.a -o ...
- 关于 truncate table 的一点学习札记
---下面整理笔记来之 itpub 的各位前辈的语录.这里做了一个汇总.仅供学习. truncate table后,oracle会回收表和其表中所在的索引到initial 大小,也就是初始分配的seg ...
- Struts2的配置文件——web.xml
任何MVC框架都需要与Web应用整合,这就不得不借助于web.xml文件,只有配置在web.xml文件中Servlet才会被应用加载. 通常,所有的MVC框架都需要Web应用加载一个核心控制器,对于S ...
- B.大钉骑马走江湖
江湖是什么,对于在象棋界厮杀的大钉来说,江湖就是一个矩阵,他的目标,就是在江湖之中骑着马,从他的位置出发,走到终点. 当然,大钉的马也遵从中国象棋中的“马走日”的规则,而且在矩阵中,也会有一些障碍物, ...
- 阶乘问题(大数阶乘)简单 n! (一个大数与一个小数相乘的算法 、一个大数与一个小数的除法算法 *【模板】 )
sdut oj 简单n! Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 给定一个数n(0 <= n <= 150), ...