解法一
🔹 壓縮成一維陣列
翻轉陣列
1 | 2 | 3 |
---|---|---|
4 | 5 | 6 |
7 | 8 | 9 |
9 | 8 | 7 |
---|---|---|
6 | 5 | 4 |
3 | 2 | 1 |
發現依照 先 row 後 col 順序遍歷陣列的值
從 1 2 3 4 5 6 7 8 9
變成了 9 8 7 6 5 4 3 2 1
因此只需要判斷 二維陣列的遍歷 是不是 回文
✅ 完整代碼
評分結果(參考) : AC (2ms, 328KB)
#include <bits/stdc++.h>
using namespace std;
int n, m, nm;
bool forward(vector<int>& nums){
for(int i=0; i<nm/2; i++){
if(nums[i] != nums[nm-i-1]) return false;
}
return true;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
for(int i=0; i<t; i++){
cin >> n >> m;
nm = n*m;
vector<int> nums(nm);
for(int& num : nums) cin >> num;
if(forward(nums)) cout << "go forward\n";
else cout << "keep defending\n";
}
return 0;
}