Original question address
Question
An 8x8 chess board, you have a pawn "Horse ". Calculate the minimum number of steps from one grid to the other.
Unlike General DFS, the route you can take is not up, down, left, right, or four directions. Instead"
It consists of eight directions. Although it is a chess horse, it is actually the same as the Chinese chess horse.
Code
#include<iostream>#include<cstdio>#include<cstring>using namespace std;int d[8][2]={-1,-2,1,-2,-1,2,1,2,-2,1,-2,-1,2,1,2,-1};int map[8][8];int bi,bj;void dfs(int i,int j,int step){ map[i][j]=step; if(bi==i&&bj==j) return; for(int ii=0;ii<8;ii++) { int x=i+d[ii][0]; int y=j+d[ii][1]; if(x<0||x>=8||y<0||y>=8||map[x][y]<=step+1) continue; dfs(x,y,step+1); }}int main(){ char a,b; int ai,aj; while(cin>>a>>aj>>b>>bj) { memset(map,0x3f,sizeof map); ai=a-'a'; bi=b-'a'; aj--; bj--; dfs(ai,aj,0); printf("To get from %c%d to %c%d takes %d knight moves.\n",a,aj+1,b,bj+1,map[bi][bj]); }}
Digress
Chinese chess and chess should have some origins. Indeed, they have different origins. Chinese chess was available before the Warring States period, and the popular saying of chess originated from ancient India. The rules and forms of the two have been changing for thousands of years. Since ancient China had a long history of dealings with India, it was extremely possible for Chinese chess to encounter international chess in history. They interact with each other. Then they developed separately. For example, the two chess boards are 8x8 grids, and the horses and elephants in the two games are the same way. In ancient Chinese chess, the Queen's law is not as powerful as it is today. The Queen's law recorded in the document is the same as that of Chinese chess. Similarly, the Chinese chess board unearthed in the Tang Dynasty was black and white, which is undoubtedly the same as the modern chess board. The Chu and Han Dynasties in Chinese chess appeared in the Song Dynasty.