結果

問題 No.228 ゆきこちゃんの 15 パズル
ユーザー ry0u_ydry0u_yd
提出日時 2015-09-06 19:48:10
言語 C++11
(gcc 11.4.0)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 2,423 bytes
コンパイル時間 923 ms
コンパイル使用メモリ 79,808 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-26 09:46:06
合計ジャッジ時間 1,593 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 5 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <algorithm>
#include <sstream>
#include <map>
#include <set>
#include <queue>

#define REP(i,k,n) for(int i=k;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 1<<30
#define pb push_back
#define mp make_pair

using namespace std;
typedef long long ll;
typedef pair<int,int> P;

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

int a[4][4] = { {1,2,3,4},
                {5,6,7,8},
                {9,10,11,12},
                {13,14,15,0} };

int main() {
    vector< vector<int> > v(4,vector<int>(4));
    rep(i,4) rep(j,4) cin >> v[i][j];

    bool flag = true;
    int res = 0;

    rep(i,4) {
        rep(j,4) {
            if(v[i][j] == 0) {
                res = abs(i-3);
                continue;
            }

            queue<pair<pair<int,int>,int > > que;
            que.push(mp(mp(i,j),0));

            bool used[5][5];
            memset(used,0,sizeof(used));
            used[i][j] = true;

            int cost = 0;
            while(que.size()) {
                P p = que.front().first;
                int cnt = que.front().second;
                que.pop();
                
                int y = p.first;
                int x = p.second;

                if(v[i][j] == a[y][x]) {
                    cost = cnt;
                    break;
                }

                used[y][x] = true;

                rep(k,4) {
                    int ny = y + dy[k];
                    int nx = x + dx[k];

                    if(0 <= ny && ny < 4 && 0 <= nx && nx < 4 && !used[ny][nx]) {
                        que.push(mp(mp(ny,nx),cnt+1));
                    }
                }
            }

            if(cost >= 2) {
                flag = false;
            }
        }
    }

    rep(i,4) {
        rep(j,4) {
            if(v[i][j] == 0) continue;
            REP(k,i,4) {
                if(k == i) {
                    REP(l,j+1,4) {
                        if(v[k][l] == 0) continue;
                        if(v[i][j] > v[k][l]) res++;
                    }
                } else {
                    rep(l,4) {
                        if(v[k][l] == 0) continue;
                        if(v[i][j] > v[k][l]) res++;
                    }
                }
            }
        }
    }


    if(flag && res%2 == 0) cout << "Yes" << endl;
    else cout << "No" << endl;

    return 0;
}
0