Problem Statement and Algorithm Description

1 Travel _ Solution

CSP-J2026 Official Solutions8.4 KBUploaded on2026/11/07 11:22

[CSP-J Algorithm Intensive Course: 2026 Problem 1] Travel

1. Problem Description

Xiao R and Xiao X are planning a high-speed train trip. They purchased two second-class tickets with their respective carriage and seat numbers. They hope to sit in adjacent seats so they can travel together.

The train they are riding consists of 17 cars, numbered from 01 to 17. Cars 01 and 16 are first-class; all others are second-class. Both passengers hold second-class tickets and will sit in a second-class car. Each second-class car has 18 rows with 5 seats per row. Seat numbers consist of a two-digit row number followed by a single uppercase letter (e.g., 03C indicates seat C in row 3). Seats in each row are arranged left to right as A, B, C, D, F, with an aisle between C and D. Thus, the left side of the aisle is A–B–C, and the right side is D–F.

Here is a seating diagram for a second-class carriage:

Queue 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18
F 01F 02F 03F 04F 05F 06F 07F 08F 09F 10F 11F 12F 13F 14F 15F 16F 17F 18F
D 01D 02D 03D 04D 05D 06D 07D 08D 09D 10D 11D 12D 13D 14D 15D 16D 17D 18D
Hallway
C 01C 02C 03C 04C 05C 06C 07C 08C 09C 10C 11C 12C 13C 14C 15C 16C 17C 18C
B 01B 02B 03B 04B 05B 06B 07B 08B 09B 10B 11B 12B 13B 14B 15B 16B 17B 18B
A 01A 02A 03A 04A 05A 06A 07A 08A 09A 10A 11A 12A 13A 14A 15A 16A 17A 18A

Two seats are adjacent if and only if they are in the same row of the same carriage, on the same side of the aisle, and next to each other in that row's seating sequence.

You need to help Xiao R. judge sequentially:

  1. Whether the seats on the two tickets are in the same carriage.
  2. Whether the seats on both tickets are in the same row of the same carriage.
  3. Whether the seats on both tickets are in the same row of the same carriage and on the same side of the aisle.
  4. Whether the seats for the two tickets are adjacent.

II. Input Format

From filetravel.inRead data from input.

The first line contains four strings: c1, s1, c2, and s2, representing the carriage and seat numbers for Xiao R's ticket, and the carriage and seat numbers for Xiao X's ticket.

III. Output Format

Export to filetravel.outIn the middle.

Output a single line containing a 4-character string for the answers to four questions, where 1 represents "Yes" and 0 represents "No".

IV. Examples

Sample 1

codeCopy
02 01A 02 01B

Output:

codeCopy
1111

Explanation: Carriage 02 is a Second Class carriage. Seats 01A and 01B are in the same carriage, row, and side of the aisle, making them adjacent; therefore, output 1111.


Sample 2

codeCopy
02 01C 02 01A

Output:

codeCopy
1110

Explanation: The two seats are in the same carriage, same row, and on the same side of the aisle. However, 01C and 01A are not adjacent because 01B is between them. Therefore, the output is 1110.


Sample 3

codeCopy
02 01C 02 01D

Output:

codeCopy
1100

Explanation: Seats 1100 are in the same carriage and row, but seat C is on the left side of the aisle while seat D is on the right. Since they are not on the same side, they are not adjacent.

V. Data Scope

For all test data, the following applies:

  • c1 and c2 are two-digit car numbers between 01 and 17, excluding 01 and 16;
  • s1 and s2 are valid seat numbers in the corresponding carriage, where the first two digits represent row numbers ranging from 01 to 18, and the last character is one of A, B, C, D, or F.
  • The two tickets do not correspond to the same seat, meaning c1 != c2 or s1 != s2.
Test Point ID Special Properties
1 ~ 4 A: Two seats in the same car
5 ~ 10 B: Two seats in the same row
11 ~ 14 A: Two seats in the same car
15 ~ 17 D: Xiao R's seat is D, and Xiao X's seat is F.
18 ~ 20

VI. Problem-Solving Approach

The core of this question isString manipulation + conditional logicNo complex algorithms are needed; the key is to break down each of the four conditions in the problem and implement them accurately in code.

Step 1: Read input

The input consists of four strings representing Xiao R's carriage number and seat number, and Xiao X's carriage number and seat number. We usecinRead sequentially.

Step 2: Initialize Results

Use a string of length 4str = "0000"Store the answers; the four positions correspond to:

  • Position 1: Same carriage?
  • Position 2: Same row
  • Position 3: Same side of the aisle
  • Position 4: Are they adjacent?

All initial values are'0', change to when conditions are met'1'。

Step 3: Evaluate Layer by Layer

1. Determine if in the same carriage

Directly compare two carriage number strings for equality:if (str1 == str3)If they are not equal, the following three conditions cannot be met; output directly."0000"。

2. Check if they are in the same row

Seat number format is "two-digit row number + one-letter" (e.g.,03C), so we only need to check if the first two characters are identical:str2.substr(0, 2) == str4.substr(0, 2)。

3. Check if they are on the same side of the aisle.

This is the answer to this question.Key ChallengesSeat letters are divided into two groups:

  • Left:A, B, C (letters with ASCII values <= 'C')
  • Right:D, F (letters with ASCII value > 'C')

Only when the two seat lettersAll belong to the left side.orAll belong to the right side.only on the same side of the aisle.

4. Check if adjacent

Given that they are on the same side:

  • Left (A/B/C): Adjacent seat pairs are (A,B) and (B,C), with absolute ASCII difference of 1. Therefore, useabs(str2[2] - str4[2]) == 1Verify. Note that the difference between A and C is 2, so they are not adjacent and will be correctly excluded.
  • Right (D/F)Only seats D and F are available on the right. Since both are confirmed to be in the same row and on the same side, they must be adjacent (with no other seats between them). Mark as adjacent.

Step 4: Output Results

Print the result string.

Complexity Analysis

  • Time Complexity: O(1), with only a constant number of string and character comparisons.
  • Space complexity: O(1), using only a few string variables.

7. My Code

C++Copy
#include 
using namespace std;

int main() {
    string str1, str2, str3, str4;
    // Enter sequentially: Xiao R's car number, seat number; Xiao X's car number, seat number
    cin >> str1 >> str2 >> str3 >> str4;
    
    // Initial result is "0000", representing in order:
    // Position 1: Same carriage
    // Position 2: Same row
    // Position 3: Is on the same side of the aisle
    // Position 4: Is adjacent
    string str = "0000";
    
    // 1. Check if in the same car
    if (str1 == str3) {
        str[0] = '1';
        
        // 2. Check if in the same row (take the first two digits of the seat number; e.g., for "01A", extract "01")
        if (str2.substr(0, 2) == str4.substr(0, 2)) {
            str[1] = '1';
            
            // 3. Check if on the same side of the aisle
            // Left side: A, B, C
            if (str2[2] <= 'C' && str4[2] <= 'C') {
                str[2] = '1';
                // Left adjacent pair: (A,B) or (B,C), with an absolute ASCII difference of 1
                if (abs(str2[2] - str4[2]) == 1) {
                    str[3] = '1';
                }
            }
            
            // Right side: D, F
            if (str2[2] > 'C' && str4[2] > 'C') {
                str[2] = '1';
                Since there are only seats D and F on the right side, any two seats in the same row on that side will necessarily be adjacent.
                str[3] = '1'; 
            }
        }
    }
    
    // Output final judgment result
    cout << str;
    
    return 0;
}

VIII. Summary

This is a typicalTrue/False QuestionThis question tests understanding of the problem conditions and basic string operations. The key to solving it lies in:

  1. Layered judgment: The four questions are interdependent. First, check if they are in the same carriage; then, within that condition, check if they are in the same row; finally, within that condition, determine if they are on the same side and adjacent. Use nestedifThe statement can naturally express this dependency.

  2. Leverage ASCII codesThe ASCII codes for seats A, B, and C on the left are consecutive (65, 66, 67), so checking if the absolute difference equals 1 efficiently determines adjacency.

  3. Uniqueness of the Right SideOn the right side, only seats D and F are available. Once both passengers are confirmed for the same row and side (both on the right), they will necessarily be adjacent—no additional check is needed. This is an optimization point often overlooked.

  4. Edge casesNote that although A and C are both on the left, they are not adjacent because B is between them (difference: 2). The code handles this by...abs() == 1This case was automatically excluded.

Overall, this problem is of moderate difficulty and emphasizes carefulness and clear logic. If you correctly implement each condition check, you'll pass all test cases.

Yu ICP Bei 2026040576 No. -1