miércoles, 18 de abril de 2012

1178. Fractal, tju online judge

#include<iostream>
#include<cstdio>
#include<cmath>
#include<map>
#define db(a) \
cout << #a << " = " << a << endl
#define db2(a, b) \
cout << #a << " = " << a << " " << #b << " = " << b << endl
#define inf (1<<30)
#define foreach(it, m) \
for (typeof(m.begin()) it = m.begin(); it != m.end(); it++)
#define MAX 729
using namespace std;
char M[MAX][MAX];
void solve(int n, int v, int h) {
 if (n == 1) {
  M[v][h] = 'X';
 }
 else {
  solve(n - 1, v - pow(3., n - 2), h - pow(3., n - 2));
  solve(n - 1, v - pow(3., n - 2), h + pow(3., n - 2));
  solve(n - 1, v, h);
  solve(n - 1, v + pow(3., n - 2), h - pow(3., n - 2));
  solve(n - 1, v + pow(3., n - 2), h + pow(3., n - 2));
 } 
}
int main() {
 #ifdef dennisbot
  freopen("in.in", "r", stdin);
  freopen("ou.out", "w", stdout);
 #endif
 int n = 7;
 for (int i = 0; i < MAX; i++) {
  for (int j = 0; j < MAX; j++) {
   M[i][j] = ' ';
  }
 }
 string linea;
 solve(n, (int)pow(3., n - 1) / 2, (int)pow(3., n - 1) / 2);
 while (scanf("%d", &n) != EOF) {
  if (n == -1) break;
  for (int i = 0; i < pow(3., n - 1) ; i++) {
   linea = "";
   for (int j = 0; j < pow(3., n - 1); j++) {
    if (M[i][j] != 'X')
     linea += " ";
    else {
     printf("%sX", linea.c_str());
     linea = "";
    }
   }
   puts("");
  }
  puts("-");
 }
 return 0;
}

No hay comentarios:

Publicar un comentario