[TIOJ] 1990. 冰塊圖

題目連結:http://tioj.infor.org/problems/1990
每次都照著換就太慢了,所以不妨做個映射$col[i]=k$代表換完後的第$i$個欄其實是原本的第$k$欄,列也是用同樣的方法,這樣的話交換$i, j$就只要$swap(col[i], col[j])$了。
#include <bits/stdc++.h>
using namespace std;
const int N = 1000000 + 5;

string mtx[N];
int col[N], row[N];

int main(){
	ios_base::sync_with_stdio(0);cin.tie(0);
	int n, m; cin>>n>>m;
	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++) cin>>mtx[i*m+j];
	}
	for(int i=0;i<n;i++) col[i]=i;
	for(int i=0;i<m;i++) row[i]=i;
	int q; cin>>q;
	while(q--){
		char type; cin>>type;
		if(type=='S'){
			int x1, y1, x2, y2; cin>>x1>>y1>>x2>>y2;
			x1--, y1--, x2--, y2--;
			swap(mtx[col[x1]*m+row[y1]], mtx[col[x2]*m+row[y2]]);
		}else if(type=='C'){
			int a, b; cin>>a>>b;
			a--, b--;
			swap(row[a], row[b]);
		}else if(type=='R'){
			int a, b; cin>>a>>b;
			a--, b--;
			swap(col[a], col[b]);
		}
	}
	for(int i=0;i<n;i++) for(int j=0;j<m;j++){
		cout<<mtx[col[i]*m+row[j]]<<" \n"[j==m-1];
	}
	return 0;
}

留言

這個網誌中的熱門文章

[TIOJ] 1902. 「殿仁.王,不認識,誰啊?」,然後他就死了……

[NPSC] 2009初賽 E. 檸檬汽水傳說

[AtCoder] [經典競程 90 題] 024 - Select +/- One(★2)