結果

問題 No.585 工夫のないパズル
ユーザー nmnmnmnmnmnmnmnmnmnmnmnmnmnm
提出日時 2017-09-11 00:39:23
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,802 bytes
コンパイル時間 835 ms
コンパイル使用メモリ 99,096 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-14 07:48:27
合計ジャッジ時間 1,891 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘void move(std::__cxx11::string&, char, ll, ll)’:
main.cpp:91:7: warning: ‘c3’ may be used uninitialized in this function [-Wmaybe-uninitialized]
   rotR(s,N-1,c3-c2);
   ~~~~^~~~~~~~~~~~~
main.cpp:90:7: warning: ‘c2’ may be used uninitialized in this function [-Wmaybe-uninitialized]
   rotC(s,c2,(N-1)-r2);
   ~~~~^~~~~~~~~~~~~~~
main.cpp:90:7: warning: ‘r2’ may be used uninitialized in this function [-Wmaybe-uninitialized]
main.cpp: In function ‘void toLeft(std::__cxx11::string&, char)’:
main.cpp:108:7: warning: ‘c’ may be used uninitialized in this function [-Wmaybe-uninitialized]
   rotC(s,c,-1);
   ~~~~^~~~~~~~
main.cpp:107:7: warning: ‘r’ may be used uninitialized in this function [-Wmaybe-uninitialized]
   rotR(s,r,-1);
   ~~~~^~~~~~~~
main.cpp: In function ‘int main()’:
main.cpp:139:7: warning: ‘c’ may be used uninitialized in this function [-Wmaybe-uninitialized]
   rotR(s,N-1,-c);
   ~~~~^~~~~~~~~~

ソースコード

diff #

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;

#define sz size()
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(c) (c).begin(), (c).end()
#define rep(i,a,b) for(ll i=(a);i<(b);++i)
#define per(i,a,b) for(ll i=b-1LL;i>=(a);--i)
#define clr(a, b) memset((a), (b) ,sizeof(a))
#define ctos(c) string(1,c)
#define print(x) cout<<#x<<" = "<<x<<endl;

#define MOD 1000000007
#define N 4

string A = "ABCDEFGHIJKLMNOP";

vector<pair<string,pair<ll,ll> > > ans;

// 指定した行rを右にa個だけRotate (-N<=a<=N)
void rotR(string &s, ll r, ll a){
  a *= -1;
  a += N;
  a %= N;
  if(a==1)ans.pb(mp("R",mp(r,3)));
  if(a==2)ans.pb(mp("R",mp(r,2)));
  if(a==3)ans.pb(mp("R",mp(r,1)));
  rep(i,0,a){
    rep(j,0,N-1){
      swap(s[r*N+j],s[r*N+j+1]);
    }
  }
}

// 指定した列cの下にa個だけRotate (-N<=a<=N)
void rotC(string &s, ll c, ll a){
  a *= -1;
  a += N;
  a %= N;
  if(a==1)ans.pb(mp("C",mp(c,3)));
  if(a==2)ans.pb(mp("C",mp(c,2)));
  if(a==3)ans.pb(mp("C",mp(c,1)));
  rep(i,0,a){
    rep(j,0,N-1){
      swap(s[j*N+c],s[(j+1)*N+c]);
    }
  }
}

// 最下段の整合性を無視することである文字を指定位置へ移動。
void move(string &s, char ch, ll r1, ll c1){
  ll r2,c2;
  rep(i,0,N*N){
    if(s[i]==ch){
      r2 = i/N;
      c2 = i%N;
    }
  }
  ll c3;
  rep(i,0,N){
    if(i!=c1&&i!=c2){
      c3 = i;
      break;
    }
  }
  rotC(s,c2,(N-1)-r2);
  rotR(s,N-1,c3-c2);
  rotC(s,c2,r2-(N-1));
  rotC(s,c1,(N-1)-r1);
  rotR(s,N-1,c1-c3);
  rotC(s,c1,r1-(N-1));
}

// 指定した文字を1つ左へ (右の文字と入れ替え) N=4に限る
void toLeft(string &s, char ch){
  ll r,c;
  rep(i,0,N*N){
    if(s[i]==ch){
      r = i/N;
      c = i%N;
    }
  }
  rotR(s,r,-1);
  rotC(s,c,-1);
  rotR(s,r,-1);
  rotC(s,c,1);
  rotR(s,r,-1);
  rotC(s,c,-1);
  rotR(s,r,-2);
  rotC(s,c,1);
}

int main() {
  string s;
  rep(i,0,4){
    string s1;
    cin>>s1;
    s += s1;
  }
  string s2 = s;
  sort(all(s2));
  if(s2!=A){
    cout << -1 << endl;
    return 0;
  }
  rep(i,0,N*(N-1)){
    move(s,A[i],i/N,i%N);
  }
  ll c;
  rep(i,0,N*N){
    if(s[i]==A[N*(N-1)]){
      c = i%N;
    }
  }
  rotR(s,N-1,-c);
  rep(i,1,N){
    char ch = A[N*(N-1)+i];
    per(j,i+1,N){
      if(s[N*(N-1)+j]==ch){
        toLeft(s,ch);
      }
    }
  }
  cout << ans.sz << endl;
  rep(i,0,ans.sz){
    cout << ans[i].fi << " " << ans[i].se.fi << " " << ans[i].se.se << endl;
  }
  return 0;
}
0