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;
|
||||||
|
}
|
21
Algorithm/Divide&Conquer/UVa536-TreeRecovery.cpp
Normal file
21
Algorithm/Divide&Conquer/UVa536-TreeRecovery.cpp
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
void build(string& _pre, string& _in, int pre_front, int pre_end, int in_front, int in_end){
|
||||||
|
if (pre_front > pre_end) return;
|
||||||
|
|
||||||
|
char root;
|
||||||
|
root = _pre[pre_front];
|
||||||
|
int k = _in.find(root, in_front);//<2F><>in_front<6E><74>ʼ
|
||||||
|
int leftsize = k - in_front;
|
||||||
|
build(_pre, _in, pre_front + 1, pre_front + leftsize, in_front, k - 1);
|
||||||
|
build(_pre, _in, pre_front + leftsize + 1 , pre_end, k + 1, in_end);
|
||||||
|
cout << root << " ";
|
||||||
|
}
|
||||||
|
int main(){
|
||||||
|
string pre, in;
|
||||||
|
cin >> pre >> in;
|
||||||
|
build(pre, in, 0, pre.size() - 1, 0, in.size() - 1);
|
||||||
|
return 0;
|
||||||
|
}
|
Reference in New Issue
Block a user