結果

問題 No.585 工夫のないパズル
ユーザー manuginomanugino
提出日時 2017-11-03 09:49:09
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 16,714 bytes
コンパイル時間 650 ms
コンパイル使用メモリ 88,616 KB
実行使用メモリ 10,624 KB
最終ジャッジ日時 2024-05-02 04:36:48
合計ジャッジ時間 7,783 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
10,624 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 11 ms
5,376 KB
testcase_03 AC 360 ms
5,376 KB
testcase_04 TLE -
testcase_05 AC 485 ms
5,376 KB
testcase_06 WA -
testcase_07 AC 5 ms
5,376 KB
testcase_08 AC 16 ms
5,376 KB
testcase_09 AC 62 ms
5,376 KB
testcase_10 AC 78 ms
5,376 KB
testcase_11 AC 48 ms
5,376 KB
testcase_12 TLE -
testcase_13 -- -
testcase_14 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// [Tips]
// XCodeでのEOF入力はCtrl+D
// ¥はAlt+\
// ansは結構INTの範囲2,147,483,647を超えることがあるのでlong long使っておいたほうが良い
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////




////////////////////////////////////////
//yukicorder
//No.585 工夫のないパズル
//
//IDA*探索に挑戦したいが、まずは愚直に、昔8パズルでやったのを思い出して
//1. なにも考えず幅優先探索BFS
//結果:当然のごとくTLE・・・
//
//そういえば8パズルでは双方向探索で劇的に速くなったけど・・・
//2. せっかくだから双方向BFSまた実装してみるか、復習がてら。どうせTLEになるだろうとは思いつつ
//結果:それでも変わらず同じ問題でTLE・・・無駄な労力返せ〜
//
//ということでようやく本題のIDA*探索へ。大いに蟻本と渦巻本を参考にしながら
//3. 知識あり(ヒューリスティクスを使った) ID-DFS (Iterative Deepening Depth-First Search) である、ID-A*を実装
//結果:なんとダメでした。TLE。2のときより1問しか多く解けなかった。
//もっとフツーに攻略法ライクに解くんだってさ。これまた徒労でした。ちーん。。。
//
//3-a. 構造体PUZZLEの状態をstring stateで表現していたが、これを試しにchar配列にしてみると高速化されたりしないか実験
//なんとこれだけで大幅にACが増えた!!string使ってたのバカバカバカバカバカバカ!!
//86手の問題が解けて、96手の問題がTLEだったから、あとほんの少し高速化できればいけそうなんだけどなー。
//
//実例:
//まず以下が3.の状態でstringを使っている場合、
//FHLE
//MOIC
//DNPB
//AKGJ
//20
//R 0 2
//R 2 1
//R 3 1
//C 1 1
//R 2 1
//C 0 1
//R 3 3
//C 0 1
//R 0 3
//R 1 2
//R 2 2
//C 2 1
//R 1 1
//C 1 1
//R 2 2
//C 2 3
//R 2 1
//R 0 1
//C 2 2
//R 0 3
//numOfSearch = 318372
//0.0960 sec
//Program ended with exit code: 0
//
//そして以下がstateをchar配列に変えた場合であるが、めちゃめちゃ速くなった!!! 0.0960sec ===> 0.0300sec
//こういう構造体を自作するケースでstring使っちゃうとめっちゃ遅くなるんやなって実感できてたいへん勉強になりました。
//FHLE
//MOIC
//DNPB
//AKGJ
//20
//R 0 2
//R 2 1
//R 3 1
//C 1 1
//R 2 1
//C 0 1
//R 3 3
//C 0 1
//R 0 3
//R 1 2
//R 2 2
//C 2 1
//R 1 1
//C 1 1
//R 2 2
//C 2 3
//R 2 1
//R 0 1
//C 2 2
//R 0 3
//numOfSearch = 318372
//0.0300 sec
//Program ended with exit code: 0



//
//4. 知識あり(ヒューリティクスを使った) BFS (Breadth-First Search) である、A*を試しに実装してみる <== イマココ
//
////////////////////////////////////////


//#define debug //*******************************************************************************************************************************************
#ifdef debug
#include <chrono>
#endif

#include <iostream>
#include <algorithm> // next_permutation
#include <iomanip>
#include <cmath>
#include <vector>
#include <sstream>
#include <string>
#include <cstring> //memcpy
#include <cstdio>
#include <stack>
#include <queue>
#include <list>
#include <numeric> //accumulate
#include <map>
//#include <unordered_map> //hash func.
#include <fstream> //ifstream, ofstream
#include <iterator> //insert_iterator::inserter
#include <set>


