Dễ 0 đã giải Tỷ lệ chấp nhận 0%

Chess Square Color

Determine the color of a square on an 8 by 8 board.

Bạn sẽ học được gì

  • Turn a square name into row and column numbers
  • Use parity to decide the color
  • Convert letters to numbers

Đề bài

Xem bản dịch

Use a board whose top-left square is white. Rows and columns start at 0 at the top left. Adjacent squares alternate colors. Return empty text for a position outside the board.

Implement chess_square_color(column, row).

Input

column: The column, counted from the left starting at 0.

row: The row, counted from the top starting at 0.

Output

Return white, black, or an empty string for invalid coordinates.

Ràng buộc

−10 ≤ column, row ≤ 20. Valid board coordinates are 0 through 7.

Ví dụ

Ví dụ 1
Đầu vào:
column = 2
row = 3

Kết quả mong đợi:
expected = "black"
Đầu vào:
long long column = 2;
long long row = 3;

Kết quả mong đợi:
std::string expected = "black";
Đầu vào:
long column = 2;
long row = 3;

Kết quả mong đợi:
string expected = "black";

Giải thích

Five steps from the white corner change color an odd number of times.

Ví dụ 2
Đầu vào:
column = 8
row = 0

Kết quả mong đợi:
expected = ""
Đầu vào:
long long column = 8;
long long row = 0;

Kết quả mong đợi:
std::string expected = "";
Đầu vào:
long column = 8;
long row = 0;

Kết quả mong đợi:
string expected = "";

Giải thích

Column 8 is outside the valid range 0 through 7.

Hướng tiếp cận

Hãy tự thử trước, sau đó mở từng gợi ý một.

Gợi ý 1

Square color alternates with moves across rows and columns, so only the coordinate pattern matters.

Gợi ý 2

A parity check on row and column can tell you which color to return.

Gợi ý 3

Return empty text immediately when either coordinate is outside the 0 to 7 board range.

Độ phức tạp mong đợi

O(1) time, O(1) extra space

Tham khảo

Thử thách này đã được điều chỉnh cho CodeUtility. Hãy làm theo đề bài và các ràng buộc ở trên.

Lời giải đạt tốt nhất của mỗi người. Lời giải nhanh hơn, tốn ít bộ nhớ hơn và được đánh giá cao hơn sẽ đứng trên.

Chưa có lời giải nào đạt. Giải bài này để giành hạng nhất.

Học hỏi từ người khác

Giải pháp cộng đồng

Chưa có giải pháp nào được chia sẻ. Hãy giải thử thách này để đăng giải pháp đầu tiên.