Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Task : _example
- Author : Phumipat C. [MAGCARI]
- Language: C++
- Created : 06 February 2023 [21:42]
- */
- #include<bits/stdc++.h>
- using namespace std;
- struct A{
- int i,j,mask;
- };
- queue<A > que;
- char a[510][510];
- int dis[510][510][10];
- int dir[2][4] = {{-1,1,0,0},{0,0,-1,1}};
- // J = 0
- // B = 1
- // P = 2
- int main(){
- cin.tie(0)->sync_with_stdio(0);
- cin.exceptions(cin.failbit);
- int n,m,sti,stj;
- cin >> n >> m;
- for(int i=1;i<=n;i++){
- for(int j=1;j<=m;j++){
- cin >> a[i][j];
- if(a[i][j] == 'S')
- sti = i,stj = j;
- }
- }
- for(int i=1;i<=n;i++)
- for(int j=1;j<=m;j++)
- for(int k=0;k<8;k++)
- dis[i][j][k] = 1e9;
- dis[sti][stj][0] = 0;
- que.push({sti,stj,0});
- while(!que.empty()){
- A now = que.front();
- que.pop();
- if(a[now.i][now.j] == 'E'){
- cout << dis[now.i][now.j][now.mask];
- return 0;
- }
- for(int k=0;k<4;k++){
- int ni = now.i+dir[0][k],nj = now.j+dir[1][k];
- if(ni<1 || nj<1 || ni>n || nj>m) continue;
- if(a[ni][nj] == '#') continue;
- if(a[ni][nj] == 'J' && (now.mask & (1<<0)) == 0) continue;
- if(a[ni][nj] == 'B' && (now.mask & (1<<1)) == 0) continue;
- if(a[ni][nj] == 'P' && (now.mask & (1<<2)) == 0) continue;
- int nmask = now.mask;
- if(a[ni][nj] == 'j') nmask |= (1<<0);
- if(a[ni][nj] == 'b') nmask |= (1<<1);
- if(a[ni][nj] == 'p') nmask |= (1<<2);
- if(dis[ni][nj][nmask] > dis[now.i][now.j][now.mask]+1){
- dis[ni][nj][nmask] = dis[now.i][now.j][now.mask]+1;
- que.push({ni,nj,nmask});
- }
- }
- }
- cout << "-1\n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement