hdoj1728【搜索的两种写法】
以前的一道题目,现在拿到总觉得是DFS,然后T掉就没什么想法了,很狗的看了以前的写法(以前还是看题解的AC的),是BFS,每次都要转弯,但是之前你的达到一种他走到了死路,所以才是不得不转弯,写法也是非常棒,预处理的转弯数是-1就可以达到一开始转弯的+1抵消。
DFS写法:
中间判断两个条件,如果是起点,不是起点,然后剪枝的话,就是有flag直接return,还有那个点不行也return,主要是转弯数不行或者点不是都要return。
#include<iostream>
#include<cstdio>
#include<math.h>
#include<queue>
#include<map>
#include<stdlib.h>
#include<string>
#include<string.h>
#include<algorithm>
using namespace std;
typedef long long LL;
#define PI acos(-1.0)
const int N=1e2+10;
bool vis[N][N];
int n,m;
char ma[N][N];
int sx,sy;
int ex,ey;
int k;
int dx[4]={1,-1,0,0};
int dy[4]={0,0,-1,1};
int flag;
struct asd{
int x,y;
int num;
};
bool JUDGE(int x,int y)
{
if(x<=0||y<=0||x>n||y>m||ma[x][y]=='*')
return 0;
return 1;
}
queue<asd>q;
void BFS()
{
while(!q.empty())
q.pop();
memset(vis,0,sizeof(vis));
if(sx==ex&&sy==ey){
flag=1;
return;
}
asd now,ne;
now.x=sx;now.y=sy;
now.num=-1;
vis[now.x][now.y]=1;
q.push(now);
while(!q.empty())
{
now=q.front();
q.pop();
for(int i=0;i<4;i++)
{
ne.x=dx[i]+now.x;
ne.y=dy[i]+now.y; //因为之前肯定是不能再走下去了,所以必须转弯。
while(JUDGE(ne.x,ne.y)){ //然后一直在这个方向走走,走到不能走为止。
if(!vis[ne.x][ne.y]){
ne.num=now.num+1;
vis[ne.x][ne.y]=1;
if(ne.x==ex&&ne.y==ey&&ne.num<=k){
flag=1;
return;
}
q.push(ne);
}
ne.x+=dx[i];
ne.y+=dy[i];
}
}
}
}
int main()
{
int t;
cin>>t;
while(t--)
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
scanf("%s",ma[i]+1);
scanf("%d",&k);
scanf("%d%d%d%d",&sy,&sx,&ey,&ex);
flag=0;
BFS();
if(flag)
printf("yes\n");
else
printf("no\n");
}
return 0;
}
DFS写法
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PI;
typedef pair< PI, int> PII;
const double eps=1e-5;
const double pi=acos(-1.0);
const int mod=1e9+7;
const int INF=0x3f3f3f3f;
const int N=110;
char ma[N][N];
int vis[N][N];
int n,m,num;
int sx,sy,tx,ty,flag;
int dx[4]={0,0,-1,1};
int dy[4]={1,-1,0,0};
void dfs(int x,int y,int d)
{
int i;
if(vis[x][y]>num)
return;
if(vis[x][y]==num&&(x!=tx&&y!=ty))
return;
if(flag)
return;
if(x==tx&&y==ty){
if(vis[x][y]<=num)
flag=1;
return;
}
for(i=0;i<4;i++){
int xx=dx[i]+x;
int yy=dy[i]+y;
if(xx<0||yy<0||xx>=n||yy>=m||ma[xx][yy]=='*'||vis[xx][yy]<vis[x][y])
continue;
if(d!=-1&&i!=d&&vis[xx][yy]<vis[x][y]+1)
continue;
vis[xx][yy]=vis[x][y];
if(d!=-1&&i!=d){
vis[xx][yy]++;
}
dfs(xx,yy,i);
}
}
int main()
{
int T,i;
cin>>T;
while(T--){
scanf("%d%d",&n,&m);
for(i=0;i<n;i++){
scanf("%s",ma[i]);
}
scanf("%d%d%d%d%d",&num,&sy,&sx,&ty,&tx);
sy--;sx--;tx--;ty--;
flag=0;
memset(vis,1,sizeof(vis));
vis[sx][sy]=0;
dfs(sx,sy,-1);
if(flag) printf("yes\n");
else printf("no\n");
}
return 0;
}
hdoj1728【搜索的两种写法】的更多相关文章
- Python 自定义元类的两种写法
有关元类是什么大家自己搜索了解,我这里写一下实现元类的两种写法 # 自定义元类 #继承type class LowercaseMeta(type): ''' 修改类的属性名称为小写的元类 ''' # ...
- ORACLE 查询一个数据表后通过遍历再插入另一个表中的两种写法
ORACLE 查询一个数据表后通过遍历再插入另一个表中的两种写法 语法 第一种: 通过使用Oracle语句块 --指定文档所有部门都能查看 declare cursor TABLE_DEPT and ...
- EF架构~linq模拟left join的两种写法,性能差之千里!
回到目录 对于SQL左外连接我想没什么可说的,left join将左表数据都获出来,右表数据如果在左表中不存在,结果为NULL,而对于LINQ来说,要实现left join的效果,也是可以的,在进行j ...
- 运算符关键字。数据区别大小写。日期范围。判空的两种写法。NOT IN的两种写法。IN范围可含NULL,但NOT IN值范围不能含NULL。
比较:>,<,=,>=,<=,<>(!=) 逻辑:AND,OR,NOT 范围:BETWEEN...AND... 范围:IN,NOT IN 判空:IS NULL, I ...
- 快速排序partition过程常见的两种写法+快速排序非递归实现
这里不详细说明快速排序的原理,具体可参考here 快速排序主要是partition的过程,partition最常用有以下两种写法 第一种: int mypartition(vector<int& ...
- java 路径分隔符File.separator 以及 路径两种写法"/"和"\\"
一.File.separator File file=new File(); 这句是新建一个文件.file.separator这个代表系统目录中的间隔符,说白了就是斜线,不过有时候需要双线,有时候是单 ...
- iOS中表视图单元格事件用nib和storyboard的两种写法总结
从ios6开始,苹果公司推出了storyborad技术取代了nib的写法,这样代码量确实少写了很多,也比较简洁.但是,从学习的角度来说,阿堂认为 用nib的写法,虽然多了些代码,但是对于掌握知识和原理 ...
- linq和ef关于group by取最大值的两种写法
LINQ: var temp = from p in db.jj_Credentials group p by p.ProfessionID into g select new { g.Key, Ma ...
- ThinkPHP中Widget的两种写法及调用
Widget扩展一般用于页面组件的扩展,在页面根据需要输出不同的内容,下面介绍一下ThinkPHP中Widget的两种写法及调用 写法一: ArticlWidget.class.php文件: clas ...
随机推荐
- 经典游戏“大富翁4”存档文件修改器Rich4Editor下载
下载地址: http://files.cnblogs.com/files/xiandedanteng/Rich4Editor20170614.zip http://files.cnblogs.com/ ...
- sublime 高速打开跳转至关联文件
在下一枚web前端,近期在用sublime text2编辑器写前端.因为页面较多,项目较大,所以难免出现非常多引用文件和一些js的teample模板. 问题:在Sublime Text编写代码过程中要 ...
- DASH----Desktop and mobile Architecture for System Hardware----桌面和移动系统硬件架构(DASH)计划
http://baike.baidu.com/subview/813787/11301142.htm http://sites.amd.com/cn/business/it-solutions/man ...
- CPU Stepping
http://baike.baidu.com/view/16839.htm?fr=ala0_1_1 步进 编辑 步进(Stepping)是CPU的一个重要参数,也叫分级鉴别产品数据转换规范,“步进 ...
- 使用Genymotion调试出现错误INSTALL_FAILED_CPU_ABI_INCOMPATIBLE解决的方法
今天在使用android studio在Genymotion上调试程序出现INSTALL_FAILED_CPU_ABI_INCOMPATIBLE导致程序安装不了: 后来百度发现是要安装:http:// ...
- Anaconda和Pycharm安装和配置教程
1.下载Anaconda2 (最好选Python2.7的,兼容性好点) 在官网下载:https://www.continuum.io/downloads 2.安装Pycharm ...
- android自己定义开关控件
近日在android项目要使用开关控件.可是android中自带的开关控件不太惬意,所以就打算通过自己定义View写一个开关控件 ios的开关控件当然就是我要仿照的目标. 先上图: waterma ...
- Linux下编译安装源码包软件 configure ,make, make install, make test/check, make clean
http://www.360doc7.net/wxarticlenew/541275971.html 一.什么是源码包软件? 顾名思义,源码包就是源代码的可见的软件包,基于Linux和BSD系统的软件 ...
- Hibernate exception
1.a different object with the same identifier value was already associated with the session. 错误原因:在h ...
- webpack 的编译原理
自从接触了react,vue 这两个框架,都会用到webpack这个打包工具.面试的时候,经常被问到知道webpack的编译原理吗? 可以简单的介绍一下.每每这个时候都被问的哑口无言,平时用的时候挺顺 ...