日期:2014-05-18 浏览次数:20907 次
#include "cstdio" // stdio.h #include "cstdlib" // stdlib.h int r(int a, int b, int N) { return a==1? b : (b==1 || b==N || a==N) ? r(N-b+1,a,N)+N-1 : r(a-1,b-1,N-2)+(N-1)*4; } int main() { const int N = 10; //scanf("%d", &N); for (int i=1; i<=N; i++, printf("\n")) for (int j=1; j<=N; j++) { printf("%d ", r(i, j, N)); } return 0; }
#include "cstdio" // stdio.h #include "cstdlib" // stdlib.h int main() { //scanf("%d", &N); const int N = 8; for (int i=1; i<=N; i++, printf("\n")) for (int j=1; j<=N; j++) { int a=i, b=j; int z[] = {a-1, b-1, N-a, N-b}; int reduce = N+1; for (int k=0; k<4; k++) if (z[k] < reduce) reduce = z[k]; int ring = N - reduce*2; // = a,b is at a ring with size = ring int ans = (N * reduce - reduce * reduce) * 4; // = (N-1)*4 + (N-3)*4 + ... (ring-1) * 4 int quaring = (ring - 1); // = 1/4 perimeter of ring int t; a-=reduce, b-=reduce; while (a != 1) { ans += quaring; t=a; a=ring-b+1; b=t;} // rotate until a = 1 printf("%d ", ans+b); } return 0; }