Skip to content

Instantly share code, notes, and snippets.

@sat0b
Created May 21, 2017 16:36
Show Gist options
  • Select an option

  • Save sat0b/1c2b2a0353f4fc27a32c22400b17f1d3 to your computer and use it in GitHub Desktop.

Select an option

Save sat0b/1c2b2a0353f4fc27a32c22400b17f1d3 to your computer and use it in GitHub Desktop.
compile ...
=== input 3 ===
***
* *
* *
=== input 4 ===
****
* *
* *
* **
=== input 5 ===
*****
* *
* * *
* * *
* ***
=== input 6 ===
******
* *
* ** *
* * *
* * *
* ****
=== input 7 ===
*******
* *
* *** *
* * * *
* * * *
* * *
* *****
=== input 8 ===
********
* *
* **** *
* * * *
* * * *
* * ** *
* * *
* ******
=== input 9 ===
*********
* *
* ***** *
* * * *
* * * * *
* * * * *
* * *** *
* * *
* *******
=== input 10 ===
**********
* *
* ****** *
* * * *
* * ** * *
* * * * *
* * * * *
* * **** *
* * *
* ********
=== input 11 ===
***********
* *
* ******* *
* * * *
* * *** * *
* * * * * *
* * * * * *
* * * * *
* * ***** *
* * *
* *********
=== input 12 ===
************
* *
* ******** *
* * * *
* * **** * *
* * * * * *
* * * * * *
* * * ** * *
* * * * *
* * ****** *
* * *
* **********
=== input 13 ===
*************
* *
* ********* *
* * * *
* * ***** * *
* * * * * *
* * * * * * *
* * * * * * *
* * * *** * *
* * * * *
* * ******* *
* * *
* ***********
=== input 14 ===
**************
* *
* ********** *
* * * *
* * ****** * *
* * * * * *
* * * ** * * *
* * * * * * *
* * * * * * *
* * * **** * *
* * * * *
* * ******** *
* * *
* ************
=== input 15 ===
***************
* *
* *********** *
* * * *
* * ******* * *
* * * * * *
* * * *** * * *
* * * * * * * *
* * * * * * * *
* * * * * * *
* * * ***** * *
* * * * *
* * ********* *
* * *
* *************
=== input 16 ===
****************
* *
* ************ *
* * * *
* * ******** * *
* * * * * *
* * * **** * * *
* * * * * * * *
* * * * * * * *
* * * * ** * * *
* * * * * * *
* * * ****** * *
* * * * *
* * ********** *
* * *
* **************
Done!
#!/bin/bash
set -eu
echo "compile ..."
javac Uzumaki.java
for i in {3..16}; do
echo "=== input $i ==="
java Uzumaki $i
done
echo "Done!"
class Uzumaki {
public static void main(String[] args) {
int n = 0;
try {
n = Integer.parseInt(args[0]);
} catch (NumberFormatException e) {
System.err.println("Input Error : must be integer");
System.exit(1);
}
if (n < 3) {
System.err.println("Input Error : n >= 3 expected, but " + n);
System.exit(1);
}
Uzu uzu = new Uzu(n);
uzu.print();
}
}
class Uzu {
private int[][] uzu;
private int pos_x;
private int pos_y;
private int length;
private int cnt;
Uzu (int length) {
this.length = length;
uzu = new int[length][length];
pos_x = 0;
pos_y = length - 1;
uzu[pos_y][pos_x] = 1;
cnt = 0;
moveUp();
if (length % 2 == 0)
uzu[pos_y][pos_x] = 0;
}
private void moveUp() {
if (pos_y > 0)
if (uzu[pos_y-1][pos_x] == 1)
return;
if (pos_y > cnt) {
pos_y -= 1;
uzu[pos_y][pos_x] = 1;
moveUp();
} else
moveRight();
}
private void moveRight() {
if (pos_x < length - 1)
if (uzu[pos_y][pos_x+1] == 1)
return;
if (pos_x < length - cnt - 1) {
pos_x += 1;
uzu[pos_y][pos_x] = 1;
moveRight();
} else
moveDown();
}
private void moveDown() {
if (pos_y < length - 1)
if (uzu[pos_y+1][pos_x] == 1)
return;
if (pos_y < length - cnt - 1) {
pos_y += 1;
uzu[pos_y][pos_x] = 1;
moveDown();
} else
moveLeft();
}
private void moveLeft() {
if (pos_x > 0)
if (uzu[pos_y][pos_x-1] == 1)
return;
// counting
if (pos_x == cnt + 2 && pos_y == length - cnt - 1)
cnt += 2;
if (pos_x > cnt) {
pos_x -= 1;
uzu[pos_y][pos_x] = 1;
moveLeft();
} else
moveUp();
}
public void print() {
for (int i = 0; i < length; i++) {
for (int j = 0; j < length; j++) {
if (uzu[i][j] == 1)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment