如有条件在检查位置

if conditions in checking location

本文关键字:位置 检查 有条件      更新时间:2023-10-16

我正在解决这个问题,但我被另外两个if条件卡住了。我不确定如何检查从b到g2-7。我也很困惑如何检查是否所有8个位置都可用。当使用if和for循环迭代或检查代码时,我总是感到困惑。以下是我目前掌握的信息:问题:http://codeforces.com/problemset/problem/710/a

目前代码:

#include <iostream>
#include <string>
using namespace std;
int main(){
  string c;
  cin >> c;
  if(c == "a8" || c == "h8" || c == "a1" || c == h1){
    cout << "3 moves only availablen" << endl;
  }
  // confused on this case
   if(c == c[0]( ){
      cout << "5 moves only availablen" << endl;
    }
     // and this case
    if(c ==){
      cout << "all 8 moves are availablen" << endl;
    }
}

首先,h1应该在引号中,因为它是一个字符串字面值,应该看起来像这样,而不是"h1"

第二种情况,当只有5个移动是沿着棋盘的一边(但不是角落)。因此,您只需要检查文件(列)是否为'a''h',或者秩(行)是否为'1''8'。使用else if来缩短为代码,这样如果它是一个角,您就不会输出两次。

那么你的第二个例子可以是这样的:

else if (c[0] == 'a' || c[0] == 'h' || c[1] == '1' || c[1] == '8){
    cout << "5 moves only availablen" << endl;
}

最后一种情况是:

else{
    cout << "all 8 moves only availablen" << endl;
}

其他情况都已处理完毕