2015年11月17日火曜日

151117(2)

C


Self-avoiding walk(2)

2 x n を調べた。
結果が2^(n - 1) (http://mathworld.wolfram.com/Self-AvoidingWalk.html)
となることを確かめよう。

#include <stdio.h>

// startがx, y、goalがw - 1, h - 1
int search(int x, int y, int w, int h, long long used, int depth){
 int cnt = 0;
 if (x < 0 || w <= x || y < 0 || h <= y || (used & (1LL << (x + y * w))) > 0) return 0;
 if (x == w - 1 && y == h - 1) return 1;
 used += 1LL << (x + y * w);
 cnt += search(x + 1, y, w, h, used, depth + 1);
 cnt += search(x - 1, y, w, h, used, depth + 1);
 cnt += search(x, y + 1, w, h, used, depth + 1);
 cnt += search(x, y - 1, w, h, used, depth + 1);
 used -= 1LL << (x + y * w);
 return cnt;
}

int main(void){
 int i;
 long long used;
 for (i = 1; i < 21; i++){
  used = 0LL;
  printf("%d\n", search(0, 0, 2, i, used, 1));
 }
 return 0;
}

出力結果
1
2
4
8
16
32
64
128
256
512
1024
2048
4096
8192
16384
32768
65536
131072
262144
524288

0 件のコメント:

コメントを投稿

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