//#define NDEBUG //If NDEBUG is defined before #include <cassert>, assert will be ignored. You had better define NDEBUG when u submit the code.
#include <cassert> //assert

using namespace std;


#define dout cout
//If u wanna output to a text file instead of standard output, plz define OUTPUTFILE.
//#define OUTPUTFILE "output.txt" //************************************************************
#ifdef OUTPUTFILE
#define dout outputfile
ofstream outputfile(OUTPUTFILE);
#define OutputFilePath "/Users/Nag/Documents/Prgm/Test/DerivedData/Test/Build/Products/Debug/output.txt"
#endif


#define din cin
//If u wanna input from a text file instead of standard input, plz define INPUTFROMTEXTFILEを.
//#define INPUTFILE "input.txt" //**************************************************************
#ifdef INPUTFILE
#define din inputfile
ifstream inputfile(INPUTFILE);
#endif

#define scand(A) scanf("%d", &(A))
#define scans(A) scanf("%s", (A))
#define printd(A) printf("%d\n", (A))
#define prints(A) printf("%s\n", (A))
#define disp(A) dout << #A << " = " << setw(3) << (A) << endl
#define disP(A) dout << setw(3) << (A) << " "
#define rep(i,a,n) for(int (i)=(a); (i)<(n); (i)++)
#define show(A,s,g) dout << #A << " = "; rep(j, (s), (g)) {disP(A[j]);} dout << endl
#define showi(A,s,g) dout << #A << " = "; rep(j, (s), (g)) {disP(j);} dout << endl


#define sign(x) ((x)>0)-((x)<0) //x<0: -1, x=0: 0, x>0: +1
#define p(i) ((i)/2)
#define l(i) ((i)*2)
#define r(i) ((i)*2+1)
#define sibling(i) (i^1) //the other sibling of i (ex. 16^1 = 17, 17^1 = 16)
#define isRightChild(i) (i&1) // ex. 16&1 = 0, 17&1 = 1
#define isLeftChild(i) (!(i&1)) // ex. 16&1 = 1, 17&1 = 0

int dx[] = {1, 0, -1,  0};
int dy[] = {0, 1,  0, -1};

typedef pair<int, int> ii;
typedef pair<ii, int> iii;
typedef vector<int> vi;
typedef long long ll;
typedef unsigned long long ull;

//const int INF = (1LL<<31)-1;
const int NONE = -1;
//const ll INF_LL = (ll)9e18-1LL; //Be careful for overflow.
//const ull INF_ULL = (ull)1e19-1ULL;
#define MOD 1000000007 //大きい素数の代表といえばこの人、10億7さん

#define N_MAX 3010 //num of vertex or element
#define M_MAX 124760 //num of edge
#define DATA_MAX 1010


#define N 4
#define N2 16

string GOAL = "ABCDEFGHIJKLMNOP";

//24 patterns of possible move
#define K_MAX 24
char dir[K_MAX] = {'R','R','R','R','R','R','R','R','R','R','R','R','C','C','C','C','C','C','C','C','C','C','C','C'}; //direction
int pos[K_MAX]  = { 0 , 0 , 0 , 1 , 1 , 1 , 2 , 2 , 2 , 3 , 3 , 3 , 0 , 0 , 0 , 1 , 1 , 1 , 2 , 2 , 2 , 3 , 3 , 3 }; //position
int dif[K_MAX]  = { 1 , 2 , 3 , 1 , 2 , 3 , 1 , 2 , 3 , 1 , 2 , 3 , 1 , 2 , 3 , 1 , 2 , 3 , 1 , 2 , 3 , 1 , 2 , 3 }; //difference


int MDT[N2][N2]; //Manhattan Distance Table


#define LIMIT_MAX 120 //ID-DFSの最大深さ制限 = 最長手数100手の問題らしいので、これくらいで制限
int limit; //ID-DFSの現在の深さ制限 < LIMIT_MAX
int path[LIMIT_MAX+1]; //ID-DFSで現在注目しているノードへのパス (パズルの初期状態から、現在注目しているパズルの状態までの手)

#define H 1 //過去H手までさかのぼって打ち消さないかチェックする

int min_solve_depth; //一番最初に解を見つけたときのdepth。ID-DFSなので、これすなわち最小手数となる
ll numOfSearch = 0;


struct PUZZLE {
//    string state;
    char state[N2+1];
    int md; //Manhattan Distance
    
    bool operator < (const PUZZLE &p) const { //for map
        rep(i,0,N2) {
            if( this->state[i]==p.state[i] ) continue;
            else return this->state[i] < p.state[i];
        }
        return false; //completely same
    }
    
//    bool isGoal() {
//        return this->state==GOAL;
//    }
    
    void display() {
        dout << "----\n";
        disp(this->state);
        disp(this->md);
        
        rep(i,0,N) {
            rep(j,0,N) {
                dout << this->state[i*N+j];
            }
            dout << endl;
        }
        dout << endl;
    }
    
    
    void rotate(int k) { //pをk番目 (k=0...23) の動かし方で1手動かして返す
                                            //同時にmdも更新する
        
        int start, last, jump;
        char tmp[N2];
        
        if(dir[k]=='R') {
            jump = 1;
            start = N * pos[k];
            last = start + (N-1)*jump; // = start + 3
        }
        else {
            assert(dir[k]=='C');
            
            jump = N;
            start = pos[k];
            last = start + (N-1)*jump; // = start + 12
        }
        
        
        //一旦tmpにdifだけずらしてコピーしておいて
        for(int i=start; i<=last; i+=jump) {
            int next = i + dif[k] * jump;
            if(next > last) next = i - (N - dif[k])*jump;
            
            tmp[next] = state[i];
            
            md -= MDT[i][state[i]-'A'];
        }
        
        //元の配列を更新する
        for(int i=start; i<=last; i+=jump) {
            state[i] = tmp[i];
            md += MDT[i][state[i]-'A'];
        }
        
    }
    
    
    static int getMD(PUZZLE p) {
        int sum = 0;
        
        rep(i,0,N2) {
            sum += MDT[i][p.state[i]-'A'];
        }
        
        return sum;
        
        
    }
};

PUZZLE currentState; //ID-DFSで現在注目しているノード (現在注目しているパズルの状態)



void init() {
    //calc. MDT
    rep(i,0,N2) {
        rep(j,0,N2) {
            
            //通常のマンハッタン距離
            MDT[i][j] = abs(i/N - j/N) + abs(i%N - j%N);
            
            //同じ行or列にあれば1手で移動できる
            //行も列も異なれば2手必要
//            MDT[i][j] = (i/N != j/N) + (i%N != j%N);
        }
    }
    
    
}



void display() {
#ifdef debug
    dout << "----\n";
    dout << "MDT[][] = \n";
    
    dout << " "; showi(j, 0, N2);
    rep(i,0,N2) {
        disP(i) << ":";
        rep(j,0,N2) {
            disP(MDT[i][j]);
        }
        dout << endl;
    }
    
#endif
}


bool dfs(int depth, int moveHistory[H]) {
    numOfSearch++;
    
#ifdef debug
    dout << "========================= dfs ( d=" << depth << ", 直近3手=" << moveHistory[0] /* << ", " << moveHistory[1] << ", " << moveHistory[2]*/ << " )\n";
    currentState.display();
#endif
    
    if( currentState.md==0 ) {
#ifdef debug
        dout << "SOLVED!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n";
#endif
        min_solve_depth = depth;
        return true;
    }
    
    if( depth + currentState.md > limit ) {
#ifdef debug
        disp(depth);
        disp(currentState.md);
        disp(depth + currentState.md);
        disp(limit);
        disp(depth + currentState.md > limit);
        dout << "limit over\n";
#endif
        return false;
    }
    
    
    //現在の状態から1手で実現できる状態を列挙する
    //その際、引数で与えられた直近の3手を打ち消すような手は行わないように注意する
    
    PUZZLE tmp = currentState; //track back用に現在の状態を保存しておく
    
    rep(k,0,K_MAX) {
        if(k%(N-1)!=0) continue; //+2, +3だけ動かすmoveは、結局+1 moveの繰り返しなので、ignoreする
        
#ifdef debug
        dout << "--------次の1手で実現できる状態の列挙\n";
        disp(k);
        disP(dir[k]);
        disP(pos[k]);
        disP(dif[k]);
        dout << endl;
#endif
        
        
        bool shouldIgnore = false;
        
        int difContinuouslyByMoveHistory = 0; //kと同じdir[]、pos[]を、過去3手で「連続で」どれくらい動かしたか?
        
        rep(h,0,H) {
            if( moveHistory[h]==NONE ) break;
            
            if( dir[k]!=dir[moveHistory[h]] or pos[k]!=pos[moveHistory[h]] ) break;
            difContinuouslyByMoveHistory += dif[moveHistory[h]];
            
            if( dif[k] + difContinuouslyByMoveHistory == N ) {
#ifdef debug
                dout << "直近の " << h+1 << " 手 = " << moveHistory[h] << "を打ち消す手なので、ignore\n";
                disP(dir[moveHistory[h]]);
                disP(pos[moveHistory[h]]);
                disP(dif[moveHistory[h]]);
#endif
                shouldIgnore = true;
                break;
            }
        }
        
        if(shouldIgnore) continue;
        
        //kが無視すべきでない手であれば
        currentState.rotate(k); //その手を実行し、
        
        //次の階層のdfsを呼び出す
        int newMoveHistory[H];
        newMoveHistory[0] = k;
        rep(h,1,H) newMoveHistory[h] = moveHistory[h-1];
        
        if(  dfs(depth+1, newMoveHistory ) ) {
            //配下からtrueが返ってきたら解けたということなので、
            path[depth] = k; //この手kを解として記録
            return true;
        }
        
        else {
            //配下で解けなかったら、back track
            currentState = tmp;
        }
        
    }
    
    
#ifdef debug
    dout << "すべての手を列挙しても解けなかったので、falseを返す\n";
    dout << "END OF ========================= dfs ( d=" << depth << ", 直近3手=" << moveHistory[0] << /*", " << moveHistory[1] << ", " << moveHistory[2] <<*/ " )\n";
#endif
    
    return false;
}




