zoukankan      html  css  js  c++  java
  • Tinkoff Challenge

    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
    Note

    The first sample is shown on the following picture:

    In the second sample it is impossible to reach Igor's office using less that 4 turns, thus there exists no path using no more than 2 turns. The path using exactly 4 turns is shown on this picture:

    题目大意:问你从S到T的路径能否拐弯小于等于两次。

    题目解析:dfs,保存前一次搜索的方向

    #include <bits/stdc++.h>
    
    using namespace std;
    const int MAXN = 1005;
    int n, m;
    char s[MAXN][MAXN];
    typedef pair<int, int>P;
    int xi[] = {1, 0, -1, 0};
    int yi[] = {0, 1, 0, -1};
    int d[MAXN][MAXN], pre[MAXN][MAXN];
    int flag = 0;
    void dfs(int x, int y, int pre, int cnt) {
    //    printf("%d %d %d %d 
    ", x, y, pre, cnt);
        if(cnt > 2 || flag) return;
        if(s[x][y] == 'T') {
            flag = 1;
            return;
        }
        if(cnt > d[x][y]) return;
        d[x][y] = cnt;
        for(int i = 0; i < 4; i++) {
            int nx = x + xi[i];
            int ny = y + yi[i];
            if(nx >= 0 && nx < n && ny >= 0 && ny < m && s[nx][ny] != '*') {
                if(i == pre) {if(d[x][y] < d[nx][ny]) dfs(nx, ny, pre, cnt);}
                else if(d[x][y] < d[nx][ny] + 1)dfs(nx, ny, i, cnt+1);
            }
        }
    }
    
    int main() {
        scanf("%d%d", &n, &m);
        for(int i = 0; i < n; i++) scanf("%s", s[i]);
        for(int i = 0; i < n; i++) {
            for(int j = 0; j < m; j++){
                d[i][j] = 4;
            }
        }
        for(int i = 0; i < n; i++) {
            for(int j = 0; j < m; j++)
                if(s[i][j] == 'S') {
                    s[i][j] = '*';
                    for(int k = 0; k < 4; k++) {
                        int nx = i + xi[k];
                        int ny = j + yi[k];
                        if(nx >= 0 && nx < n && ny >= 0 && ny < m && s[nx][ny] != '*') {
                             dfs(nx, ny, k, 0);
                        }
                    }
                }
        }
    //    for(int i = 0; i < n; i++){
    //        for(int j = 0; j < m; j++){
    //            printf("%d", d[i][j]);
    //        }
    //        printf("
    ");
    //    }
        if(flag) printf("YES
    ");
        else printf("NO
    ");
        return 0;
    }
    View Code
  • 相关阅读:
    Kappa Architecture: A Different Way to Process Data
    Lambda architecture and Kappa architecture
    Questioning the lambda architecure
    Lambda Architecture: Achieving Velocity and Volume with Big Data
    beego 参数配置
    hadoop 3.1.1 安装
    Idea 切换git账号
    IntelliJ IDEA 开发git多模块项目
    打印1到1亿的平方
    IDEA 逆向工程
  • 原文地址:https://www.cnblogs.com/cshg/p/6759458.html
Copyright © 2011-2022 走看看