提出 #49299739


ソースコード 拡げる

#include<bits/stdc++.h>
#define int long long 
using namespace std;
inline int read(){
	int x=0;
	char ch=getchar();
	while(ch<'0'||ch>'9'){
		ch=getchar();
	}
	while(ch>='0'&&ch<='9'){
		x=x*10+(ch^48);
		ch=getchar(); 
	}
	return x;
}
const int p1=998244353,p2=1e9+7,N=131,INF=1e9;
int n,m,ans=INF,a[10][10],b[10][10];
map<pair<int,int>,int> mp,vis; 
vector<int> vec;
inline pair<int,int> calc(){
	int s1=0,s2=0;
	for(auto x:vec){
		s1=(s1*N+x)%p1;
		s2=(s2*N+x)%p2;
	}
	return make_pair(s1,s2);
}
inline void rotate(int x,int y){
	for(int i=1;i<n;i++){
		for(int j=1;j<m;j++){
			int ti=(n-i),tj=(m-j);
			if((i*m+j)<=(ti*m+tj)) continue;
			swap(a[i+x][j+y],a[ti+x][tj+y]);
		}
	}
	return;
} 
void dfs1(int x){
	if(x>10) return;
	vec.clear();
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++) vec.push_back(a[i][j]); 
	}
	pair<int,int> N=calc();
	if(!vis[N]){
		vis[N]=1,mp[N]=x;
	}else{
		mp[N]=min(mp[N],x);
	}
	for(int i=0;i<=1;i++){
		for(int j=0;j<=1;j++){
			rotate(i,j);
			dfs1(x+1);
			rotate(i,j);
		}
	}
	return;
}
void dfs2(int x){
	if(x>10) return;
	vec.clear();
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++) vec.push_back(a[i][j]);
	}
	pair<int,int> N=calc();
	if(vis[N]) ans=min(ans,x+mp[N]);
	for(int i=0;i<=1;i++){
		for(int j=0;j<=1;j++){
			rotate(i,j);
			dfs2(x+1);
			rotate(i,j);
		}
	}
	return;
}
signed main(){
	n=read(),m=read();
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			a[i][j]=read();
		}
	}
	dfs1(0);
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			a[i][j]=(i-1)*m+j;
		}
	}
	dfs2(0);
	printf("%lld\n",(ans==INF)?-1:ans);
	return 0;
} 

提出情報

提出日時
問題 F - Rotation Puzzle
ユーザ wrhaco
言語 C++ 20 (gcc 12.2)
得点 550
コード長 1674 Byte
結果 AC
実行時間 2628 ms
メモリ 25932 KiB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 550 / 550
結果
AC × 4
AC × 45
セット名 テストケース
Sample example_00.txt, example_01.txt, example_02.txt, example_03.txt
All example_00.txt, example_01.txt, example_02.txt, example_03.txt, hand_00.txt, hand_01.txt, hand_02.txt, hand_03.txt, hand_04.txt, random_00.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt, random_20.txt, random_21.txt, random_22.txt, random_23.txt, random_24.txt, random_25.txt, random_26.txt, random_27.txt, random_28.txt, random_29.txt, random_30.txt, random_31.txt, random_32.txt, random_33.txt, random_34.txt, random_35.txt
ケース名 結果 実行時間 メモリ
example_00.txt AC 459 ms 3936 KiB
example_01.txt AC 1313 ms 23032 KiB
example_02.txt AC 1305 ms 23076 KiB
example_03.txt AC 804 ms 9952 KiB
hand_00.txt AC 456 ms 3776 KiB
hand_01.txt AC 2628 ms 25884 KiB
hand_02.txt AC 1860 ms 18080 KiB
hand_03.txt AC 2358 ms 18344 KiB
hand_04.txt AC 2124 ms 18428 KiB
random_00.txt AC 2625 ms 25836 KiB
random_01.txt AC 2398 ms 25704 KiB
random_02.txt AC 2404 ms 25704 KiB
random_03.txt AC 2186 ms 25688 KiB
random_04.txt AC 423 ms 3976 KiB
random_05.txt AC 1147 ms 16396 KiB
random_06.txt AC 2618 ms 25768 KiB
random_07.txt AC 2385 ms 25908 KiB
random_08.txt AC 2392 ms 25712 KiB
random_09.txt AC 2179 ms 25680 KiB
random_10.txt AC 1033 ms 16296 KiB
random_11.txt AC 1244 ms 16392 KiB
random_12.txt AC 2616 ms 25912 KiB
random_13.txt AC 2395 ms 25884 KiB
random_14.txt AC 2394 ms 25720 KiB
random_15.txt AC 2180 ms 25616 KiB
random_16.txt AC 1873 ms 25192 KiB
random_17.txt AC 1941 ms 25524 KiB
random_18.txt AC 2609 ms 25780 KiB
random_19.txt AC 2382 ms 25740 KiB
random_20.txt AC 2378 ms 25668 KiB
random_21.txt AC 2165 ms 25632 KiB
random_22.txt AC 1778 ms 25552 KiB
random_23.txt AC 1238 ms 16396 KiB
random_24.txt AC 2628 ms 25684 KiB
random_25.txt AC 2401 ms 25648 KiB
random_26.txt AC 2379 ms 25736 KiB
random_27.txt AC 2172 ms 25876 KiB
random_28.txt AC 1026 ms 16232 KiB
random_29.txt AC 1930 ms 25612 KiB
random_30.txt AC 2626 ms 25932 KiB
random_31.txt AC 2412 ms 25708 KiB
random_32.txt AC 2389 ms 25704 KiB
random_33.txt AC 2176 ms 25688 KiB
random_34.txt AC 2427 ms 23292 KiB
random_35.txt AC 1213 ms 22704 KiB