Hdu1372 DFS search for chess horse

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.