public int[][] generateMatrix(int n) {
		int[][] res = new int[n][n];
		
		int total = n * n;
		boolean used[][] = new boolean[n][n];
		int[] x = new int[]{0, 1, 0, -1}, y = new int[]{1, 0, -1, 0};
		int s = 0, t = 0, currentX = 0, currentY = 0;
		
		for(int i = 1; i <= total; i++){
			res[s][t] = i;
			used[s][t] = true;
			if(i == total) break;
			while(!(s + x[currentX] >= 0 && s + x[currentX] < n && t + y[currentY] >= 0
					&& t + y[currentY] < n && !used[s + x[currentX]][t + y[currentY]])){
				currentX = (currentX == 3) ? 0 : currentX + 1;
				currentY = (currentY == 3) ? 0 : currentY + 1;
			}
			s = s + x[currentX];
			t = t + y[currentY];
		}
		
		return res;
	}