# 題目: UVa 11360 - Have Fun with Matrices
# 題目說明
給一個n * n的矩陣,有以下5種指令可以控制矩陣變換,輸出執行完指令的矩陣
row a b: 交換row a與row bcol a b: 交換col a與col binc: 將矩陣所有值+1後取10的餘數dec: 將矩陣所有值-1後取10的餘數transpose: 對矩陣進行轉置
INPUT:
第一行輸入一個整數t,代表測資數
每筆測資先輸入一個整數n,代表矩陣的大小
接下來輸入n * n個整數,代表矩陣的值
之後輸入一個整數m,代表指令數
接下來輸入m個字串
OUTPUT:
輸出執行完指令的矩陣
# 解題方法
先將矩陣的值存入二維陣列
之後隨著指令及時更動矩陣內的值
最後再將結果輸出即可
# 參考程式碼
#include <iostream>
#include <vector>
using namespace std;
static auto fast_io = []
{
ios::sync_with_stdio(false);
cout.tie(nullptr);
cin.tie(nullptr);
return 0;
}();
int main()
{
int t, n, m, a, b, Case = 0;
string str;
cin >> t;
while (t--)
{
cin >> n;
vector<vector<int>> V(n, vector<int>(n));
for (int i = 0, tmp; i < n; ++i)
{
cin >> tmp;
for (int j = n - 1; j >= 0; --j)
{
V[i][j] = tmp % 10;
tmp /= 10;
}
}
cin >> m;
while (m--)
{
cin >> str;
if (str == "row")
{
cin >> a >> b;
for (int i = 0; i < n; ++i)
swap(V[a - 1][i], V[b - 1][i]);
}
else if (str == "col")
{
cin >> a >> b;
for (int i = 0; i < n; ++i)
swap(V[i][a - 1], V[i][b - 1]);
}
else if (str == "inc")
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
V[i][j] = (V[i][j] + 1) % 10;
else if (str == "dec")
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
V[i][j] = (V[i][j] + 9) % 10;
else
for (int i = 0; i < n; ++i)
for (int j = i + 1; j < n; ++j)
swap(V[i][j], V[j][i]);
}
cout << "Case #" << ++Case << "\n";
for (auto& i : V)
{
for (auto& j : i) cout << j;
cout << "\n";
}
cout << "\n";
}
}