题目链接:http://codeforces.com/problemset/problem/793/B

题目大意:告诉你起点和终点,要求你在只能转弯两次的情况下能不能到达终点。能就输出“YES”,不能就输出“NO”。

解题思路:这算是经典的转弯题了,接近半年没写搜索题了,,所以今天先拿这道题开刀吧。这个题关键就是“判重”,如何记录走过的点。我们可以开个三维数组,记录各个坐标各个方向上的转弯数,如果下次在到这个点这个方向,那就比较转弯数,如果这次转弯数大于等于已经记录的转弯数那就不用再找下去了,因为这次能走到的地方,上次肯定也能走到。估计一下时间复杂度大概为O(4*3n)。

dfs:

 #include<iostream>
#include<cstring>
using namespace std;
const int N=1e3+;
int m,n;
bool flag=false;
char map[N][N];
int vis[N][N][];//*关键*用来标记走过的点,记录该点朝着各方向转弯数
int d[][]={{,},{-,},{,},{,-}}; void dfs(int x,int y,int dir,int cnt){
if(x<=||x>m||y<=||y>n||cnt>)
return;
if(vis[x][y][dir]<=cnt)//如果这个位置这个方向已经走过,且用了更小的转弯数,那就不用再走这个点了
return;
if(map[x][y]=='T'){
flag=true;
return;
}
if(map[x][y]!='.'&&map[x][y]!='S')
return;
vis[x][y][dir]=cnt;
for(int i=;i<;i++) {
int x1=x+d[i][];
int y1=y+d[i][];
if(dir==-)
dfs(x1,y1,i,cnt);
else if(dir!=i)
dfs(x1,y1,i,cnt+);
else
dfs(x1,y1,i,cnt);
}
} int main(){
int index,indey;
scanf("%d %d",&m,&n);
getchar();
for(int i=;i<=m;i++){
for(int j=;j<=n;j++){
scanf("%c",&map[i][j]);
if(map[i][j]=='S'){
index=i;
indey=j;
}
}
getchar();
}
memset(vis,0x3f,sizeof(vis));//转弯数初始化为无限大
dfs(index,indey,-,);//-1表示开始位置没有方向
if(flag)
printf("YES\n");
else
printf("NO\n");
}

bfs,跟上面差不多的:

 #include<iostream>
#include<cstring>
#include<queue>
using namespace std;
const int N=1e3+;
int vis[N][N][];
int d[][]={{,},{-,},{,},{,-}};
char map[N][N];
int m,n;
bool flag=false; struct node{
int x,y,dir,cnt;
}now,t,pre;
//int num=0;
void bfs(int stax,int stay){
queue<node>q;
t.x=stax;
t.y=stay;
t.dir=-;
t.cnt=;
q.push(t);
while(!q.empty()){
pre=q.front();
q.pop();
for(int i=;i<;i++){
int x=pre.x+d[i][];
int y=pre.y+d[i][];
int cnt;
if(pre.dir==-)//判断一下上次方向和当前要走的方向的关系
cnt=;
else if(pre.dir==i)
cnt=pre.cnt;
else if(pre.dir!=i)
cnt=pre.cnt+;
if(x<=||x>m||y<=||y>n||cnt>)
continue;
if(map[x][y]=='T'){//到达终点
flag=true;
return;
}
if(map[x][y]!='S'&&map[x][y]!='.')
continue;
if(vis[x][y][i]<=cnt)//这个点这个方向已经有更优方案了
continue;
vis[x][y][i]=cnt;
now.x=x;
now.y=y;
now.dir=i;
now.cnt=cnt;
// num++;
// printf("%d\n",num);
q.push(now);
}
}
}
int main(){
int index,indey;
scanf("%d %d",&m,&n);
getchar();
for(int i=;i<=m;i++){
for(int j=;j<=n;j++){
scanf("%c",&map[i][j]);
if(map[i][j]=='S'){
index=i;
indey=j;
}
}
getchar();
}
memset(vis,0x3f,sizeof(vis));
bfs(index,indey);
if(flag)
printf("YES\n");
else
printf("NO\n");
}

