Skip to content

Instantly share code, notes, and snippets.

@w00lf
Created September 2, 2013 14:19
Show Gist options
  • Select an option

  • Save w00lf/6413366 to your computer and use it in GitHub Desktop.

Select an option

Save w00lf/6413366 to your computer and use it in GitHub Desktop.
************Спираль*********** http://acmp.ru/index.asp?main=task&id_task=196
import java.io.*;
import java.math.BigInteger;
import java.util.*;
public class Main{
public static void main(String[] argv) throws IOException,Exception{
new Main().run();
}
public static StreamTokenizer sc ;
public static int nextInt() throws Exception{
sc.nextToken();
return (int)sc.nval;
}
public void run() throws IOException,Exception{
sc = new StreamTokenizer(new BufferedReader(new FileReader("input.txt")));
int n = nextInt();
char[] directions = {'r', 'd', 'l', 'u' };
char direction = directions[0];
int[][] arr = new int[n][n];
arr[0][0] = 1;
int x = 1;
int y = 0;
Boolean cont = true;
int count = 2;
int k = 0;
PrintWriter pw = new PrintWriter(new File("output.txt"));
if (n == 1) {
pw.print(1);
pw.close();
System.exit(0);
}
while(cont) {
switch (direction) {
case 'r':
for (int i = x; i < n; i++) {
if (arr[y][i] == 0) {
arr[y][i] = count++;
if (x+1 < n && arr[y][x+1] == 0) {
x++;
}
}else {
break;
}
}
if (y+1 < n) {
y++;
}
break;
case 'd':
for (int i = y; i < n; i++) {
if (arr[i][x] == 0) {
arr[i][x] = count++;
if (y +1 < n && arr[y+1][x] == 0) {
y++;
}
}else {
break;
}
}
if (x-1 >= 0) {
x--;
}
break;
case 'l':
for (int i=x; i >= 0; i--) {
if (arr[y][i] == 0) {
arr[y][i] = count++;
if (x-1 >= 0 && arr[y][x-1] == 0) {
x--;
}
}else {
break;
}
}
if (y-1 >= 0) {
y--;
}
break;
case 'u':
for (int i=y; i >= 0; i--) {
if (arr[i][x] == 0) {
arr[i][x] = count++;
if (y -1 >= 0 && arr[y-1][x] == 0) {
y--;
}
}else {
break;
}
}
if (x + 1 < n) {
x++;
}
break;
default:
throw new AssertionError();
}
if (arr[y][x] != 0) {
cont = false;
}
k++;
direction = directions[k % directions.length];
}
for (int[] row : arr) {
for (int elem : row) {
pw.printf("%6d", elem);
}
pw.println();
}
pw.close();
}
// public static int[][] getSpiralArray(int dimension) {
// int[][] spiralArray = new int[dimension][dimension];
//
// int numConcentricSquares = (int) Math.ceil((dimension) / 2.0);
//
// int j;
// int sideLen = dimension;
// int currNum = 1;
//
// for (int i = 0; i < numConcentricSquares; i++) {
// // do top side
// for (j = 0; j < sideLen; j++) {
// spiralArray[i][i + j] = currNum++;
// }
//
// // do right side
// for (j = 1; j < sideLen; j++) {
// spiralArray[i + j][dimension - 1 - i] = currNum++;
// }
//
// // do bottom side
// for (j = sideLen - 2; j > -1; j--) {
// spiralArray[dimension - 1 - i][i + j] = currNum++;
// }
//
// // do left side
// for (j = sideLen - 2; j > 0; j--) {
// spiralArray[i + j][i] = currNum++;
// }
//
// sideLen -= 2;
// }
//
// return spiralArray;
// }
//
// public static void print2dArray(int[][] array) throws FileNotFoundException {
// PrintWriter pw = new PrintWriter(new File("output.txt"));
// for (int[] row : array) {
// for (int elem : row) {
// pw.printf("%6d", elem);
// }
// pw.println();
// }
// pw.close();
// }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment