2015年11月23日月曜日

151123(5)

C


Dyck path とSelf-avoiding walk の融合(5)

F(3m, 3) を出力してみる。

#include <stdio.h>

int triangular_number(int x, int y, int m, int n){
 int i;
 i = x - m * y + (m * (2 * n - y + 1) + 2) * y / 2;
 return i;
}

// startがx, y、goalの座標がmn, n
long long search(int x, int y, int m, int n, long long used){
 long long cnt = 0LL;
 int z;
 z = triangular_number(x, y, m, n);
 if (x < 0 || m * n + 1 <= x || y < 0 || m * y > x || (used & (1LL << z)) > 0) return 0;
 if (x == m * n && y == n) return 1;
 used += 1LL << z;
 cnt += search(x + 1, y, m, n, used);
 cnt += search(x - 1, y, m, n, used);
 cnt += search(x, y + 1, m, n, used);
 cnt += search(x, y - 1, m, n, used);
 used -= 1LL << z;
 return cnt;
}

int main(void){
 int m;
 int n = 3;
 long long used;
 for (m = 0; m < 11; m++){
  used = 0LL;
  printf("%lld\n", search(0, 0, m, n, used));
 }
 return 0;
}

出力結果
1
7
44
284
1872
12384
81856
540736
3571712
23592704
155842560

0 件のコメント:

コメントを投稿

注: コメントを投稿できるのは、このブログのメンバーだけです。