2016-06-15 58 views
0

我想檢查騎士的下一步是否會同時威脅kind和queen,如果有這樣的位置,它會輸出YES和position,否則會是NO。檢查下一步是否爲check-mate

輸入將只包含K爲王,Q爲皇后,N爲騎士,並且它們不會重複多次。

樣品輸入:

........ 
........ 
........ 
...K.... 
....Q... 
........ 
N....... 
........ 

該輸入例如指示該騎士是2A,Queen是在圖4E中,國王是在圖5D。

這裏是我的代碼:

#include <cmath> 
#include <stdio.h> 
#include <cstdlib> 
#include <iostream> 
#include <vector> 
#include <ctype.h> 
#include <fstream> 
#include <cstddef> 
#include <sstream> 
#include<string.h> 
#include<cstring> 
#include<map> 
#include<algorithm> 
using namespace std; 

int main() 
{ 
      string temp ; 
      bool flag1 = false , flag2 = false ; 
      int row1 = 0 , col1 = 0 , row2 = 0 , col2 = 0 ; 
      int ik=0 , jk=0 , iq=0 , jq=0 , in=0 , jn=0 , i = 8 ; 
      while (std::getline (std::cin,temp)) 
      { 
       for (int j = 0 ; j<=7 ; j++) 
       { 
        if(temp[j] == 'K') 
        { ik = i ; jk = j+1 ; } 
        else if(temp[j] == 'Q') 
        { iq = i ; jq = j+1 ; } 
        else if(temp[j] == 'N') 
        { in = i ; jn = j+1 ; } 
       } 
       i-- ; 
      } 

      // j for columns , i for rows 
      // if jk = 1 means A , =2 means B , and so on 
      int threatk[8][2] = {0} , threatq[8][2]= {0} , expn[8][2] = {0} ; 

      // columns first (position 0) 
      // rows second (position 1) 
      threatk[0][0] = jk+1 ; 
      threatk[0][1] = ik+2 ; 
      threatk[1][0] = jk+1 ; 
      threatk[1][1] = ik-2 ; 
      threatk[2][0] = jk+2 ; 
      threatk[2][1] = ik+1 ; 
      threatk[3][0] = jk+2 ; 
      threatk[3][1] = ik-1 ; 
      threatk[4][0] = jk-1 ; 
      threatk[4][1] = ik+2 ; 
      threatk[5][0] = jk-1 ; 
      threatk[5][1] = ik-2 ; 
      threatk[6][0] = jk-2 ; 
      threatk[6][1] = ik+1 ; 
      threatk[7][0] = jk-2 ; 
      threatk[7][1] = ik-1 ; 

     threatq[0][0] = jq+1 ; 
     threatq[0][1] = iq+2 ; 
     threatq[1][0] = jq+1 ; 
     threatq[1][1] = iq-2 ; 
     threatq[2][0] = jq+2 ; 
     threatq[2][1] = iq+1 ; 
     threatq[3][0] = jq+2 ; 
     threatq[3][1] = iq-1 ; 
     threatq[4][0] = jq-1 ; 
     threatq[4][1] = iq+2 ; 
     threatq[5][0] = jq-1 ; 
     threatq[5][1] = iq-2 ; 
     threatq[6][0] = jq-2 ; 
     threatq[6][1] = iq+1 ; 
     threatq[7][0] = jq-2 ; 
     threatq[7][1] = iq-1 ; 

     expn[0][0] = jn+1 ; 
     expn[0][1] = in+2 ; 
     expn[1][0] = jn+1 ; 
     expn[1][1] = in-2 ; 
     expn[2][0] = jn+2 ; 
     expn[2][1] = in+1 ; 
     expn[3][0] = jn+2 ; 
     expn[3][1] = in-1 ; 
     expn[4][0] = jn-1 ; 
     expn[4][1] = in+2 ; 
     expn[5][0] = jn-1 ; 
     expn[5][1] = in-2 ; 
     expn[6][0] = jn-2 ; 
     expn[6][1] = in+1 ; 
     expn[7][0] = jn-2 ; 
     expn[7][1] = in-1 ; 

    for (int a = 0 ; a<=7 ; a++) 
     { 
     for (int b=0 ; b<=7 ; b++) 
     { 
if ( ( expn[a][0] == threatk[b][0] && expn[a][1] == threatk[b][1])) 
     { flag1 = true ; col1 = expn[a][0] ; row1 = expn[a][1] ; } 

     } 
     } 

    for (int a = 0 ; a<=7 ; a++) 
     { 
      for (int b=0 ; b<=7 ; b++) 
      { 
    if ( ( expn[a][0] == threatq[b][0] && expn[a][1] == threatq[b][1])) 
      { flag2 = true ; col2 = expn[a][0] ; row2 = expn[a][1] ; } 

      } 
     } 
if ( (flag1 && flag2) && (col1 >= 1 && col1 <= 8 && row1 >= 1 && row1 <= 8) 
    && (col2 >= 1 && col2 <= 8 && row2 >= 1 && row2 <= 8) 
     && (col1 = col2 && row1 = row2) ) 
{ string out = "" ; 
    if (col1 == 1)out = "A" ; 
    else if (col1 == 2) out = "B" ; 
    else if (col1 == 3) out = "C" ; 
    else if (col1 == 4) out = "D" ; 
    else if (col1 == 5) out = "E" ; 
    else if (col1 == 6) out = "F" ; 
    else if (col1 == 7) out = "G" ; 
    else if (col1 == 8) out = "H" ; 

    cout<<"YES"<<" "<<row1<<out ; 
} 
    else cout<<"NO" ;} ' 

