Paste will expire never.
- #include <iostream>
- #include <vector>
- #include <string>
- using namespace std;
- const int SIZE = 5;
- int mas[SIZE][SIZE];
- bool usd[SIZE][SIZE];
- vector<int> steps;
- int moveX[8] = {-2,-2,-1,-1,1,1,2,2};
- int moveY[8] = {-1,1,-2,2,-2,2,-1,1};
- int code(int x, int y) {
- return x * SIZE + y;
- }
- void uncode(int val, char& x, char& y) {
- x = 'A' + val / SIZE;
- y = '1' + val % SIZE;
- }
- bool correct(int x, int y) {
- if (x < 0 || y < 0) return false;
- if (x >= SIZE || y >= SIZE) return false;
- return true;
- }
- bool go(int x, int y, int cnt, string& ans) {
- if (cnt == 0) {
- char x,y;
- for (size_t i=0;i<steps.size();++i) {
- uncode(steps[i],x,y);
- ans += x;
- ans += y;
- }
- return true;
- }
- usd[x][y] = true;
- for (int i = 0; i < 8; ++i) {
- int nx = x + moveX[i];
- int ny = y + moveY[i];
- if (correct(nx, ny) && !usd[nx][ny]) {
- usd[nx][ny] = true;
- steps.push_back(code(nx,ny));
- if (go(nx,ny,cnt-1,ans)) {
- return true;
- }
- steps.pop_back();
- usd[nx][ny] = false;
- }
- }
- return false;
- }
- int main() {
- #ifdef _DEBUG
- freopen("input.txt","r",stdin);
- freopen("output.txt","w",stdout);
- #endif
- char beg[2 + 1];
- cin>>beg;
- int begX = beg[0] - 'A';
- int begY = beg[1] - '1';
- string ans = "";
- steps.push_back(code(begX, begY));
- cout<<(go(begX, begY, SIZE*SIZE-1, ans) ? ans : "0");
- return 0;
- }
Editing is locked.