15北京区域赛——构造——hihoCoder 1257 Snake Carper
注意是正的偶数。。不包括零
还有n=2的时候应该横着放
#include <cstdio> #include <algorithm> using namespace std; void dfs(int n) { if(n == 1){ printf("1 1\n"); return; } else if(n ==2){ printf("1 1\n"); printf("1 2 1 3\n"); return; } else if(n == 3){ printf("1 2\n"); printf("1 1 2 1\n"); printf("1 3 2 3 2 2\n"); return; } else if(n % 2 == 1){ dfs(n-3); int num = (n-3)*(n-2)/2; int col = (n-2)/2; int row = num/col; //n-2 for(int i = 1; i <= col; i++) printf("%d %d ", i, row+1); printf("%d %d ", col+1, row+1); for(int i = 1; i <= col; i++){ printf("%d %d ", col+1, row-i+1); } puts(""); //n-1 for(int i = 1; i <= (n-1)/2; i++) printf("%d %d ", col+1, i); for(int i = (n-1)/2; i >= 1; i--) printf("%d %d ", col+2, i); puts(""); //n for(int i = 1; i <= n/2; i++) printf("%d %d ", col+2, (n-1)/2+i); printf("%d %d ", col+2, row+2); for(int i = 1; i <= n/2; i++) printf("%d %d ", col+2-i, row+2); puts(""); } else { dfs(n-1); int num = (n-1)*n/2; int col = n/2; int row = num/col; for(int i = 1; i <= n/2; i++) printf("%d %d ", i, row+1); for(int i = n/2; i >= 1; i--) printf("%d %d ", i, row+2); puts(""); } } int main() { int n; while(~scanf("%d", &n)){ int xx, yy; if(n % 2 == 1){ xx = (n+1)/2; yy = n; } else { xx = n/2; yy = n+1; } printf("%d %d\n", xx, yy); dfs(n); } return 0; }