zoukankan      html  css  js  c++  java
  • CF 793B

    Igor and his way to work
    题目描述

    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.

    输入

    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.

    输出

    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.
    大致题意,在最多只能转2次弯的情况下能不能从S(入口)抵达T(出口)。

    思路

    采用一个4维数组,后两位用来储存方向和可转次数。

    #include<bits/stdc++.h`>
    using namespace std;
    char s[1005][1005];
    int dx[4]={1,-1,0,0};
    int dy[4]={0,0,1,-1};
    int used[1005][1005][4][2];
    int n,m;
    struct node
    {
        int x,y,d,sum;//x,y表示坐标,d为方向,sum可转次数
    };
    int  bfs(node zuobiao)
    {
        queue<node>q;
        for(int i=0;i<4;i++)
        {
            node v=zuobiao;
            v.x+=dx[i];
            v.y+=dy[i];
            if(v.x>=1&&v.x<=n&&v.y>=1&&v.y<=m&&s[v.x][v.y]!='*')//记录前四个可        
            {                                            //可用方向
                v.d=i;
                v.sum=2;
                used[v.x][v.y][i][2]=1;
                q.push(v);
            }
        }
        while(!q.empty())
        {
           node u=q.front();
            q.pop();
            if(s[u.x][u.y]=='T')//当遇到出口时说明可以实现,结果为真;
                return 1;
            for(int i=0;i<4;i++)
            {
                node v=u;
                v.x+=dx[i];
                v.y+=dy[i];
                if(v.x>=1&&v.x<=n&&v.y>=1&&v.y<=m&&s[v.x][v.y]!='*')
                {
                    if(v.d==i) //此为方向相同
                    {
                        if(used[v.x][v.y][i][v.sum]==0){
                        q.push(v);
                        used[v.x][v.y][i][v.sum]=1;
                        }
                    }
                    else if(v.sum>0)当方向不同时
                    {
                        if(!used[v.x][v.y][i][v.sum-1])
                        {
                            v.sum--;
                            v.d=i;   //**调换方向**
                            used[v.x][v.y][i][v.sum]=1;
                           q.push(v);
                        }
                    }
                }
     
            }
     
        }
        return 0;
    }
    int main()
    {
        scanf("%d%d",&n,&m);
        memset(used,0,sizeof(used));
        node zuobiao;
        for(int i=1;i<=n;i++)
        {
            scanf("%s",&s[i][1]);
            for(int j=1;j<=m;j++)
            {
                if(s[i][j]=='S')
                {
                    zuobiao.x=i;
                    zuobiao.y=j;
                }
            }
        }
           if(bfs(zuobiao)) printf("YES
    ");
            else printf("NO
    ");
        return 0;
    }
    View Code
  • 相关阅读:
    shell脚本中执行python脚本并接收其返回值的例子
    linux查找所有文件中某个字符串
    Shell脚本中单引号(‘)和双引号(“)的使用区别
    第一个shell脚本
    shell 比较符号
    source ~/.bash_profile是什么意思
    bash shell:获取当前脚本的绝对路径(pwd/readlink)
    poj 3307 Smart Sister 打表解因子生成数问题
    Python将JSON格式数据转换为SQL语句以便导入MySQL数据库
    UISegmentedControl的具体使用
  • 原文地址:https://www.cnblogs.com/Suiyue-Li/p/10915261.html
Copyright © 2011-2022 走看看