解題思路
先把固定票券的5橫列、5直行與2條大對角線存成12個號碼集合。讀每行已抽球,檢查整數範圍、遞增且不重複的格式。
對每條線求尚未抽出的號碼集合;只有恰剩1個時,下一顆抽該號才會新增這條線,替此號的得分加1。已完成或至少缺2個的線都不貢獻。
取最高得分的全部球號升序輸出;最高為0或格式不符時輸出−1。每條線對其唯一候選只加一次,因此候選得分就是精確新增線數。
每行最多75號,時間O(75+12×5),額外空間O(75)。
Step-by-step animation
只差一格的賓果線|已抽33,55,3,64,第一列只缺5。
步驟 1 / 3
第一列
已抽33,55,3,64,第一列只缺5。
33
55
3
64
缺5
- 5得分
- 1
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
string line;
getline(cin, line);
int board[5][5] = {{33, 55, 3, 64, 5},
{26, 7, 18, 19, 10},
{11, 12, 13, 14, 15},
{16, 17, 8, 70, 20},
{9, 32, 23, 24, 25}};
vector<vector<int>> lines;
for (int i = 0; i < 5; i++) {
vector<int> r, c;
for (int j = 0; j < 5; j++) {
r.push_back(board[i][j]);
c.push_back(board[j][i]);
}
lines.push_back(r);
lines.push_back(c);
}
vector<int> d, e;
for (int i = 0; i < 5; i++) {
d.push_back(board[i][i]);
e.push_back(board[i][4 - i]);
}
lines.push_back(d);
lines.push_back(e);
while (n--) {
getline(cin, line);
istringstream in(line);
set<int> seen;
bool ok = true;
int last = 0, x;
while (in >> x) {
if (x < 1 || x > 75 || x <= last)
ok = false;
last = x;
seen.insert(x);
}
if (!in.eof())
ok = false;
int score[76] = {};
if (ok)
for (auto &r : lines) {
int missing = 0, v = 0;
for (int x : r)
if (!seen.count(x)) {
missing++;
v = x;
}
if (missing == 1)
score[v]++;
}
int best = *max_element(score, score + 76);
if (!best) {
cout << "-1\n";
continue;
}
bool first = true;
for (int x = 1; x <= 75; x++)
if (score[x] == best) {
cout << (first ? "" : " ") << x;
first = false;
}
cout << "\n";
}
}
先試著自己完成,再切換語言比對解法。參考程式可複製到題目編輯器。