NC14608 after and maze bfs

Posted by Liodel on Wed, 02 Mar 2022 13:38:19 +0100

After's algorithm book is left in a maze called AIJ. This maze has N*M rooms, the entrance of the maze is (1, 1), and the algorithm book is left in (r, c). The rooms in the maze have four states: empty rooms, inaccessible rooms, rooms with Mephisto and rooms with Lilith. Mephisto will deny everything, and Lilith will tempt people to do an activity called YK. After is a weak willed person. When he meets Mephisto and Lilith, he will become a super YK robot with empty eyes. After each step can go from his current room to one of the four rooms up, down, left and right. After is afraid of becoming a super YK robot, so we should get the algorithm book as soon as possible and escape from the entrance. How many steps does after need to take to get the algorithm book back from the entrance and escape the maze without becoming a super YK robot?

The first line is a positive integer t (T < = 10), indicating a total of T groups of data.
For each group of data, the first row has four positive integers N, M, r, C (1 < = N, M < = 1000; 1 < = r < = N; 1 < = C < = M).
The next N lines, each with M characters, represent the state of the room, "." Indicates an empty room, "*" indicates an inaccessible room, "F" indicates a room with Mephisto, and "m" indicates a room with Lilith.
Data assurance (1, 1) is ".".  
Output a row for each group of data, that is, the minimum number of steps to be taken after. If after cannot retrieve the algorithm book, output "IMPOSSIBLE" (without quotation marks).

input

1
4 4 4 3
..**
*F..
*.*.
*M.F

output

14

Typical bfs example, but the meaning of its title is really fascinating. After reading other people's interpretation of the title, I found that: at the same time, K will become a super YK robot after Y.

Therefore, our problem-solving idea can become to compare the length of walking only F with that of walking only W.

#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<queue>
#include<algorithm>
using namespace std;
char a[2002][2002];
int dist[2002][2002],dis[2002][2002];
int dx[]={-1,0,0,1},dy[]={0,1,-1,0};
int t;
int n,m,r,c;
typedef pair<int,int> PII;
int bfs()
{
    queue<PII> q;
    memset(dist,-1,sizeof dist);
    dist[1][1]=0;
    q.push({1,1});
    while(q.size())
    {
        auto t=q.front();
        q.pop();
        for(int i=0;i<4;i++)
        {
            int xx=t.first+dx[i],yy=t.second+dy[i];
            if(xx>=1&&xx<=n&&yy>=1&&yy<=m&&dist[xx][yy]==-1&&a[xx][yy]!='*'&&a[xx][yy]!='M')
            {
                dist[xx][yy]=dist[t.first][t.second]+1;
                q.push({xx,yy});
            }
        }
    }
    return dist[r][c];
}
int bfs2()
{
    queue<PII> p;
    memset(dis,-1,sizeof dis);
    dis[1][1]=0;
    p.push({1,1});
    while(p.size())
    {
        auto t=p.front();
        p.pop();
        for(int i=0;i<4;i++)
        {
            int xx=t.first+dx[i],yy=t.second+dy[i];
            if(xx>=1&&xx<=n&&yy>=1&&yy<=m&&dis[xx][yy]==-1&&a[xx][yy]!='*'&&a[xx][yy]!='F')
            {
                dis[xx][yy]=dis[t.first][t.second]+1;
                p.push({xx,yy});
            }
        }
    }
    return dis[r][c];
}
int main()
{
    //cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
    cin>>t;
    while(t--)
    {
        cin>>n>>m>>r>>c;
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
                cin>>a[i][j];
        bfs();
        bfs2();
        /*for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                cout<<dist[i][j]<<" ";
            }
            cout<<endl;
        }*/
        //cout<<dist[r][c]<<" "<<dis[r][c]<<endl;
        if(dist[r][c]==-1&&dis[r][c]==-1) cout<<"IMPOSSIBLE"<<endl;
        else if(dist[r][c]!=-1&&dis[r][c]==-1) cout<<dist[r][c]*2<<endl;
        else cout<<dis[r][c]*2<<endl;
    }
    return 0;
}

I'm really worried about graph theory 😟  

Topics: Algorithm Graph Theory bfs