Advertisement
MonsterScripter

CodinGame_2023_09_14__12_05_10__abcdefghijklmnopqrstuvwxyz.java

Sep 14th, 2023
878
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.77 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.List;
  3. import java.util.ArrayList;
  4. import java.util.LinkedList;
  5. import java.util.Queue;
  6.  
  7. class Solution {
  8.  
  9.     public static void main(String args[]) {
  10.         final Scanner in = new Scanner(System.in);
  11.         final int n = in.nextInt();
  12.         String m;
  13.         List<String> strArr = new ArrayList<String>();
  14.         for (int i = 0; i < n; i++) {
  15.             m = in.next();
  16.             strArr.add(m.toLowerCase());
  17.         }
  18.         Queue<CharPosition> lstOfA = new LinkedList<CharPosition>();
  19.         // Récupère tous la position i, j où un 'a' se situe
  20.         for (int i = 0; i < strArr.size(); i++) {
  21.             String tmp = strArr.get(i);
  22.             for (int j = 0; j < tmp.length(); j++) {
  23.                 if (tmp.charAt(j) == 'a') {
  24.                     lstOfA.add(new CharPosition('a', i, j));
  25.                 }
  26.             }
  27.         }
  28.         List<CharPosition> indexOfCorrectLetters = new ArrayList<CharPosition>();
  29.         CharPosition cp, tmpCp;
  30.         int i, j;
  31.         char c;
  32.         while (!lstOfA.isEmpty()) {
  33.             cp = lstOfA.poll();
  34.             i = cp.getI();
  35.             j = cp.getJ();
  36.             tmpCp = new CharPosition(cp.getCharacter(), i, j);
  37.             indexOfCorrectLetters.add(tmpCp);
  38.             c = '\0';
  39.             for (c = (char) (cp.getCharacter() + 1); c <= 'z'; c++) {
  40.                 tmpCp = null;
  41.                 // en haut
  42.                 if (i > 0 && strArr.get(i-1).charAt(j) == c) {
  43.                     i--;
  44.                     tmpCp = new CharPosition(c, i, j);
  45.                 }
  46.                 // en bas
  47.                 else if (i < (n-1) && strArr.get(i+1).charAt(j) == c) {
  48.                     i++;
  49.                     tmpCp = new CharPosition(c, i, j);
  50.                 }
  51.                 // à gauche
  52.                 else if (j > 0 && strArr.get(i).charAt(j-1) == c) {
  53.                     j--;
  54.                     tmpCp = new CharPosition(c, i, j);
  55.                 }
  56.                 // à droite
  57.                 else if (j < (n-1) && strArr.get(i).charAt(j+1) == c) {
  58.                     j++;
  59.                     tmpCp = new CharPosition(c, i, j);
  60.                 }
  61.                 if (tmpCp != null) {
  62.                     indexOfCorrectLetters.add(tmpCp);
  63.                 } else {
  64.                     indexOfCorrectLetters.clear();
  65.                     break;
  66.                 }
  67.             }
  68.             if (indexOfCorrectLetters.size() > 0 && indexOfCorrectLetters.get(indexOfCorrectLetters.size() - 1).getCharacter() == 'z') {
  69.                 break;
  70.             }
  71.         }
  72.         // Affichage
  73.         for (i=0; i < n; i++) {
  74.             for (j=0; j < n; j++) {
  75.                 c = '-';
  76.                 for (CharPosition cPos : indexOfCorrectLetters) {
  77.                     if (cPos.getI() == i && cPos.getJ() == j) {
  78.                         c = cPos.getCharacter();
  79.                         break;
  80.                     }
  81.                 }
  82.                 System.out.print(c);
  83.             }
  84.             System.out.println();
  85.         }
  86.     }
  87.  
  88.     private static class CharPosition {
  89.  
  90.         private char character;
  91.         private int i;
  92.         private int j;
  93.  
  94.         public CharPosition(char character, int i, int j) {
  95.             this.character = character;
  96.             this.i = i;
  97.             this.j = j;
  98.         }
  99.  
  100.         public char getCharacter() {
  101.             return character;
  102.         }
  103.  
  104.         public void setCharacter(char character) {
  105.             this.character = character;
  106.         }
  107.  
  108.         public int getI() {
  109.             return i;
  110.         }
  111.  
  112.         public void setI(int i) {
  113.             this.i = i;
  114.         }
  115.  
  116.         public int getJ() {
  117.             return j;
  118.         }
  119.  
  120.         public void setJ(int j) {
  121.             this.j = j;
  122.         }
  123.  
  124.         @Override
  125.         public String toString() {
  126.             return "CharPosition{" +
  127.                     "character=" + character +
  128.                     ", i=" + i +
  129.                     ", j=" + j +
  130.                     '}';
  131.         }
  132.        
  133.     }
  134.    
  135. }
  136.  
  137. /**
  138.  * Objectif :
  139.  * Ce programme cherche à identifier la séquence consécutive de 'a' à 'z' dans l'ordre alphabétique dans une chaîne multiligne 'm' de 'n' lignes de longueur 'n'.
  140.  * Vous pouvez vous déplacer vers le haut, la droite, la gauche ou le bas. Tout d'abord, vous trouvez 'a'.
  141.  * Si 'b' est situé en haut, en bas, à gauche ou à droite de 'a', vous pouvez vous y déplacer. Si 'c' est situé de la même manière par rapport à 'b', vous pouvez vous y déplacer, et ainsi de suite jusqu'à 'z'.
  142.  * Toutes les séquences de lettres non consécutives de 'a' à 'z' sont réécrites en '-'. En d'autres termes, ce problème consiste à afficher uniquement la séquence consécutive de 'a' à 'z' dans une chaîne multiligne 'm' de 'n' lignes de longueur 'n'.
  143.  *
  144.  * Entrée :
  145.  * Ligne 1 : Un entier 'n' pour la taille de la figure en chaîne.
  146.  * Les 'n' lignes suivantes : une chaîne multiligne 'm'.
  147.  * Sortie :
  148.  * Affichez uniquement les séquences alphabétiques de 'a' à 'z' dans la chaîne multiligne 'm', les autres parties devant être remplacées par '-'.
  149.  * Contraintes :
  150.  * 10 <= n <= 30
  151.  * 'm' se compose uniquement de caractères alphabétiques minuscules.
  152.  * Il peut y avoir plus d'un 'a'.
  153.  * Il n'y a qu'une seule séquence 'abcdefghijklmnopqrstuvwxyz' dans la figure en chaîne.
  154.  *
  155.  * La sortie est la suivante :
  156.  *
  157.  * Exemple :
  158.  *
  159.  * 10
  160.  * vkbjbzmbgb
  161.  * abcccpzouv
  162.  * fedopwlmcl
  163.  * glmnqrszyw
  164.  * hkrhiutymj
  165.  * ijqcmvwxoc
  166.  * pcvlpqzphl
  167.  * hsgvoklcxy
  168.  * urdjusmbmz
  169.  * rchbcausnp
  170.  *
  171.  * La réponse à cela est...
  172.  *
  173.  * ----------
  174.  * abc-------
  175.  * fedop-----
  176.  * glmnqrsz--
  177.  * hk---uty--
  178.  * ij---vwx--
  179.  * ----------
  180.  * ----------
  181.  * ----------
  182.  * ----------
  183.  */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement