#include <iostream>
#include <queue>
int N, M;
int sx, sy, fx, fy;
int grid[110][110];
struct coor{
int x,y;
};
//Generic Class
queue<struct coor> q;
int main() {
scanf("%d %d", &N, &M);
scanf("%d %d", &sx, &sy);
scanf("%d %d", &fx. &fy);
q.push({sx, sy});
//-----solution------------------------------------
while(q.size() > 0) {
auto c = q.front(); //peek //auto -> 자동으로 변수 지정)
q.pop();
if(c.x == fx && c.y == fy){
//destination
printf("%d\n", grid[c.x][c.y]);
}
int dx[] ={1,1,-1,-1,2,2,-2,-2};
int dy[] ={2,-2,2,-2,1,-1,1,-1};
for(int i =0; i <8; i++) {
int nx = c.x + dx[i];
int ny = c.y + dy]i];
if(!(1 < nx && nx <=N && 1<= ny && ny <= M))
continue;
if(grid[nx][ny]) !=0)
continue;
grid[nx][ny] = grid[c.x][c.y]+1
q.push({nx, ny});
}
}
return 0;
}
To embed this project on your website, copy the following code and paste it into your website's HTML: