ZeroJudge L003: Chessboard

Xiaoming wants to play chess, but he doesn't have a chessboard. Therefore, he wants to record the coordinates and then present them as an image. Please write a program to help Xiaoming solve this problem.

The chessboard is 10x10 in size.

Sample Inputs/Outputs

Sample Input(s)Sample Output(s)
The input consists of N lines, terminated by EOF.
Format: (y value, x value)
y value represents the row number (vertical), and x value represents the column number (horizontal).
The output consists of ten lines, as shown in the example output. The numbers in the chessboard represent the steps.
(1,5)
(6,7)
(10,10)
(8,4)
(7,9)
—-1—–
———-
———-
———-
———-
——2—
——–5-
—4——
———-
———3
(1,1)
(3,7)
(2,8)
1———
——-3–
——2—
———-
———-
———-
———-
———-
———-
———-

Thought Process

After extracting the coordinates, use a Map to record the position of each coordinate. Then, iterate through a loop of 10x10. If the current (i, j) is stored in the Map and not equal to 0, output this Map value; otherwise, output '-'.

Sample Code-ZeroJudge L003: Chessboard

#include <iostream>
#include <map>
using namespace std;

pair<int, int> axis (int a, int b)
{
    pair<int, int>tmp;
    tmp.first = a;
    tmp.second = b;
    return tmp;
}

int main() {
    cin.sync_with_stdio(0);
    cin.tie(0);
    string str;
    int count = 1;
    map<pair<int, int>, int>MAP;
    while (cin >> str)
    {
        int a, b;
        if (str[2] != ',')
        {
            a = 10;
            if (str[5] != ')') b = 10;
            else b = int(str[4] - '0');
        }
        else
        {
            a = int(str[1] - '0');
            if (str[4] != ')') b = 10;
            else b = int(str[3] - '0');
        }
        MAP[axis(a, b)] = count;
        count++;
    }
    for (int i = 1; i<=10; i++)
    {
        for (int j = 1; j<=10; j++)
        {
            if (MAP[axis(i, j)] != 0) cout << MAP[axis(i, j)];
            else cout << "-";
        }
        cout << "\n";
    }
}

//ZeroJudge L003
//Dr. SeanXD

Comments