[TIOJ] 1994. 冰塊線

題目連結:http://tioj.infor.org/problems/1994
就照著做,分成左上左下右上右下四塊遞迴下去,但是很噁心,要判一些case QQ,賽中寫了2hr,然後就沒時間寫其他題了
#include <bits/stdc++.h>
using namespace std;
const int N = 1<<11;
const int A=0, B=1, C=2, D=3, E=4;

int n;
int ans[N][N];
void dfs(int,int,int,int,int,int);

int main(){
	ios_base::sync_with_stdio(0);cin.tie(0);
	cin>>n;
	int sz = 1<<n;
	dfs(0, 0, sz, sz, 0, B);
	for(int i=0;i<sz;i++) for(int j=0;j<sz;j++){
		cout<<ans[i][j]<<" \n"[j==sz-1];
	}
	return 0;
}

void dfs(int l, int u, int r, int d, int x, int f){
	if(r-l==1){
		ans[l][u]=x;
		return;
	}
	int hal = (r-l)>>1;
	if(f==A){
		dfs(l, u, r-hal, d-hal, x, B);
		dfs(l+hal, u, r, d-hal, x+hal*hal*3, E);
		dfs(l+hal, u+hal, r, d, x+hal*hal*2, A);
		dfs(l, u+hal, r-hal, d, x+hal*hal*1, A);
	}else if(f==D){
		dfs(l, u, r-hal, d-hal, x+hal*hal*2, D);
		dfs(l+hal, u, r, d-hal, x+hal*hal, D);
		dfs(l+hal, u+hal, r, d, x, E);
		dfs(l, u+hal, r-hal, d, x+hal*hal*3, B);
	}else if(f==E){
		dfs(l, u, r-hal, d-hal, x+hal*hal*2, E);
		dfs(l+hal, u, r, d-hal, x+hal*hal*3, A);
		dfs(l+hal, u+hal, r, d, x, D);
		dfs(l, u+hal, r-hal, d, x+hal*hal*1, E);
	}else{
		dfs(l, u, r-hal, d-hal, x, A);
		dfs(l+hal, u, r, d-hal, x+hal*hal, B);
		dfs(l+hal, u+hal, r, d, x+hal*hal*2, C);
		dfs(l, u+hal, r-hal, d, x+hal*hal*3, D);
	}
}

留言

這個網誌中的熱門文章

[TIOJ] 1094. C.幼稚國王的獎賞

[Codeforces] 731F. Video Cards

[IOJ] 19. 啦啦啦