Skip to content

Instantly share code, notes, and snippets.

@behitek
Created February 22, 2017 00:24
Show Gist options
  • Save behitek/15a02a1176ec051f95d0cc752ec348f6 to your computer and use it in GitHub Desktop.
Save behitek/15a02a1176ec051f95d0cc752ec348f6 to your computer and use it in GitHub Desktop.
LÁT GẠCH 2
package com.vtcc.antispam.hieunv.latgach;
import java.util.Arrays;
import java.util.Scanner;
/**
* Created by HieuNguyen on 02/21/2017.
*/
public class Main {
static int n, x, y, a[][], cnt;
static void push(int x, int y, int s, int t)
{
if (s == 2)
{
cnt ++;
if (t != 1) a[x][y] = cnt;
if (t != 2) a[x][y+1] = cnt;
if (t != 3) a[x+1][y] = cnt;
if (t != 4) a[x+1][y+1] = cnt;
}
else
{
if (t==1)
{
push(x, y+s/2, s/2, 3);
push(x+s/2, y+s/2, s/2, 1);
push(x+s/2, y, s/2, 2);
push(x+s/4, y+s/4, s/2, 1);
}
if (t==2)
{
push(x, y, s/2, 4);
push(x+s/2, y, s/2, 2);
push(x+s/2, y+s/2, s/2, 1);
push(x+s/4, y+s/4, s/2, 2);
}
if (t==3)
{
push(x, y, s/2, 4);
push(x, y+s/2, s/2, 3);
push(x+s/2, y+s/2, s/2, 1);
push(x+s/4, y+s/4, s/2, 3);
}
if (t==4)
{
push(x, y, s/2, 4);
push(x, y+s/2, s/2, 3);
push(x+s/2, y, s/2, 2);
push(x+s/4, y+s/4, s/2, 4);
}
}
}
static void build(int s, int x, int y, int h, int c)
{
if (s < 2) return ;
if (x < h+s/2)
{
if (y < c+s/2)
{
push(h, c, s, 1);
build(s/2, x, y, h, c);
}
else
{
push(h, c, s, 2);
build(s/2, x, y, h, c+s/2);
}
}
else
{
if (y < c+s/2)
{
push(h, c, s, 3);
build(s/2, x, y, h+s/2, c);
}
else
{
push(h, c, s, 4);
build(s/2, x, y, h+s/2, c+s/2);
}
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
x = sc.nextInt();
y = sc.nextInt();
a = new int[n+1][n+1];
build(n, x, y, 1, 1);
for (int i=1; i<=n; i++)
{
for (int j=1; j<= n; j++) System.out.print(a[i][j] + " ");
System.out.println();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment