zoukankan      html  css  js  c++  java
  • 限制转弯次数的搜索

    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.

    Example
    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:

    题目分析 : 题意是让你最多转两次弯,问你是否能到达目的地。

    思路分析 :

      和正常的 BFS 一样,多了一个限制条件,就是最多转两次方向,WA 了一路,然后自己造样例,发现一个问题,就是我广搜的时候,我先给定 4 个方向,但是我在搜的时候,走过的点是有可能在继续走的,之前缺少了这步,当时改改了,记步数的地方没整好,改了以后是超时,然后我就想着题得用优先队列啊,先走 0 步的地方,在走 一步的地方,最后两步,思路应该没问题,写搓了。一位丁学长启发了我,把之前广搜的记步数的地方改了改,然后交就直接过了。

    代码示例 :

    const int eps = 1e6+5;
    const double pi = acos(-1.0);
    const int inf = 1<<29;
    #define Max(a,b) a>b?a:b
    #define Min(a,b) a>b?b:a
    #define ll long long
    int n, m;
    char mp[1005][1005];
    int sx, sy, gx, gy;
    
    struct node
    {
        int x, y;
        int f, c;
        node(int _x, int _y, int _f, int _c):x(_x), y(_y), f(_f), c(_c){}
    };
    queue<node>que;
    int dir[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};
    int vis[1005][1005];
    int sign = 0;
    
    void bfs(int x, int y, int pt){
        while(!que.empty()) que.pop();
        vis[x][y] = 0;
        que.push(node(x, y, pt, 0));
        
        while(!que.empty()){
            node v = que.front();
            que.pop();
            if (v.c > 2 || sign) continue;
            if (v.x == gx && v.y == gy) {sign = 1; return;}
            for(int i = 0; i < 4; i++){
                int fx = dir[i][0] + v.x;
                int fy = dir[i][1] + v.y;
                
                if (vis[fx][fy] != -1){
                    if (v.f == i && v.c < vis[fx][fy]){
                        que.push(node(fx, fy, i, v.c));
                        vis[fx][fy] = v.c;
                    }
                    else if (v.c + 1 < vis[fx][fy]) {
                         que.push(node(fx, fy, i, v.c+1));
                         vis[fx][fy] = v.c + 1;
                    }
                }
                else if (fx >= 1 && fx <= n && fy >= 1 && fy <= m && mp[fx][fy] != '*' && vis[fx][fy] == -1){
                    
                    if (v.f == i) {
                        que.push(node(fx, fy, i, v.c));
                        vis[fx][fy] = vis[v.x][v.y];
                    }
                    else {
                        que.push(node(fx, fy, i, v.c+1));
                        vis[fx][fy] = vis[v.x][v.y] + 1;
                    }    
                }
            }    
        }
    }
    
    int main() {
        //freopen("in.txt", "r", stdin);
        //freopen("out.txt", "w", stdout);
        scanf("%d%d", &n, &m);
        for(int i = 1; i <= n; i++){
            scanf("%s", mp[i]+1);
            for(int j = 1; j <= m; j++){
                if (mp[i][j] == 'S') {sx = i; sy = j; break;}
            }
            for(int j = 1; j <= m; j++){
                if (mp[i][j] == 'T') {gx = i; gy = j; break;}
            }    
        }
        for(int i = 0; i < 4; i++){
            memset(vis, -1, sizeof(vis));
            vis[sx][sy] = 0;
            bfs(sx, sy, i);
        }
        
        if (sign) printf("YES
    ");
        else printf("NO
    ");
        return 0;
    }
    
    东北日出西边雨 道是无情却有情
  • 相关阅读:
    sed 命令编辑文本
    Iocomp控件教程之Pie Chart——饼状图控件
    《Go并发编程实战》第2版 紧跟Go的1.8版本号
    浅谈PHP数据结构之栈
    oracle11g导入dmp文件(根据用户)
    exp命令ORACLCE10G导出ORACLE11G的数据1455错误
    将war文件解压到指定目录
    JAVA包命名规范
    Eclipse中新建jsp文件访问页面时乱码问题
    网页编码就是那点事
  • 原文地址:https://www.cnblogs.com/ccut-ry/p/8403997.html
Copyright © 2011-2022 走看看