結果

問題 No.228 ゆきこちゃんの 15 パズル
ユーザー syoken_desukasyoken_desuka
提出日時 2015-07-03 00:29:34
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,850 bytes
コンパイル時間 611 ms
コンパイル使用メモリ 82,748 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-22 05:48:14
合計ジャッジ時間 1,695 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp:45:8: warning: extra tokens at end of #endif directive [-Wendif-labels]
 #endif _DEBUG
        ^~~~~~

ソースコード

diff #

#include <algorithm>
#include <array>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include<complex>
#include <cmath>
#include <iostream>
#include <fstream>
#include <queue>
#include <list>
#include <map>
#include <numeric>
#include <set>
#include <sstream>
#include <string>
#include <vector>
#include <string>
using namespace std;
#define REP(i,a,n) for(int i=(a); i<(int)(n); i++)
#define rep(i,n) REP(i,0,n)
#define ALLOF(c) (c).begin(), (c).end()
#define IS_BETWEEN(x,a,b) (a <= x && x <= b)
typedef long long ll;

int mass[4][4];
bool canMove[4][4];
int vx[4] = {1,0,-1,0};
int vy[4] = {0,1,0,-1};
int remain = 16;

bool f(int x, int y)
{
    if (remain == 0)
    {
        return true;
    }
    rep(i,4)
    {
        if (IS_BETWEEN(x+vx[i],0,3) && IS_BETWEEN(y + vy[i],0,3) && canMove[x+ vx[i]][y+vy[i]] && (mass[x+vx[i]][y + vy[i]] == 4*x+y+1 ))
        {

#ifdef _DEBUG
            cout << (mass[x+vx[i]][y + vy[i]] - (4*x+y+1)) << endl;
#endif _DEBUG      

            swap(mass[x][y],mass[x+vx[i]][y+vy[i]]);
            canMove[x][y] = false;
            remain--;
            return f(x+vx[i],y+vy[i]);
        }
    }
    return false; // cant move
}

int main()
{
    int init_x = 0;
    int init_y = 0;
    rep(i,4)
        rep(j,4)
        canMove[i][j] = true;

    rep(i,4)
        rep(j,4)
        cin >> mass[i][j];

    rep(i,4)
    {
        rep(j,4)
        {
            if (mass[i][j] == 0 )
            {
                init_x = i;
                init_y = j;
            }
            if (mass[i][j] == 4 * i + j + 1)
            {
                remain--;
                canMove[i][j] = false;
            }
        }
    }
    remain--; // remove diff of 0 panel;

    if (f(init_x, init_y))
    {
        cout << "Yes" << endl;
    }
    else
    {
        cout << "No" << endl;
    }
    return 0;

}

0