【題解】ZeroJudge e601: 00255 – Correct Move

【題目敘述】https://zerojudge.tw/ShowProblem?problemid=e601

#include <iostream>
using namespace std;

int K, Q, N, kx, ky, qx, qy, nx, ny;

int main() {
    while (cin >> K >> Q >> N){
        if (K == Q){
            cout << "Illegal state\n";
            continue;
        }
        kx = K / 8;
        ky = K % 8;
        qx = Q / 8;
        qy = Q % 8;
        nx = N / 8;
        ny = N % 8;
        if (qy!=ny && qx!=nx){
            cout << "Illegal move\n";
            continue;
        }
        if (Q == N || K == N){
            cout << "Illegal move\n";
            continue;
        }
        if (qy==ny && ny==ky){
            if (qx > nx && kx < qx && kx >= nx){
                cout << "Illegal move\n";
                continue;
            }
            if (qx < nx && kx > qx && kx <= nx){
                cout << "Illegal move\n";
                continue;
            }
        }
        else if (qx==nx && nx==kx){
            if (qy > ny && ky < qy && ky >= ny){
                cout << "Illegal move\n";
                continue;
            }
            if (qy < ny && ky > qy && ky <= ny){
                cout << "Illegal move\n";
                continue;
            }
        }
        if ((abs(nx-kx)==1 && ny==ky) || (nx==kx && abs(ny-ky)==1)){
            cout << "Move not allowed\n";
            continue;
        }
        bool flag = false;
        if (kx+1 < 8 && kx+1 != nx && ky != ny) flag = true;
        if (kx-1 >= 0 && kx-1 != nx && ky != ny) flag = true;
        if (ky+1 < 8 && kx != nx && ky+1 != ny) flag = true;
        if (ky-1 >= 0 && kx != nx && ky-1 != ny) flag = true;
        if (flag) cout << "Continue\n";
        else cout << "Stop\n";
    }
}

Python code (credit: Amy Chou)

while True:
    try:
        K, Q, N = map(int, input().strip().split())
        
        if (K == Q):
            print("Illegal state")
            continue
        
        kx = K // 8
        ky = K % 8
        qx = Q // 8
        qy = Q % 8
        nx = N // 8
        ny = N % 8
        
        if (qy != ny) and (qx != nx):
            print("Illegal move")
            continue
    
        if (Q == N) or (K == N):
            print("Illegal move")
            continue
        
        if (qy == ny) and (ny == ky):
            if (qx > nx) and (kx < qx) and (kx >= nx):
                print("Illegal move")
                continue
            
            if (qx < nx) and (kx > qx) and (kx <= nx):
                print("Illegal move")
                continue
        elif (qx == nx) and (nx == kx):
            if (qy > ny) and (ky < qy) and (ky >= ny):
                print("Illegal move")
                continue
            if (qy < ny) and (ky > qy) and (ky <= ny):
                print("Illegal move")
                continue
        
        if (((abs(nx-kx)==1) and (ny==ky)) or ((nx==kx) and (abs(ny-ky)==1))):
            print("Move not allowed")
            continue
        
        flag = False
        if (kx+1 < 8) and (kx+1 != nx) and (ky != ny):
            flag = True
        if (kx-1 >= 0) and (kx-1 != nx) and (ky != ny):
            flag = True
        if (ky+1 < 8) and (kx != nx) and (ky+1 != ny):
            flag = True
        if (ky-1 >= 0) and (kx != nx) and (ky-1 != ny):
            flag = True
        if flag:
            print("Continue")
        else:
            print("Stop")
    except:
        break
分享本文 Share with friends