a368中等矩陣座標・旋轉與形狀檢查

APCS 2025/10:航空拍照圖 (ZeroJudge r489)

依序產生四種旋轉,僅比較尺寸相同的方向並計數相等像素。

前往題目練習 →回報教學錯誤時間 O(RC),空間 O(RC);旋轉與比較各至多四次。

這題在學什麼

固定 A,旋轉 B 即可涵蓋所有相對方向。順時針旋轉的映射為原 B[i][j] 放到新矩陣 [j][舊列數−1−i],因此新尺寸由 R×C 變成 C×R;連續做四次就回到原方向。

每個方向先檢查尺寸,尺寸不同的相似度直接是 0。尺寸相同時掃描對應位置,累計相等數 S。四種方向的分母都是 R×C,因此只要保留最大的 S,最後用整數除法 100×S/(R×C) 輸出百分比,等同先計每個方向再取最大。

Step-by-step animation

示意:A=[[1,2],[3,4]],B=[[3,1],[4,2]]

回報動畫錯誤
步驟 1 / 3

原方向

四個對應位置均不相同,匹配數 0。

A:1 2 / 3 4
B:3 1 / 4 2
角度
0°
S
0

Reference solution

回報程式錯誤

完整程式

#include <bits/stdc++.h>
using namespace std;
int main() {
    int r, c;
    cin >> r >> c;
    vector<vector<int>> a(r, vector<int>(c)), b = a;
    for (auto &row : a)
        for (int &x : row)
            cin >> x;
    for (auto &row : b)
        for (int &x : row)
            cin >> x;
    int best = 0;
    for (int turn = 0; turn < 4; ++turn) {
        int rows = b.size(), cols = b[0].size();
        if (rows == r && cols == c) {
            int same = 0;
            for (int i = 0; i < r; ++i)
                for (int j = 0; j < c; ++j)
                    same += (a[i][j] == b[i][j]);
            best = max(best, same);
        }
        vector<vector<int>> rotated(cols, vector<int>(rows));
        for (int i = 0; i < rows; ++i)
            for (int j = 0; j < cols; ++j)
                rotated[j][rows - 1 - i] = b[i][j];
        b = move(rotated);
    }
    cout << 100 * best / (r * c) << "%\n";
}

先試著自己完成,再切換語言比對解法。參考程式可複製到題目編輯器。

更多