結果
| 問題 |
No.585 工夫のないパズル
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2017-11-02 14:19:16 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 13,023 bytes |
| コンパイル時間 | 953 ms |
| コンパイル使用メモリ | 89,792 KB |
| 実行使用メモリ | 10,496 KB |
| 最終ジャッジ日時 | 2024-11-22 13:40:07 |
| 合計ジャッジ時間 | 26,048 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 6 TLE * 6 |
ソースコード
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// [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・・・無駄な労力返せ〜
//
//3. ということでようやく本題のIDA*探索へ
//
////////////////////////////////////////
//#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 ll INF_LL = (ll)9e18-1LL; //Be careful for overflow.
const ull INF_ULL = (ull)1e19-1ULL;
const int NONE = -1;
const ll MOD = (ll)1e9+7; //大きい素数の代表といえばこの人、10億7さん
const int N_MAX = 3010; //num of vertex or element
const int M_MAX = 124760; //num of edge
const int 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 3 //過去H手までさかのぼって打ち消さないかチェックする
int min_solve_depth; //一番最初に解を見つけたときのdepth。ID-DFSなので、これすなわち最小手数となる
struct PUZZLE {
string state;
int md; //Manhattan Distance
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) {
//同じ行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]) {
#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(limit);
dout << "limit over\n";
#endif
return false;
}
//現在の状態から1手で実現できる状態を列挙する
//その際、引数で与えられた直近の3手を打ち消すような手は行わないように注意する
PUZZLE tmp = currentState; //track back用に現在の状態を保存しておく
rep(k,0,K_MAX) {
#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++) {
//track back to initial state
currentState = initialState;
int noMoveHistory[H];
rep(h,0,H) noMoveHistory[h] = NONE;
if( dfs(0, noMoveHistory) ) { //solved!!
return min_solve_depth;
}
}
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, ss;
rep(i,0,N) {
din >> s;
ss += s;
}
//------------------------------------------------------------------------------------------
#ifdef debug
//start timer
auto startTime = chrono::system_clock::now();
#endif
//------------------------------------------------------------------------------------------
PUZZLE puzzle;
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,ans) {
dout << dir[path[i]] << " " << pos[path[i]] << " " << dif[path[i]] << endl;
}
}
else {
dout << "UNSOLVABLE!!!\n";
}
//------------------------------------------------------------------------------------------
#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;
}