ZCMU-1345: Chess

There is a pitfall in this question, that is, I have never played chess, thinking that I can only walk a straight line with a slope of plus or minus 1 once when I walk diagonally. . .

topic

ZCMU-1345
1345: Chess
Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 224 Solved: 75
[Submit][Status][Web Board]
Description
The chess board is a black and white 8 * 8 square, chess pieces Put it in the middle of the grid.
The rules for walking the king, queen, cart, and elephant are as follows:
Wang: You can walk horizontally, straight, and diagonally, but each step is limited to one square.
Back: You can walk horizontally, vertically, and diagonally, and the number of steps per step is unlimited.
Car: You can walk both horizontally and vertically, not diagonally, and there is no limit to the number of grids.
Bishop: It can only go diagonally, and there is no limit to the number of grids.
Your task is to write a program that, given a starting position and a target position, calculate the minimum number of steps required for the king, rear, car, and elephant to walk from the starting position to the target position.

The
first line of Input is the number of test data groups t (0 <= t <= 20). Each of the following rows is a set of test data, each group includes two positions on the chessboard, the first is the starting position, and the second is the target position. The position is expressed in the form of "letter-number", with letters from "a" to "h", and numbers from "1" to "8".

Output
For each set of input test data, output the minimum number of steps required for the king, queen, car, and elephant. If it cannot be reached, output "Inf".

Sample Input
2
a1 c3
f5 f8
Sample Output
2 1 2 1
3 1 1 Inf
HINT
Please use %s to read the string


idea

This question is actually a simple simulation, but there are still a few points to note.
First, we classify the relative positions of the starting point and the ending point. Divided into three categories:
1. The starting point and the end point are in the same line or in the same column
2. The starting point and the end point are on a straight line with an absolute slope of 1
3. Other situations

Insert picture description here

Pay special attention to the three situations of walking diagonally, one is that you cannot walk, the other is to walk, and the other is to walk two steps.
First, if you can walk diagonally, then the starting point and the end point must be the same color (the upper board Medium), that is to say, the addition of the horizontal and vertical coordinates of the start point and the addition of the horizontal and vertical coordinates of the end point must be the same even, so that it is judged whether it is on a straight line with an absolute slope of 1, if it is, it is 1 step, otherwise it is two steps Come to

In other cases, it's simple, let's go directly to the code!


AC code

#include<bits/stdc++.h>
using namespace std;
#define gcd __gcd;
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define pre(i,a,b) for(int i=a;i>=b;--i)
#define m(x) memset(x,0,sizeof x);
int dis_a,dis_b,dis_c,dis_d;
int main(){
    
    
    int T;
    int flag1,flag2;
    int Xs,Ys,Xe,Ye;
    char input1[5],input2[5];
    scanf("%d",&T);
    while(T--)
    {
    
    
        //flag1标记起始和终点是否在同一行或者同一列
        //flag2标记是否在斜率为1的直线上
        flag1 = flag2 = 0;
        scanf("%s%s",input1,input2);
        Xs = input1[0] - 'a' + 1;Ys = input1[1] - '0';
        Xe = input2[0] - 'a' + 1;Ye = input2[1] - '0';
//        cout << Xs << " " << Ys << endl;
//        cout << Xe << " " << Ye << endl;
        if(Xs==Xe&&Ys==Ye){
    
    
            printf("0 0 0 0\n");
            continue;
        }
        if(Xs==Xe||Ys==Ye)flag1 = 1;
        if(fabs(Ys-Ye)==fabs(Xs-Xe))flag2 = 1;
        if(flag1){
    
    
            dis_a = fabs(Xs-Xe) + fabs(Ys-Ye);
            dis_b = 1;
            dis_c = 1;
            if((Xs+Ys)+(Xe+Ye)&1)//检查起点和终点是否在同色棋盘上
            printf("%d %d %d %s\n",dis_a,dis_b,dis_c,"Inf");
            else
            printf("%d %d %d %d\n",dis_a,dis_b,dis_c,2);
            continue;
        }
        if(flag2){
    
    
            dis_a = fabs(Xs-Xe);
            dis_b = dis_d = 1;
            dis_c = 2;
            printf("%d %d %d %d\n",dis_a,dis_b,dis_c,dis_d);
            continue;
        }
        dis_a = max(fabs(Xs-Xe),fabs(Ys-Ye));
        dis_b = 2;
        dis_c = 2;
        if((Xs+Ys)+(Xe+Ye)&1)
        printf("%d %d %d %s\n",dis_a,dis_b,dis_c,"Inf");
        else
        printf("%d %d %d %d\n",dis_a,dis_b,dis_c,2);
    }
    
    return 0;
}

Guess you like

Origin blog.csdn.net/DAVID3A/article/details/115256688