首页 > 其他 > 详细

CF 793B - Igor and his way to work(bfs)

时间:2019-05-23 23:56:26      阅读:248      评论:0      收藏:0      [点我收藏+]

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\n");
        else printf("NO\n");
    return 0;
}
View Code

 

CF 793B - Igor and his way to work(bfs)

原文:https://www.cnblogs.com/Suiyue-Li/p/10915261.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!