Pagini recente » Cod sursa (job #2478623) | Cod sursa (job #2281788) | Cod sursa (job #860126) | Cod sursa (job #1794213) | Cod sursa (job #1054596)
#include <iostream>
#include <fstream>
using namespace std;
int hilbert(int order, int x, int y)
{
if(order == 1)
{
if(x == 1 && y == 1)
return 0;
if(x == 1 && y == 2)
return 1;
if(x == 2 && y == 2)
return 2;
if(x == 2 && y == 1)
return 3;
}
if(order > 1) {
int dimension = 1;
for(int i = 0; i < order; i++)
dimension *= 2;
// there are four cases
// first case
if((x <= (dimension/2)) && (y <= (dimension/2))) {
return hilbert(order - 1, y, x);
}
// second case
if((x <= (dimension/2)) && (y > (dimension/2))) {
return ((dimension / 2) * (dimension / 2) + hilbert(order - 1, x, y - dimension / 2));
}
// third case
if((x > (dimension/2)) && (y > (dimension/2))) {
return ((dimension / 2) * (dimension / 2) * 2 + hilbert(order - 1, x - dimension / 2, y - dimension / 2));
}
// fourth case
if((x > (dimension/2)) && (y <= (dimension/2))) {
return ((dimension / 2) * (dimension / 2) * 4 - 1- hilbert(order - 1, y, dimension - x + 1));
}
}
}
int main() {
int x, y, z;
ifstream fin ("fractal.in");
fin >> x;
fin >> y;
fin >> z;
fin.close();
ofstream fout ("fractal.out");
fout << hilbert(x, y, z);
fout.close();
return 0;
}