public int totalNQueens(int n) { StringBuilder[] board = new StringBuilder[n]; for (int i = 0; i < n; i++) { board[i] = new StringBuilder(); for (int j = 0; j < n; j++) { board[i].append('.'); } } for (int i = 0; i < n / 2; i++) { board[0].setCharAt(i, 'Q'); dfs(n, 1, board); board[0].setCharAt(i, '.'); } ArrayList<String[]> aux = new ArrayList<String[]>(); for (String[] k : res) { String[] tmp = new String[n]; for (int i = 0; i < n; i++) { StringBuilder sb = new StringBuilder(k[i]).reverse(); tmp[i] = sb.toString(); } aux.add(tmp); } res.addAll(aux); if (n % 2 != 0) { board[0].setCharAt(n / 2, 'Q'); dfs(n, 1, board); board[0].setCharAt(n / 2, '.'); } return res.size(); }
// ************************************************** // ************************************************** // ************************************************** // MAP STUFF // ************************************************** // ************************************************** // ************************************************** private static void generateMap(String mapName) { try { PrintWriter writer = new PrintWriter(mapName); final char[] map = new char[rows * cols]; Arrays.fill(map, '.'); for (WareHouse warehouse : warehouses) { map[warehouse.x * cols + warehouse.y] = 'w'; } for (Order order : orders) { map[order.x * cols + order.y] = 'o'; } for (int r = 0; r < rows; r++) { final StringBuilder stringBuilder = new StringBuilder(); for (int c = 0; c < cols; c++) { stringBuilder.append(map[r * cols + c]); } stringBuilder.append('\n'); writer.println(stringBuilder.toString()); } writer.close(); } catch (IOException e) { System.out.println("error writing file " + e.getMessage()); } }
public String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != // ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); }
private String ns1() { int b = skip(); StringBuilder sb = new StringBuilder(); while (!(isSpaceChar(b) && b != ' ')) { // when nextLine, sb.appendCodePoint(b); b = readByte(); } return sb.toString(); }
private static void addDeliverCommand( Drone drone, Order order, int productType, int productNumber) { StringBuilder stringBuilder = new StringBuilder(5); stringBuilder.append(drone.id); stringBuilder.append(' '); stringBuilder.append('D'); stringBuilder.append(' '); stringBuilder.append(order.id); stringBuilder.append(' '); stringBuilder.append(productType); stringBuilder.append(' '); stringBuilder.append(productNumber); commands.add(stringBuilder.toString()); }
private static void addLoadCommand( Drone drone, WareHouse wareHouse, int productType, int productNumber) { StringBuilder stringBuilder = new StringBuilder(5); stringBuilder.append(drone.id); stringBuilder.append(' '); stringBuilder.append('L'); stringBuilder.append(' '); stringBuilder.append(wareHouse.id); stringBuilder.append(' '); stringBuilder.append(productType); stringBuilder.append(' '); stringBuilder.append(productNumber); commands.add(stringBuilder.toString()); }
public boolean SaveDefaultCfg() { StringBuilder str = new StringBuilder(); str.append("To jest zbior konfiguracyjny do wyliczen linii paskowej niesymetrycznej\n"); str.append(MakeBackCompatibleForStorage(w)); str.append("\n"); str.append(MakeBackCompatibleForStorage(h)); str.append("\n"); str.append(MakeBackCompatibleForStorage(er)); str.append("\n"); str.append(MakeBackCompatibleForStorage(t)); str.append("\n"); return ShortTxtFile.EasyWrite(FName, str.toString()); }
private static List<String> importGenomeData( File genome_text_file, String target_organism, int sex) throws IOException { List<String> temp_genome_data = new ArrayList<String>(); // The genome data array that contains the sizes of each chromosome // int haploid_number; // Used to determine the size of the first dimension in the // temp_genome_data array boolean found_target_organism = false; // Used to determine what action to take when a new header line in the file is found; // close if true, keep reading if false boolean end_of_genome = false; // True when the Y chromosome has been dealt with // Construct BufferedReader from FileReader; search for header line of target organism and // obtain the haploid number then create a two dimensional array, size of the first dimension // equals the haploid number, size of second dimension equals two (two chromosomes to form // deploid organism) // At this point, the next lines correspond to the size of each chromosome so populate the newly // created array // with this information. Stop reading when no more new lines or when a new header line is found BufferedReader genome_file_reader = new BufferedReader(new FileReader(genome_text_file)); String line = null; StringBuilder organism_name; while ((line = genome_file_reader.readLine()) != null) { String[] split_line = line.split(" "); if (split_line[0].equals(">")) // If this is a first header line { organism_name = new StringBuilder() .append(split_line[1]) .append(" ") .append( split_line[ 2]); // Recreate the genus and species of the organism from the strings that // were separated during the splitting of the line if (organism_name .toString() .equals(target_organism)) // If this line refers to the organism of interest { found_target_organism = true; haploid_number = Integer.parseInt(split_line[4]); // Get the haploid number stored in the header line } else { found_target_organism = false; continue; // This is a header line but it is not the organism of interest } } else if (found_target_organism && !end_of_genome) // This is not a header line and we probably want to import this line { // boolean autosome = true; switch (sex) { case 1: // Female { if (split_line[0].equals("chrX")) { temp_genome_data.add(split_line[1] + "," + split_line[1]); } else if (split_line[0].equals("chrY")) { end_of_genome = true; } // Ignore the Y chromosome else temp_genome_data.add( split_line[1] + "," + split_line[1]); // The current line is an autosome } break; case 2: // Male { if (split_line[0].equals("chrX")) { temp_genome_data.add(split_line[1] + ","); } else if (split_line[0].equals("chrY")) { String temp = temp_genome_data.get( temp_genome_data.size() - 1); // Store the value already there temp_genome_data.remove(temp_genome_data.size() - 1); temp = temp + split_line[1]; temp_genome_data.add(temp); end_of_genome = true; } else temp_genome_data.add( split_line[1] + "," + split_line[1]); // The current line is an autosome } break; } } } // while ((line = genome_file_reader.readLine()) != null) genome_file_reader.close(); return temp_genome_data; } // importGenomeData