int solve_IterativeDeepening(PUZZLE initialState) { //最初に見つかった解法の手数 (ID-DFSなので、これは最小手数になっている) を返す、解けなればNONEを返す
    
    //初期状態のMD
    limit = initialState.md = PUZZLE::getMD(initialState);
    
#ifdef debug
    dout << "Initial State =\n";
    initialState.display();
#endif
    
    for(limit=initialState.md; limit < LIMIT_MAX; limit++) {
#ifdef debug
        dout << "//////////////////////////////////////////////////////////////////////////////////////////////////\n";
        dout << "limitを1つ増やして、また初期状態からDFSし直す\n";
        disp(limit);
        dout << "//////////////////////////////////////////////////////////////////////////////////////////////////\n";
#endif
        
        //track back to initial state
        currentState = initialState;
        
        int noMoveHistory[H];
        rep(h,0,H) noMoveHistory[h] = NONE;
        
        if( dfs(0, noMoveHistory) ) { //solved!!
            
            int ans = min_solve_depth;
            
            //optimize the solution, path[i] and min_solve_depth
            //+2, +3するmoveをskipしていたので、+1 moveが連続する箇所を、対応する+2 move or +3 moveに変換する
            rep(i,0,min_solve_depth-1) {
                if( dir[path[i]] == dir[path[i+1]] and pos[path[i]] == pos[path[i+1]]) {
                    path[i+1] += dif[path[i]];
                    path[i] = NONE;
                    ans--;
                }
            }
            
            
            return ans;
        }
    }
    
    return NONE;
}




int main() {
    
    //cin, coutの高速化  *注意:cinを使うなら全部cinで、scanfを使うなら全部scanfで統一するように!
    cin.tie(0); //cinとcoutの同期を切る
    ios::sync_with_stdio(false); //iostreamとstdioの同期を切る
    
    //read input data
//    scand(N);
    
    string s;
//    string ss;
    char ss[N2+1];
    rep(i,0,N) {
        din >> s;
//        ss += s;
        rep(j,0,N) {
            ss[i*N+j] = s[j];
        }
    }
    ss[N2] = '\0';
    
    
    //------------------------------------------------------------------------------------------
#ifdef debug
    //start timer
    auto startTime = chrono::system_clock::now();
#endif
    //------------------------------------------------------------------------------------------
    
    
    PUZZLE puzzle;
//    puzzle.state = ss;
    strcpy(puzzle.state, ss);
    
    init();
    display();
    
    
    int ans = solve_IterativeDeepening(puzzle);
    
    
    
    
#ifdef debug
    dout << "=== OUTPUT ===\n";
#endif
    
    if(ans!=NONE) {
        dout << ans << endl;
        
        rep(i,0,min_solve_depth) {
            if( path[i]==NONE ) continue;
            dout << dir[path[i]] << " " << pos[path[i]] << " " << dif[path[i]] << endl;
        }
    }
    else {
        dout << "UNSOLVABLE!!!\n";
    }

//    disp(numOfSearch);
    
    
    //------------------------------------------------------------------------------------------
#ifdef debug
    //stop timer
    auto endTime = chrono::system_clock::now();
    auto dur = endTime - startTime;
    auto msec = chrono::duration_cast<chrono::milliseconds>(dur).count();
    dout << fixed << setprecision(4) << (double)msec/1000 << " sec \n";
#endif
    //------------------------------------------------------------------------------------------
    
#ifdef INPUTFILE
    inputfile.close();
#endif
    
#ifdef OUTPUTFILE
    outputfile.close();
    cout << "\"" << OutputFilePath << "\"" << endl;
#endif
    
    return 0;
}
0