zoukankan      html  css  js  c++  java
  • 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
    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 = 1009;
    char f[Max][Max];
    int d[Max][Max];
    int m,n;
    
    int fuck(int x,int y,int v)
    {
        int i=x+1;int j=y;
        while(i<m&&d[i][j]==-1&&f[i][j]!='*') d[i++][j]=v;
        i=x-1;j=y;
        while(i>=0&&d[i][j]==-1&&f[i][j]!='*') d[i--][j]=v;
        i=x;j=y+1;
        while(j<n&&d[i][j]==-1&&f[i][j]!='*') d[i][j++]=v;
        i=x;j=y-1;
        while(j>=0&&d[i][j]==-1&&f[i][j]!='*') d[i][j--]=v;
    }
    
    int main()
    {
        ios_base::sync_with_stdio(0);
        int i,j;
        cin>>m>>n;
        memset(d, -1, Max*Max*sizeof(int));
        for(i=0;i<m;i++) for(j=0;j<n;j++) cin>>f[i][j];
        for(i=0;i<m;i++) for(j=0;j<n;j++) if(f[i][j]=='S'){
            d[i][j]=0;
            //cout<<i<<j;
            fuck(i,j,0);
        }
        for(i=0;i<m;i++) for(j=0;j<n;j++) if(d[i][j]==0){
            fuck(i,j,1);}
        for(i=0;i<m;i++) for(j=0;j<n;j++) if(d[i][j]==1){
            fuck(i,j,2);}
        for(i=0;i<m;i++) for(j=0;j<n;j++) if(f[i][j]=='T'){
            cout<<(d[i][j]==-1?"NO":"YES");}
        return 0;
    }
     
  • 相关阅读:
    提高网站访问速度的34条军规(7-10)
    指针与函数传参的思考
    提高网站访问速度的34条军规(11-13)
    [置顶] 程序员面试之道(《程序员面试笔试宝典》)之如何回答技术性的问题?
    CentOS6.4 编译安装Python 3.3.2
    hdu 4055 Number String(有点思维的DP)
    解读ASP.NET 5 & MVC6系列(4):核心技术与环境配置
    解读ASP.NET 5 & MVC6系列(6):Middleware详解
    解读ASP.NET 5 & MVC6系列(5):Configuration配置信息管理
    解读ASP.NET 5 & MVC6系列(7):依赖注入
  • 原文地址:https://www.cnblogs.com/kls123/p/6869264.html
Copyright © 2011-2022 走看看