这个C过了86 , 求调

#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define INF 0x3f3f3f3f
#define debug(a) cout<<#a<<"="<<a<<endl;
#define rep(i,a,n) for(int i =a;i<=n;++i)
#define per(i,a,n) for(int i =n;i>=a;--i)
const double pi = 3.14;
#define ld long double
#define int long long
// #define ll long long
typedef pair<int,int> pii;
typedef unsigned long long ull;

const int N =2000;

int n , m;
int stx , sty;
int edx , edy;
char s[N][N];
bool vis[N][N][6];
const int dx[] = {1,0,-1,0} , dy[] = {0,-1,0,1};
typedef array<int,3>node;
bool bfs(){
  queue<node>q;
  q.push({stx , sty , 0});
  q.push({stx , sty , 1});  //上
  q.push({stx , sty , 2});  //下
  q.push({stx , sty , 3});  //左
  q.push({stx , sty , 4});  //右
  while(!q.empty()){
    auto [x,y,op] = q.front();
    q.pop();
   
      for(int i =0;i<4;++i){
        int tx = x + dx[i] , ty = y + dy[i];
        if(tx <1 || tx > n || ty < 1 || ty > m)continue;
        if(s[tx][ty] == '#' && (i+1) != op)continue;
        if(vis[tx][ty][op])continue;
        if(tx == edx && ty == edy)return true;
        vis[tx][ty][] = 1;
        q.push({tx,ty,0});
      }
    
  return false;
}

void solve(){
 cin>>n>>m;
  rep(i,1,n){
    rep(j,1,m){
      cin>>s[i][j];
      if(s[i][j] == 'S'){
      stx = i , sty = j;
    }
    if(s[i][j] == 'E'){
      edx = i , edy =j;
    }
    }
  }

  
  if(!bfs()){
    cout<<"NO";
  }
  else cout<<"YES"<<endl;
}



signed main(){
    ios::sync_with_stdio(false);
    cout.tie(0);
    cin.tie(0);
    int _ =1;   
    
    while(_--){
      solve();
    }
    
    return 0;
}

全部评论

相关推荐

07-22 11:07
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务