洛谷提高组P5461赦免战俘(动态规划dp)

#include <stdio.h>
#include <math.h>
int poh(int m,int n){
	int s=1;
	for(int i=0;i<n;i++)
	s*=m;
	return s;
}
int main(){
	int n;
	scanf("%d",&n);
	int a[poh(2,n)+1][poh(2,n)+1];
	a[poh(2,n)-1][poh(2,n)-1]=0;
	a[poh(2,n)-1][poh(2,n)]=1;
	a[poh(2,n)][poh(2,n)-1]=1;
	a[poh(2,n)][poh(2,n)]=1;
	for(int i=1;i<n;i++){
		for(int j=1;j<=poh(2,i);j++){
		for(int k=1;k<=poh(2,i);k++){
			a[poh(2,n)-poh(2,i)+1-j][poh(2,n)-poh(2,i)+1-k]=0;
			a[poh(2,n)-poh(2,i)+1-j+poh(2,i)][poh(2,n)-poh(2,i)+1-k]=
			a[poh(2,n)-poh(2,i)+1-j+poh(2,i)][poh(2,n)-poh(2,i)+1-k+poh(2,i)];
			a[poh(2,n)-poh(2,i)+1-j][poh(2,n)-poh(2,i)+1-k+poh(2,i)]=
			a[poh(2,n)-poh(2,i)+1-j+poh(2,i)][poh(2,n)-poh(2,i)+1-k+poh(2,i)];
		}
		}
	}
	for(int i=1;i<=poh(2,n);i++){
		for(int j=1;j<=poh(2,n);j++){
			printf("%d ",a[i][j]);
		}printf("\n");
	}
	return 0;
}

ps:写poh函数是因为pow 的返回值为浮点型


版权声明:本文为qq_45410719原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。