Nc14608 after and maze

after and maze

Title No.: NC14608
Time limit: 1 second for C / C + + and 2 seconds for other languages
Space limitation: C/C++ 131072K, other languages 26244k
64bit IO Format: %lld

Title Description  

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 to become a super YK robot, so we should get the algorithm book as soon as possible and escape from the entrance. How many steps does it take after to get back the algorithm book from the entrance and escape the maze without becoming a super YK robot?

Enter Description:

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 contains four positive integers N, M, r, C (1 < = N, M < = 1000; 1 < = r < = N; 1 < = C < = M).
Next, there are N lines with M characters in each line. Each line represents the state of the room, "." represents an empty room, "*" represents an inaccessible room, "F" represents a room with Mephisto, and "m" represents a room with Lilith.
The data guarantee (1, 1) is ".".

Output Description:

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, it will output "IMPOSSIBLE" (without quotation marks).

Example 1

input

copy

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

output

copy

14
//#include <bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<string>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<list>
#include<set>
#include<iomanip>
#include<cstring>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<cassert>
#include<sstream>
#include<algorithm>
using namespace std;
const int mod=1e9+7;
typedef long long  ll;
#define ls (p<<1)
#define rs (p<<1|1)
#define mid (l+r)/2
#define over(i,s,t) for(register long long i=s;i<=t;++i)
#define lver(i,t,s) for(register long long i=t;i>=s;--i)
const int MAXN = 305;
const int INF = 0x3f3f3f3f;
const int N=5e4+7;
const int maxn=1e5+5;
const double EPS=1e-10;
const double Pi=3.1415926535897;
//inline double max(double a,double b){
//    return a>b?a:b;
//}
//inline double min(double a,double b){
//    return a<b?a:b;
//}

int xd[8] = {0, 1, 0, -1, 1, 1, -1, -1};
int yd[8] = {1, 0, -1, 0, -1, 1, -1, 1};

//void Fire(){
//    queue<node> p;
//    p.push({fx,fy,0});
//    memset(fire, -1, sizeof(fire));
//    fire[fx][fy]=0;
//    while(!p.empty()){
//        node temp=p.front();
//        p.pop();
//        for(int i=0;i<8;i++){
//            int x=temp.x+xd[i];
//            int y=temp.y+yd[i];
//            if(x<0||x>=n||y<0||y>=m||fire[x][y]!=-1){
//                continue;
//            }
//            fire[x][y]=temp.val+1;
//            p.push({x,y,temp.val+1});
//        }
//    }
//}
//int bfs(){
//    queue<node> p;
//    memset(vis, 0, sizeof(vis));
//    p.push({sx,sy,0});
//    while (!p.empty()) {
//        node temp=p.front();
//        vis[temp.x][temp.y]=1;
//        p.pop();
//        for(int i=0;i<4;i++){
//            int x=temp.x+xd[i];
//            int y=temp.y+yd[i];
//            if(x<0||x>=n||y<0||y>=m)  continue;
//            if(x==ex&&y==ey&&temp.val+1<=fire[x][y]) return temp.val+1;
//            if(vis[x][y]||temp.val+1>=fire[x][y]||a[x][y]=='#') continue;
//            p.push({x,y,temp.val+1});
//        }
//    }
//    return -1;
//}
int t,n,m,r,c;
int num1,num2;
struct node{
    int x,y;
    int step;
};
char a[1010][1010];
char a1[1010][1010];
int vis[1010][1010];
int bfs(int x,int y,int x1,int x2){
    queue<node>p;
    node q;
    q.x=1;
    q.y=1;
    q.step=0;
    p.push(q);
    
    vis[1][1]=1;
    while (!p.empty()) {
        node now=p.front();
        p.pop();
        if(now.x==x1&&now.y==x2){
            return now.step;
        }
        for(int i=0;i<4;i++){
            q.x=now.x+xd[i];
            q.y=now.y+yd[i];
            if(q.x>0&&q.x<=n&&q.y>0&&q.y<=m&&!vis[q.x][q.y]&&a1[q.x][q.y]=='.'){
                q.step=now.step+1;
                vis[q.x][q.y]=1;
                p.push(q);
            }
        }
    }
    return -1;
}
int main()
{
    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];
                if(a[i][j]=='F'){
                    a1[i][j]='.';
                }
                else {
                    a1[i][j]=a[i][j];
                }
            }
        }
        num1=bfs(1,1,r,c);
        memset(vis, false, sizeof(vis));
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                
                if(a[i][j]=='M'){
                    a1[i][j]='.';
                }
                else {
                    a1[i][j]=a[i][j];
                }
            }
        }
        num2=bfs(1,1,r,c);
        if(num1<0&&num2<0){
            cout<<"IMPOSSIBLE"<<endl;
        }
        else if(num1<0||num2<0){
            cout<<2*max(num1,num2)<<endl;
        }
        else cout<<2*min(num1,num2)<<endl;
    }
}

Tags: Algorithm codeforce AcWing

Posted on Tue, 23 Nov 2021 19:52:12 -0500 by bubbasheeko