結果

問題 No.228 ゆきこちゃんの 15 パズル
ユーザー itezpaceitezpace
提出日時 2016-09-15 08:53:11
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,278 bytes
コンパイル時間 587 ms
コンパイル使用メモリ 52,860 KB
実行使用メモリ 4,388 KB
最終ジャッジ日時 2023-08-10 20:34:21
合計ジャッジ時間 1,600 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,388 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:63:10: warning: ‘w’ may be used uninitialized in this function [-Wmaybe-uninitialized]
   if(calc(h,w,ary2,ary3)){
      ~~~~^~~~~~~~~~~~~~~
main.cpp:63:10: warning: ‘h’ may be used uninitialized in this function [-Wmaybe-uninitialized]

ソースコード

diff #

#include <iostream>
using namespace std;

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

int calc(int h,int w,int ary2[][4],int ary3[]){
  int ret=0;
  int f=0;
  for(int i=0; i<4; ++i){
    for(int j=0; j<4; ++j){
      if(ary1[i][j]!=ary2[i][j]){
        f=1;
        break;
      }
    }
  }
  if(f==1){
    for(int i=0; i<4; ++i){
      int h2=h+dh[i];
      int w2=w+dw[i];
      if(h2<0 || h2>3 || w2<0 || w2>3) continue;
      if(ary3[ary2[h2][w2]]==1) continue;
      int ary4[16];
      for(int i=0; i<16; ++i){
        ary4[i]=ary3[i];
      }
      ary4[ary2[h2][w2]]=1;
      int ary5[4][4];
      for(int i2=0; i2<4; ++i2){
        for(int j2=0; j2<4; ++j2){
          ary5[i2][j2]=ary2[i2][j2];
        }
      }
      ary5[h][w]=ary5[h2][w2];
      ary5[h2][w2]=0;
      ret+=calc(h2,w2,ary5,ary4);
    }
  } else{
    ret+=1;
  }
  return ret;
}

int main(){
  int ary2[4][4];
  int a,h,w;
  for(int i=0; i<4; ++i){
    for(int j=0; j<4; ++j){
      cin>>a;
      ary2[i][j]=a;
      if(a==0){
        h=i;
        w=j;
      }
    }
  }
  int ary3[16];
  for(int i=0; i<16; ++i){
    ary3[i]=0;
  }
  if(calc(h,w,ary2,ary3)){
    cout<<"Yes"<<endl;
  } else {
    cout<<"No"<<endl;
  }
  return 0;
}
0