답안 #803654

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
803654 2023-08-03T05:17:40 Z 박상훈(#10101) Shifty Grid (CCO17_shifty) C++17
0 / 25
0 ms 212 KB
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

int n, m;

int a[101][101], b[101][101];
pair<int, int> pos[100100];
vector<array<int, 3>> Q;

void shiftR(int i, int c){
	assert(0 <= c && c < m);
	for (int j=0;j<m;j++) b[i][(j+c)%m] = a[i][j];
	for (int j=0;j<m;j++){
		a[i][j] = b[i][j];
		pos[a[i][j]] = {i, j};
	} 

	Q.push_back({1, i, c});
}

void shiftC(int j, int c){
	assert(0 <= c && c < n);
	for (int i=0;i<n;i++) b[(i+c)%n][j] = a[i][j];
	for (int i=0;i<n;i++){
		a[i][j] = b[i][j];
		pos[a[i][j]] = {i, j};
	}

	Q.push_back({2, j, c});
}

void solveR(int p){
	for (int j=0;j<m;j++){
		auto [cx, cy] = pos[p*m + j];
		if (cx==p && cy==j) continue;
		if (cx==p){
			shiftC(j, 1);
			shiftC(cy, 1);
			shiftR(p+1, (m+j-cy) % m);
			shiftC(j, n-1);
			shiftC(cy, n-1);
		}

		else if (cy==j){
			shiftR(cx, 1);
			shiftC(j, (n+cx-p) % n);
			shiftR(cx, m-1);
			shiftC(j, (n+p-cx) % n);
		}

		else{
			shiftC(j, (n+cx-p) % n);
			shiftR(cx, (m+j-cy) % m);
			shiftC(j, (n+p-cx) % n);
		}
	}

	// printf("ok\n");
	// for (int i=0;i<n;i++){
	// 	for (int j=0;j<m;j++) printf("%d ", a[i][j]);
	// 	printf("\n");
	// }
}

void swap2(int p, int q){
	shiftC(p, 1);
	shiftR(0, (m+q-p) % m);
	shiftC(q, 1);
	shiftR(0, (m+p-q) % m);
	shiftC(p, 1);
}

void sort2(){
	for (int j=0;j<m;j++){
		auto [cx, cy] = pos[m+j];
		if (cy==j) continue;
		swap2(j, cy);
	}
}

int main(){
	scanf("%d %d", &n, &m);
	for (int i=0;i<n;i++){
		for (int j=0;j<m;j++){
			scanf("%d", a[i]+j);
			pos[a[i][j]] = {i, j};
		} 
	}

	for (int i=0;i<n-1;i++) solveR(i);
	if (m==2){
		if (a[n-1][0] > a[n-1][1]) shiftR(n-1, 1);
	}
	else if (n==2) sort2();
	
	for (int i=0;i<n;i++){
		for (int j=0;j<m;j++) assert(a[i][j] == i*m + j);
	}

	// printf("ok done\n");
	// for (int i=0;i<n;i++){
	// 	for (int j=0;j<m;j++) printf("%d ", a[i][j]);
	// 	printf("\n");
	// }

	printf("%d\n", (int)Q.size());
	for (auto &[x, y, z]:Q) printf("%d %d %d\n", x, y, z);
}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:84:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   84 |  scanf("%d %d", &n, &m);
      |  ~~~~~^~~~~~~~~~~~~~~~~
Main.cpp:87:9: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   87 |    scanf("%d", a[i]+j);
      |    ~~~~~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 212 KB Integer parameter [name=j] equals to 0, violates the range [1, 4]
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 212 KB Integer parameter [name=j] equals to 0, violates the range [1, 4]
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 212 KB Integer parameter [name=j] equals to 0, violates the range [1, 4]
2 Halted 0 ms 0 KB -