codeforces 793B - Igor and his way to work(dfs、bfs)的更多相关文章

  1. codeforces 793B. Igor and his way to work

    B. Igor and his way to work time limit per test 3 seconds memory limit per test 256 megabytes input ...

  2. Codeforces Beta Round #94 div 2 C Statues dfs或者bfs

    C. Statues time limit per test 2 seconds memory limit per test 256 megabytes input standard input ou ...

  3. Codeforces 374 C. Travelling Salesman and Special Numbers (dfs、记忆化搜索)

    题目链接:Travelling Salesman and Special Numbers 题意: 给了一个n×m的图,图里面有'N','I','M','A'四种字符.问图中能构成NIMA这种序列最大个 ...

  4. codeforces793 B. Igor and his way to work (dfs)

    题目链接:codeforces793 B. Igor and his way to work (dfs) 求从起点到终点转方向不超过两次是否有解,,好水啊,感觉自己代码好搓.. #include< ...

  5. 【codeforces 793B】Igor and his way to work

    [题目链接]:http://codeforces.com/contest/793/problem/B [题意] 给一个n*m大小的方格; 有一些方格可以走,一些不能走; 然后问你从起点到终点,能不能在 ...

  6. codeforces 598D Igor In the Museum

    题目链接:http://codeforces.com/problemset/problem/598/D 题目分类:dfs 题目分析:处理的时候一次处理一片而不是一个,不然会超时 代码: #includ ...

  7. Codeforces 747F Igor and Interesting Numbers DP 组合数

    题意:给你一个数n和t,问字母出现次数不超过t,第n小的16进制数是多少. 思路:容易联想到数位DP, 然而并不是...我们需要知道有多少位,在知道有多少位之后,用试填法找出答案.我们设dp[i][j ...

  8. Codeforces Round #407 (Div. 1) B. Weird journey —— dfs + 图

    题目链接:http://codeforces.com/problemset/problem/788/B B. Weird journey time limit per test 2 seconds m ...

  9. Codeforces Round #306 (Div. 2) B. Preparing Olympiad dfs

    B. Preparing Olympiad Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/550 ...

随机推荐

  1. Ionic 3 延迟加载(Lazy Load)实战(一)

    本文分享并演示了在 Ionic 3 框架中如何进行模块的延迟加载(Lazy Load)开发. 在我的实战课程「快速上手Ionic3 多平台开发企业级问答社区」中,因为开发的仿知乎 App 模块间的加载 ...

  2. zookeeper 动态管理nginx配置

    假设我们有一个场景,所有服务器共享同一份配置文件,我们肯定不可能单独手动维护每台服务器,这时可以利用zookeeper的配置管理功能. 环境:python + nginx + zookeeper 目的 ...

  3. django通用权限控制框架

    在web项目中根据不同的用户肯定会限制其不同的权限,利用以下模块可以满足日常几乎所有的权限控制 permission_hook.py  # 自定义权限控制,必须返回True/false  ,True表 ...

  4. Centos下DNS+NamedManager高可用部署方案完整记录

    之前说到了NamedManager单机版的配置,下面说下DNS+NamedManager双机高可用的配置方案: 1)机器环境 主机名 ip地址 dns01.kevin.cn 192.168.10.20 ...

  5. css-preprocessors

    what ? 预处理器是css 能够使用 变量.操作符.函数.mixins.interpolations 等类似于js 功能的一种语言. 目前比较常用是三种:SASS.less .stylus . W ...

  6. 《Linux内核设计与实现》第五章学习笔记

    <Linux内核设计与实现>第五章学习笔记 姓名:王玮怡  学号:20135116 一.与内核通信     在Linux中,系统调用是用户空间访问内核的唯一手段:除异常和陷入外,它们是内核 ...

  7. myeclipse从SVN检出项目报错

    今天是在公司实习的第一天,从SVN服务器检出项目后发现报错. 解决方法: 1. 右键项目,选择属性properties-->选择resource-->将others选中并换为UTF-8 2 ...

  8. Voltage Keepsake CodeForces - 801C (思维+二分)

    题目链接 这是一道很棒的二分题. 思路: 首先先思考什么情况下是可以无限的使用,即输出-1. 我们思考可知,如果每一秒内所有设备的用电量总和小于等于充电器每秒可以充的电,那么这一群设备就可以无限使用. ...

  9. Sonatype Nexus 2.11.1-01 使用入门

    nexus安装与启动 linux下: 安装路径 /home/maven/nexus-2.11.1-01/ 启动方法 ./bin/nexus start windows下: 管理员模式运行cmd.exe ...

  10. nodefs模块的使用demo

    为什么要使用递归?因为stat本身就是一个异步的函数所有存在异步问题不能够进行循环遍历. 在使用该种方法时候需要注意的一点是必须要在箭头标记处进行数据数组的存取.否则会由于异步问题导致输出空或者其他问 ...