我的做法是得到國王和騎士女王的威脅位置,以及與之相比較下一個可能的移動爲騎士 它的正常工作,但它在一些測試中失敗了,我沒有意識到我只是知道它是否通過了所有的測試。 你認爲什麼是錯的?

+1

標題有誤導性;所描述的問題與被拘留者無關。 (死亡是國王受到襲擊的情況,並且攻擊無法解決,這是一個非常困難的問題,因爲通常有三種解決方案:移動國王,移除攻擊者,或者放入其他棋子) – MSalters

+0

其中這是從哪裏來的?你能分享鏈接嗎? – vegi

回答

1

當您設計代碼時,請確保所有部件都易於測試。設計功能可以完成一件清晰的事情,並且可以輕鬆地重複使用。之後對它們進行測試,以便可以確定代碼的哪部分是錯誤的。

這是很難審查你的代碼,並找出什麼可能是錯誤的。我在Python中編寫了一個應該通過所有可能的邊緣案例的解決方案,我會在這裏分享它。解析輸入和輸出不是它的一部分。

N = 8 

def generateThreat(y, x): 
    threats = [] 

    candidate = (y+2, x+1) 
    if (candidate[0] < N-1 and candidate[1] < N-1): 
     threats.append(candidate) 

    candidate = (y+2, x-1) 
    if (candidate[0] < N-1 and candidate[1] >= 0): 
     threats.append(candidate) 

    candidate = (y-2, x+1) 
    if (candidate[0] >= 0 and candidate[1] < N-1): 
     threats.append(candidate) 

    candidate = (y-2, x-1) 
    if (candidate[0] >= 0 and candidate[1] >= 0): 
     threats.append(candidate) 

    candidate = (y+1, x+2) 
    if (candidate[0] < N-1 and candidate[1] < N-1): 
     threats.append(candidate) 

    candidate = (y+1, x-2) 
    if (candidate[0] < N-1 and candidate[1] >= 0): 
     threats.append(candidate) 

    candidate = (y-1, x+2) 
    if (candidate[0] >= 0 and candidate[1] < N-1): 
     threats.append(candidate) 

    candidate = (y-1, x-2) 
    if (candidate[0] >= 0 and candidate[1] >= 0): 
     threats.append(candidate) 

    return threats 


def generateAllThreatsFromCurrent(y, x): 
    all_threats = set() 
    for next_step in generateThreat(y, x): 
     all_threats.update(generateThreat(next_step[0], next_step[1])) 

    return all_threats 



def isMatePossible(king, queen, knight): 
    y, x = knight 
    all_threats = generateAllThreatsFromCurrent(y, x) 
    if king in all_threats and queen in all_threads: 
     return True 

    return False