YTU OJ 3166 shared bicycle

Posted by webgod on Wed, 01 Jan 2020 03:09:27 +0100

Shared bicycle

 

Title Description

Sharing bicycles into Yantai, Xiaoming decided to try. Xiaoming starts the bike sharing app to easily find nearby bikes. So the question is, how many meters does Xiao Ming have to walk to the nearest bike?  
Now simplify the problem. Set the map as a two-dimensional flat area composed of 100 meters * 100 meters pixel blocks. If there is a bicycle in a box, the pixel block will be displayed as the character "x"; if there is a passable road in this box, it will be displayed as "."; if the box is a building, it will be displayed as "*", and the building will not be passable.  
Xiaoming's position on the map is displayed as "O", and he can walk in eight directions: "up", "down", "left", "right", "top left", "bottom left", "top right", "bottom right". The figure below shows a Xiaoming standing in pixel block O. if Xiaoming goes to the right to X, he will walk 100 meters; if he goes to the right, he will walk 141 meters (no square).  
Suppose Xiao Ming and the bicycle are in the middle of the square. Now I give T maps based on the above rules. The number of lines and columns of the maps are n and m respectively. Please estimate how many meters Xiaoming will have to walk to get to the nearest bicycle?  

If the decimal appears in the calculation, it will be discarded directly. The final output is integer.
There is at least one bicycle in the given map, if the bicycle position cannot be reached finally, output - 1.  
 

input

Line 1, T, indicates that there are T groups of test data below
For each group of test data
(1) Line 1, n and m, indicates the size of the map;
(2) Line 2 starts with a specific map.  

output

T row data
One integer per line, representing the solution of the problem

sample input

2
3 8
.....x..
.o...x..
........
8 10
.***......
.***......
.***..x...
..........
.....*****
..o..*****
.......x..
...*******

sample output

400
523

//Master bfs queue

//Because of multiple sets of data, vis marks

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<queue>
int sum = 1e5;
char mp[999][999];
int vis[999][999]={0};
int dx[8]={-1,1,0,0,1,1,-1,-1};
int dy[8]={0,0,-1,1,-1,1,-1,1};
int m,n;
using namespace std;
typedef struct M
{
    int x,y;
    int steps;
}M;
void bfs(int xx,int yy,int nx,int ny)
{
    queue <M> q;
    M head;
    head.x=xx;
    head.y=yy;
    head.steps=0;
    q.push(head);
    vis[head.x][head.y]=1;
    M s,next;
    while(!q.empty())
    {
        s=q.front();
        q.pop();
        if(s.x==nx&&s.y==ny)
        {
            if(sum>s.steps)
            {
                sum=s.steps;
            }
            return ;
        }
        else
        {
            for(int i=0;i<8;i++)
            {
                next.x=s.x+dx[i];
                next.y=s.y+dy[i];
                if(next.x>=0&&next.x<n&&next.y>=0&&next.y<m&&vis[next.x][next.y]==0&&mp[next.x][next.y]!='*')
                {
                    if(i<4)
                    {
                        next.steps=s.steps+100;
                    }
                    else
                    {
                        next.steps=s.steps+141;
                    }
                    q.push(next);
                    vis[next.x][next.y]=1;
                }
            }
        }
    }
}
int main()
{
    int t;
    cin>>t;
    int x,y;
    while(t--)
    {
        sum = 1e5;
        cin>>n>>m;
        for(int i=0;i<n;i++)
        {
            scanf("%s",&mp[i]);
        }
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                if(mp[i][j]=='o')
                {
                    x=i;
                    y=j;
                }
            }
        }
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                if(mp[i][j]=='x')
                {
                    memset(vis,0,sizeof(vis));
                    bfs(x,y,i,j);
                }
            }
        }
        if(sum==1e5)
        {
            printf("-1\n");
        }
        else
        {
            printf("%d\n",sum);
        }
    }
    return 0;
}