搜索
来源:互联网 发布:淘宝卖家怎么升级快 编辑:程序博客网 时间:2024/05/16 09:54
深度优先搜索
虽然是很简单的一道题目,但是通过自己码出来还是很有成就感的~
POJ 1979
适合入门
#include <iostream>
#include <vector>
#include <map>
#include <queue>
#include <stdio.h>
#include <string.h>
#include <utility>
#define MAX 25
using namespacestd;
typedef pair<int,int> P;
char maze[MAX][MAX];
int W,H;
int sx,sy;
int step=-1;
int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};
//由于题目限制只能上下左右移动,所以不能直接用两个for循环套用
void dfs(int x,int y)
{
step++;
maze[x][y]='#';
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
int nx=x+dx[i];
int ny=y+dy[i];
if(0<=nx && nx<H &&0<=ny && ny<W &&maze[nx][ny]!='#')
{
dfs(nx,ny);
}
}
}
}
int main()
{
while(cin>>W>>H)
{
memset(maze,0,sizeof(maze));
if(W==0&&H==0)
return0;
step=0;
for(int i=0;i<H;i++)
{
for(int j=0;j<W;j++)
{
cin>>maze[i][j];
if(maze[i][j]=='@')
{
sx=i;sy=j;
}
}
}
dfs(sx,sy);
cout<<step<<endl;
}
return0;
}
- 搜索
- 搜索
- 搜索
- 搜索
- 搜索
- 搜索
- 搜索
- 搜索
- 搜索
- 搜索
- 搜索
- 搜索
- 搜索
- 搜索
- 搜索
- 搜索
- 搜索
- 搜索
- sparkRDDsample定量样本输出为0的解决方法
- UML类图几种关系的总结
- CollectionView使用入门
- 内核启动阶段kernel_init(init)进程分析
- Spring简介 01
- 搜索
- ios开发--IOS 中通过URL Schemes让一个应用程序启动另一个指定应用程序
- 【cdoj 1335】 郭大侠与“有何贵干?” 线段树+扫描线
- 一个简单的下划菜单
- 子类A继承父类B, A a = new A(); 则父类B构造函数、父类B静态代码块、父类B非静态代码块、子类A构造函数、子类A静态代码块、子类A非静态代码块 执行的先后顺序是?
- ORA-01460: 转换请求无法实现或不合理
- 【原创】如何确定Kafka的分区数、key和consumer线程数
- java调用CMD完成文件复制
- EF(Linq)框架使用过程中的小技巧汇总