Altered Structure
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
//ab<61><62><EFBFBD>յ<EFBFBD>
|
||||
int dx[8] = {-2, -1, 1, 2, 2, 1, -1, -2};
|
||||
int dy[8] = {1, 2, 2, 1, -1, -2, -2, -1};
|
||||
int step[9][9];
|
||||
|
||||
void dfs(int _a, int _b, int _n, int _m){
|
||||
if(_n > 8 || _n < 1 || _m < 1 || _m > 8) return;
|
||||
if(_n == _a && _m == _b) return;
|
||||
for(int i = 0; i < 8; i++){
|
||||
int nx = _n + dx[i];
|
||||
int ny = _m + dy[i];
|
||||
|
||||
if (nx >= 1 && nx <= 8 && ny >= 1 && ny <= 8) {
|
||||
int stepnew = step[_n][_m] + 1;
|
||||
if (step[nx][ny] == 0 || stepnew < step[nx][ny]) {
|
||||
step[nx][ny] = stepnew;
|
||||
dfs(_a, _b, nx, ny);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main(){
|
||||
int a, b, n, m;
|
||||
cin >> n >> m >> a >> b;
|
||||
step[n][m] = 0;
|
||||
dfs(a, b, n, m);
|
||||
cout << step[a][b] << endl;
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user