Codeforces 793B Igor and his way to work dfs
来源:互联网 发布:开淘宝手持身份证照片 编辑:程序博客网 时间:2024/06/05 07:38
填一下半年前的坑,当时还是yeleng的短期队友。。结果首次见面就给我愉快的来了一套CF。。。好吧那是我第一次看CF 的题目。然后这么明显的搜索题目读了好久,然后心很慌地想写bfs,但是发现不会,诶,于是dfs,但是还是WA收尾的。。后来也没有补。。现在来填一下这个坑。
题意:从S到T两点转弯小于等于2次时存在就是YES,否则为NO。
思路:标准的DFS,但是这里还是要注意的是记录当前所在点所在方向的转弯次数,显然如果vis[x][y][dir]<=cnt时是无意义的,直接return就好(虽然我暂时不知道为什么要取等号,但是不取就T了,参考别人的博客了一下)。
附代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=1000+5;
int xx[4]={-1,1,0,0};
int yy[4]={0,0,-1,1};
bool flag;
int stx,sty;
int tx,ty;
int n,m;
char maps[N][N];
int vis[N][N][6];
void dfs(int x,int y,int dir,int cnt)
{
if(x<1||x>n||y<1||y>m||cnt>2)
return;
if(vis[x][y][dir]<=cnt)
return;
if(x==tx&&y==ty)
{
flag=true;
return;
}
if(maps[x][y]=='*')
return;
vis[x][y][dir]=cnt;
for(int i=0;i<4;i++)
{
int vx=x+xx[i];
int vy=y+yy[i];
if(dir==-1)
dfs(vx,vy,i,0);
else if(dir!=i)
dfs(vx,vy,i,cnt+1);
else dfs(vx,vy,i,cnt);
}
}
int main()
{
int p,q;
int i,j,k;
scanf("%d%d",&n,&m);
getchar();
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
scanf("%c",&maps[i][j]);
if(maps[i][j]=='S')
{
stx=i;
sty=j;
}
else if(maps[i][j]=='T')
{
tx=i;
ty=j;
}
}
getchar();
}
flag=false;
memset(vis,100,sizeof(vis));
dfs(stx,sty,-1,0);
if(flag)
printf("Yes\n");
else printf("No\n");
}
- Codeforces 793B Igor and his way to work dfs
- Codeforces 793B Igor and his way to work 题解
- Codeforces 793B Igor and his way to work (dfs/bfs
- codeforces B. Igor and his way to work dfs or bfs
- codeforces Igor and his way to work
- B. Igor and his way to work
- B. Igor and his way to work
- B. Igor and his way to work
- codeforce 793 B Igor and his way to work
- Igor and his way to work-Codeforce 793B BFS
- CF793B:B Igor and his way to work(DFS & BFS)
- Tinkoff B. Igor and his way to work(BFS)
- Codeforces-739B:Igor and his way to work(BFS+优化)
- Codeforces 793b B. Igor and his way to work 觉得大神写的3维bfs太复杂,突然发现这题是连连看算法。
- CF-Tinkoff Challenge-Elimination Round-B-Igor and his way to work
- BFS--cf793b Igor and his way to work
- 【codeforces 776B】Sherlock and his girlfriend
- Codeforces 776B-Sherlock and his girlfriend
- python3下使用map()函数返回<map object at 0x00000000038327F0>导致不能直接使用的解决办法
- datatable中的如何让页面上的序号在翻页后自增
- None of the configured nodes are available
- Listener
- oracle一张表的id关联另一张表中的字段
- Codeforces 793B Igor and his way to work dfs
- 链表详解
- 2017.10.19一试
- java单例设计模式 懒汉式与饿汉式
- Java 8的新特性
- Yolo解读值Cython
- python编程练习1-----实现全排列问题
- Python 3.x 字符串:makestrans+translate函数
- Android开发环境搭建(二)——基于Eclipse的开发环境搭建