Luo Gu p1443

Is a horse walking 'day' shape, and then output to the respective positions of the shortest path, come to the stage outputs -1

Can be directly solved by BFS search, but the output having some problems, I started with printf ( "% - 5d) All WA, results into cout << left << setw (5); but AC

I do not know what the problem is, solution to a problem which was also used printf

#include<bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

const int INF = -1;
int n, m;
int sx, sy;
int maze[500][500];
int d[500][500];
int dx[8] = {-1, -2, -2, -1, 1, 2, 2, 1}, dy[8] = {-2, -1, 1, 2, 2, 1, -1, -2};

void bfs(int x, int y){
  queue< pair<int, int> > q;
  q.push( pair<int, int>(x, y) );
  d[x][y] = 0;

  while( !q.empty() ){
    pair<int, int> t = q.front(); q.pop();

    for(int i=0; i<8; i++){
      int nx = t.first + dx[i];
      int ny = t.second + dy[i];

      if(nx >= 0 && nx < n && ny >= 0 && ny < m && d[nx][ny] == INF){
        d[nx][ny] = d[t.first][t.second] + 1;
        q.push( pair<int, int>(nx, ny) );
      }

    }
  }
}

int main(){
  ios::sync_with_stdio(false);

  cin >> n >> m >> sx >> sy;
  memset(d, INF, sizeof d);

  bfs(sx-1, sy-1);

  for(int i=0; i<n; i++){
    for(int j=0; j<m; j++)
      cout<<left<<setw(5)<<d[i][j];
    cout << endl;
  }
  return 0;
}

 

Guess you like

Origin www.cnblogs.com/ssNiper/p/11265589.html