結果

問題 No.1598 4×4 Grid
ユーザー furafura
提出日時 2021-07-09 23:47:40
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 553 ms / 4,000 ms
コード長 908 bytes
コンパイル時間 2,169 ms
コンパイル使用メモリ 202,604 KB
実行使用メモリ 261,568 KB
最終ジャッジ日時 2023-09-14 11:14:39
合計ジャッジ時間 8,910 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 549 ms
261,568 KB
testcase_01 AC 550 ms
261,380 KB
testcase_02 AC 547 ms
261,340 KB
testcase_03 AC 553 ms
261,384 KB
testcase_04 AC 548 ms
261,484 KB
testcase_05 AC 547 ms
261,424 KB
testcase_06 AC 547 ms
261,228 KB
testcase_07 AC 545 ms
261,236 KB
testcase_08 AC 548 ms
261,496 KB
testcase_09 AC 551 ms
261,424 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;
using lint=long long;

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

inline int popcount(unsigned int x){
	x-=(x>>1)&0x55555555;
	x=(x&0x33333333)+((x>>2)&0x33333333);
	return ((x+(x>>4)&0x0f0f0f0f)*0x01010101)>>24;
}

int main(){
	vector dp(1<<16,vector(500,0LL));
	const int offset=250;
	dp[0][offset]=1;
	rep(S,1<<16){
		int val=popcount(S)+1;
		for(int score=-offset;score<offset;score++){
			if(dp[S][score+offset]==0) continue;

			rep(i,4) rep(j,4) if(~S>>(i*4+j)&1) {
				int score2=score;
				rep(k,4){
					int x=i+dx[k],y=j+dy[k];
					if(0<=x && x<4 && 0<=y && y<4){
						if(S>>(x*4+y)&1) score2+=val;
						else             score2-=val;
					}
				}
				dp[S|1<<(i*4+j)][score2+offset]+=dp[S][score+offset];
			}
		}
	}

	int tar; scanf("%d",&tar);
	printf("%lld\n",dp[(1<<16)-1][tar+offset]);

	return 0;
}
0