ง่าย 0 ข้อที่แก้แล้ว อัตราผ่าน 0%

Chess Square Color

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

สิ่งที่คุณจะได้เรียนรู้

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

โจทย์

ดูฉบับแปล

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.

เงื่อนไข

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

ตัวอย่าง

ตัวอย่างที่ 1
อินพุต:
column = 2
row = 3

ผลลัพธ์ที่คาดหวัง:
expected = "black"
อินพุต:
long long column = 2;
long long row = 3;

ผลลัพธ์ที่คาดหวัง:
std::string expected = "black";
อินพุต:
long column = 2;
long row = 3;

ผลลัพธ์ที่คาดหวัง:
string expected = "black";

คำอธิบาย

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

ตัวอย่างที่ 2
อินพุต:
column = 8
row = 0

ผลลัพธ์ที่คาดหวัง:
expected = ""
อินพุต:
long long column = 8;
long long row = 0;

ผลลัพธ์ที่คาดหวัง:
std::string expected = "";
อินพุต:
long column = 8;
long row = 0;

ผลลัพธ์ที่คาดหวัง:
string expected = "";

คำอธิบาย

Column 8 is outside the valid range 0 through 7.

แนวทางการแก้

ลองทำเองก่อน แล้วค่อยเปิดคำใบ้ทีละข้อ

คำใบ้ 1

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

คำใบ้ 2

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

คำใบ้ 3

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

ความซับซ้อนที่คาดหวัง

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

แหล่งอ้างอิง

โจทย์นี้ปรับให้เหมาะกับ CodeUtility กรุณาทำตามคำชี้แจงและข้อจำกัดด้านบน

คำตอบที่ผ่านดีที่สุดของแต่ละคน คำตอบที่เร็วกว่า ใช้หน่วยความจำน้อยกว่า และได้คะแนนดีกว่าจะอยู่ด้านบน

ยังไม่มีคำตอบที่ผ่าน แก้โจทย์นี้แล้วคว้าอันดับหนึ่งไปเลย

เรียนรู้จากคนอื่น

แนวทางจากชุมชน

ยังไม่มีวิธีทำที่เผยแพร่ ลองแก้โจทย์นี้เพื่อแชร์วิธีแรก