[TIOJ]1990. 冰塊圖

題目連結:http://tioj.infor.org/problems/1990

可以直接利用索引值交換,來取代陣列中的值的交換。
#include <iostream>
#include <algorithm>
#include <cmath>
#include <bitset>
#include <queue>
#include <vector>
#define lld long long
#define PB push_back
#define F first
#define S second
#define jizz cin.tie(0);ios_base::sync_with_stdio(0);
#define endl '\n'
using namespace std;
typedef pair<int,int> Pair;
string **s;
int r[1000006],c[1000006];
int main(){jizz
    int n,m;cin >> n >> m;
    s = new string*[n+1];
    for(int i = 1 ; i <= n ; i++){
        s[i] = new string[m+1];
        r[i] = i;
        for(int j = 1; j <= m ; j++){
            c[j] = j;
            cin >> s[i][j];
        }
    }
    int q;cin >>q;
    while(q--){
        char cc;cin >> cc;
        if(cc == 'S'){
            int x1,y1,x2,y2;cin >> x1 >> y1 >> x2 >> y2;
            swap(s[r[x1]][c[y1]],s[r[x2]][c[y2]]);
        }else if(cc == 'R'){
            int x,y;cin >> x >> y;
            swap(r[x],r[y]);
        }else{
            int x,y;cin >> x >> y;
            swap(c[x],c[y]);
        }
    }
    for(int i = 1; i <= n ; i++){
        for(int j = 1; j <= m ; j++){
            cout << s[r[i]][c[j]] << " \n"[j == m];
        }
    }
    return 0;
}

留言

這個網誌中的熱門文章

[TIOJ]1617. [Interactive] 中位數

[TIOJ]1337. 隕石

[TIOJ]1994. 冰塊線