B. Igor and his way to work

time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Woken up by the alarm clock Igor the financial analyst hurried up to the work. He ate his breakfast and sat in his car. Sadly, when he opened his GPS navigator, he found that some of the roads in Bankopolis, the city where he lives, are closed due to road works. Moreover, Igor has some problems with the steering wheel, so he can make no more than two turns on his way to his office in bank.

Bankopolis looks like a grid of n rows and m columns. Igor should find a way from his home to the bank that has no more than two turns and doesn't contain cells with road works, or determine that it is impossible and he should work from home. A turn is a change in movement direction. Igor's car can only move to the left, to the right, upwards and downwards. Initially Igor can choose any direction. Igor is still sleepy, so you should help him.

Input

The first line contains two integers n and m (1 ≤ n, m ≤ 1000) — the number of rows and the number of columns in the grid.

Each of the next n lines contains m characters denoting the corresponding row of the grid. The following characters can occur:

  • "." — an empty cell;
  • "*" — a cell with road works;
  • "S" — the cell where Igor's home is located;
  • "T" — the cell where Igor's office is located.

It is guaranteed that "S" and "T" appear exactly once each.

Output

In the only line print "YES" if there is a path between Igor's home and Igor's office with no more than two turns, and "NO" otherwise.

Examples
input
5 5
..S..
****.
T....
****.
.....
output
YES
input
5 5
S....
****.
.....
.****
..T..
output
NO
 
解题思路:
题目意思为求能否在不转2个弯以上的情况下从S到T,那么首先从S开始上下左右遍历得出在不转第一个弯之前能到达的所有位子,然后再分别由这些位置为起点遍历四周,循环两次,可得出转两次
弯所能到达所有位置,只需判断其中是否包括T就行
 
ps: 代码逻辑还是有问题,这方法是类似于取巧的,还是有漏洞,虽然能过cf样例,但无法过评论中的样例。还是得用搜索做才是正确的做法
实现代码:
#include<bits/stdc++.h>
using namespace std; const int Max = ;
char f[Max][Max];
int d[Max][Max];
int m,n; int fuck(int x,int y,int v)
{
int i=x+;int j=y;
while(i<m&&d[i][j]==-&&f[i][j]!='*') d[i++][j]=v;
i=x-;j=y;
while(i>=&&d[i][j]==-&&f[i][j]!='*') d[i--][j]=v;
i=x;j=y+;
while(j<n&&d[i][j]==-&&f[i][j]!='*') d[i][j++]=v;
i=x;j=y-;
while(j>=&&d[i][j]==-&&f[i][j]!='*') d[i][j--]=v;
} int main()
{
ios_base::sync_with_stdio();
int i,j;
cin>>m>>n;
memset(d, -, Max*Max*sizeof(int));
for(i=;i<m;i++) for(j=;j<n;j++) cin>>f[i][j];
for(i=;i<m;i++) for(j=;j<n;j++) if(f[i][j]=='S'){
d[i][j]=;
//cout<<i<<j;
fuck(i,j,);
}
for(i=;i<m;i++) for(j=;j<n;j++) if(d[i][j]==){
fuck(i,j,);}
for(i=;i<m;i++) for(j=;j<n;j++) if(d[i][j]==){
fuck(i,j,);}
for(i=;i<m;i++) for(j=;j<n;j++) if(f[i][j]=='T'){
cout<<(d[i][j]==-?"NO":"YES");}
return ;
}
 

codeforces 793B. Igor and his way to work的更多相关文章

  1. codeforces 793B - Igor and his way to work(dfs、bfs)

    题目链接:http://codeforces.com/problemset/problem/793/B 题目大意:告诉你起点和终点,要求你在只能转弯两次的情况下能不能到达终点.能就输出“YES”,不能 ...

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

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

  3. codeforces 598D Igor In the Museum

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

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

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

  5. Educational Codeforces Round 1 D. Igor In the Museum bfs 并查集

    D. Igor In the Museum Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/598 ...

  6. Educational Codeforces Round 1(D. Igor In the Museum) (BFS+离线访问)

    题目链接:http://codeforces.com/problemset/problem/598/D 题意是 给你一张行为n宽为m的图 k个询问点 ,求每个寻问点所在的封闭的一个上下左右连接的块所能 ...

  7. 【CodeForces - 598D】Igor In the Museum(bfs)

    Igor In the Museum Descriptions 给你一个n*m的方格图表示一个博物馆的分布图.每个方格上用'*'表示墙,用'.'表示空位.每一个空格和相邻的墙之间都有一幅画.(相邻指的 ...

  8. Codeforces 598D:Igor In the Museum

    D. Igor In the Museum time limit per test 1 second memory limit per test 256 megabytes input standar ...

  9. [BFS]Codeforces Igor In the Museum

     Igor In the Museum time limit per test 1 second memory limit per test 256 megabytes input standard ...

随机推荐

  1. 18-(基础入门篇)GPRS(Air202)拨打电话--(由于板子做修订,所以暂停更新)

    https://www.cnblogs.com/yangfengwu/p/9968883.html 这个直接用官方给的demo就可以 先睹为快 现在说个需求哈,是当初一个人给提出的需求 例如存入的号码 ...

  2. 如何应用ML的建议-下

    正则化与过拟合(highvariance)和欠拟合(highbias)的关系-部分(五) ML的诊断方法-部分(六) 如何采取下一步-部分(七) 部分(五) 从图中可以看出,正则化项可以用来影响模型函 ...

  3. SQL Server如何更改系统用户dbo的所属账号

    在SQL Server的每个数据库中都有一个dbo系统用户,dbo是系统默认创建的,无法被删除,如下: dbo在内部其实是绑定了一个SQL Server账号的,可以通过其属性查看Login name, ...

  4. 【LeetCode105】Construct Binary Tree from Preorder and Inorder Traversal★★

    1.题目 2.思路 3.java代码 //测试 public class BuildTreeUsingInorderAndPreorder { public static void main(Stri ...

  5. Luogu4546 THUWC2017 在美妙的数学王国中畅游 LCT、泰勒展开

    传送门 题意:反正就是一堆操作 LCT总是和玄学东西放在一起我们不妨令$x_0=0.5$(其实取什么都是一样的,但是最好取在$[0,1]$的范围内),将其代入给出的式子,我们得到的$f(x)$的式子就 ...

  6. 【适配整理】Android 7.0 调取系统相机崩溃解决android.os.FileUriExposedException

    一.写在前面 最近由于廖子尧忙于自己公司的事情和 OkGo (一款专注于让网络请求更简单的网络框架) ,故让LZ 接替维护 ImagePicker(一款支持单.多选.旋转和裁剪的图片选择器),也是处理 ...

  7. 【持续更新中···】Linux下的小技巧

    1.Linux回到上级文件的命令: cd ..回到上一级目录(注意:cd 和..中间有空格) cd ~回到home目录 cd -回到某一目录

  8. Gitblit版本服务器环境部署记录

    Gitblit介绍Gitblit 是一个纯 Java 库用来管理.查看和处理 Git 资料库.相当于 Git 的 Java 管理工具,支持linux系统.Git是分布式版本控制系统,它强调速度.数据一 ...

  9. WEEK 7:团队项目的感想

    经过了几个星期的团队协作,我们的“爬虫”有了很大的完善,我作为团队中的主DEV,在这个过程中一边工作一边阅读,也有了不少的收获. Brooks的<没有银弹>告诉我们,在软件领域,没有什么绝 ...

  10. SqlDataAdapter简单介绍 (转)

    From:  http://blog.sobnb.com/u/92/5532.html 一.特点介绍 1.表示用于填充 DataSet 和更新 SQL Server 数据库的一组数据命令和一个数据库连 ...