Блог пользователя anujnaruka02

Автор anujnaruka02, история, 8 месяцев назад, По-английски

Problem link

My solution is giving wrong output. Any suggestions to help me in this. My code is as follows —

int main(){
int n,m;
   cin>>n>>m;

   vector<vector<char>> v(n,vector<char>(m));
   vector<vector<int>> vis(n,vector<int>(m,0));
   vector<vector<int>> prevs(n,vector<int>(m));
   queue<pair<int,int>> q;
   pair<int,int> start,end;
   
   int delrow[] = {-1,0,1,0};
   int delcol[] = {0,1,0,-1};
   string dir = "URDL";

   for(int i = 0; i<n; i++){
    for(int j = 0; j<m; j++){
        cin>>v[i][j];

        if(v[i][j] == 'A'){
            start = {i,j};
        }
        else if(v[i][j] == 'B'){
            end = {i,j};
        }
    }
   } 
   
   q.push({start.first,start.second});
   vis[start.first][start.second] = 1;
   

   while(!q.empty()){
    int row = q.front().first;
    int col = q.front().second;
    q.pop();

    for(int i = 0; i<4; i++){
        int nrow = row + delrow[i];
        int ncol = col + delcol[i];

        if(nrow>=0 && nrow<n && ncol>=0 && ncol<n && !vis[nrow][ncol] && v[nrow][ncol] != '#'){
            vis[nrow][ncol] = 1;
            prevs[nrow][ncol] = i;
            q.push({nrow,ncol});
        }
    }
   }

   if(vis[end.first][end.second] == 1){
    cout << "YES" << endl;
    vector<int> steps;
    while(end.first != start.first || end.second != start.second){
        int p = prevs[end.first][end.second];
        steps.push_back(p);

        end.first = end.first - delrow[p];
        end.second = end.second - delcol[p];
    }

    reverse(steps.begin(),steps.end());

    cout << steps.size() << endl;
    for(auto it : steps){
        cout << dir[it];
    }
    cout << endl;
   }
   else{
    cout << "NO" << endl;
   }
return 0;
}
  • Проголосовать: нравится
  • +8
  • Проголосовать: не нравится