fork(1) download
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.ArrayList;
  5. import java.util.HashMap;
  6. import java.util.LinkedList;
  7. import java.util.StringTokenizer;
  8. import java.util.Vector;
  9. import java.util.regex.Pattern;
  10. import java.util.Collections;
  11.  
  12. class Main{
  13.  
  14. public static void main(String args[]) throws IOException{
  15. MaterialePerK matPerK[]= new MaterialePerK[16];
  16.  
  17. int Adiac0 []={1,4,5};
  18. matPerK[0]= new MaterialePerK(new CellaBoggle(0, 0),Adiac0 );
  19. int Adiac1[]={0,4,5,6,2};
  20. matPerK[1]= new MaterialePerK(new CellaBoggle(0, 1),Adiac1 );
  21. int Adiac2[]={1,5,6,3,7};
  22. matPerK[2]= new MaterialePerK(new CellaBoggle(0, 2),Adiac2 );
  23. int Adiac3[]={2,6,7};
  24. matPerK[3]= new MaterialePerK(new CellaBoggle(0, 3),Adiac3 );
  25.  
  26.  
  27. int Adiac4[]= {0,1,5,9,8};
  28. matPerK[4]= new MaterialePerK(new CellaBoggle(1, 0),Adiac4);
  29. int Adiac5[]= {4,0,1,2,6,10,9,8};
  30. matPerK[5]= new MaterialePerK(new CellaBoggle(1, 1),Adiac5 );
  31. int Adiac6[]={5,1,2,3,7,11,10,9};
  32. matPerK[6]= new MaterialePerK(new CellaBoggle(1, 2),Adiac6 );
  33. int Adiac7[]={3,2,6,10,11};
  34. matPerK[7]= new MaterialePerK(new CellaBoggle(1, 3),Adiac7 );
  35.  
  36. int Adiac8[]={4,5,9,13,12};
  37. matPerK[8]= new MaterialePerK(new CellaBoggle(2, 0),Adiac8 );
  38. int Adiac9[]={8,4,5,6,10,14,13,12};
  39. matPerK[9]= new MaterialePerK(new CellaBoggle(2, 1),Adiac9);
  40. int Adiac10[]={9,5,6,7,11,15,14,13};
  41. matPerK[10]= new MaterialePerK(new CellaBoggle(2, 2),Adiac10);
  42. int Adiac11[]={7,6,10,14,15};
  43. matPerK[11]= new MaterialePerK(new CellaBoggle(2, 3),Adiac11);
  44.  
  45. int Adiac12[]={8,9,13};
  46. matPerK[12]= new MaterialePerK(new CellaBoggle(3, 0),Adiac12);
  47. int Adiac13[]={12,8,9,10,14};
  48. matPerK[13]= new MaterialePerK(new CellaBoggle(3, 1),Adiac13);
  49. int Adiac14[]={13,9,10,11,15};
  50. matPerK[14]= new MaterialePerK(new CellaBoggle(3, 2),Adiac14);
  51. int Adiac15[]={11,10,14};
  52. matPerK[15]= new MaterialePerK(new CellaBoggle(3,3),Adiac15);
  53.  
  54. ContenitorePigEwu C= new ContenitorePigEwu();
  55. GrafoBoggle Gr= new GrafoBoggle(matPerK,C);
  56. LeggiPreparaM L= new LeggiPreparaM(Gr);
  57. L.LeggiRigheM();
  58. }
  59.  
  60. static class CellaBoggle {
  61. private int Righe,Col;
  62.  
  63. public CellaBoggle(int r,int c){
  64. Righe=r; Col=c;
  65. }
  66. int getRiga(){ return Righe; }
  67. int getCol(){ return Col; }
  68.  
  69. void setRiga(int r){Righe=r; }
  70. void setCol(int c){ Col=c; }
  71. }
  72.  
  73. static class MaterialePerK {
  74. private int NodiAdiacenti[];
  75. private CellaBoggle C;
  76.  
  77. public MaterialePerK(CellaBoggle cb,int adiac[]) {
  78. C=cb;
  79. NodiAdiacenti= adiac;
  80. }
  81.  
  82. CellaBoggle getCellaBoggle(){
  83. return C; }
  84.  
  85.  
  86. void setArrayAdiac(int ind,int val){
  87. NodiAdiacenti[ind]=val;
  88. }
  89.  
  90. int [] getArrayAdiac(){
  91. return NodiAdiacenti;
  92. }
  93.  
  94. }
  95.  
  96.  
  97. static class LeggiPreparaM {
  98. private GrafoBoggle Gr;
  99. private String Riga;
  100. private char B1[][];
  101. private char B2[][];
  102. private StringTokenizer st;
  103. private BufferedReader In_Str;
  104. private String pattern = "([A-Z] ){3}[A-Z]{1}(\t)+([A-Z] ){3}[A-Z]";
  105. private Pattern p = Pattern.compile(pattern);
  106.  
  107. LeggiPreparaM(GrafoBoggle G) {
  108. Riga=""; B1=new char[4][4]; B2=new char[4][4]; st=null;
  109. In_Str = new BufferedReader(new InputStreamReader(System.in)); Gr=G;
  110. }
  111.  
  112. char[][] getB1() {
  113. return B1;
  114. }
  115.  
  116. char[][] getB2() {
  117. return B2;
  118. }
  119.  
  120. void LeggiRigheM()throws IOException{
  121. int R; boolean cicla=true;
  122. try{
  123. while(true){
  124. R=0;
  125. while(R<4){
  126.  
  127. Riga=In_Str.readLine();
  128. if(Riga.equals("#") )
  129. return ;
  130.  
  131. System.out.println();
  132.  
  133. st= new StringTokenizer(Riga," ");
  134. AssegnaRiga(B1,R,0);
  135. AssegnaRiga(B2,R,0);
  136. R++;
  137. }
  138. Gr.TrovaParolePigEwu(getB1(),false);
  139.  
  140.  
  141. if(!Gr.TrovaParolePigEwu(getB2(),true)){
  142. System.out.println("There are no common words for this pair of boggle boards.");
  143. System.out.println();
  144. }
  145.  
  146.  
  147.  
  148. Gr.getContenitoreParole().rimuoviVettore();
  149. Gr.getContenitoreParole().allocaVettore();
  150. Gr.getContenitoreParole().setCiSonoComuni(false);
  151.  
  152. }
  153.  
  154. }catch (IOException e) {
  155. return;
  156. }
  157. }
  158.  
  159. void AssegnaRiga(char B[][],int R,int C1){
  160. while(st.hasMoreTokens() && C1<4){
  161. B[R][C1] = st.nextToken().charAt(0);
  162. C1++;
  163. }
  164. return ;
  165. }
  166.  
  167. }
  168.  
  169. static class ContenitorePigEwu {
  170.  
  171. private boolean ciSonoComuni;
  172. private Vector<ArrayList<String>> parole = new Vector<ArrayList<String>>(26);
  173. private ArrayList<String> paroleComuni ;
  174.  
  175.  
  176. ContenitorePigEwu(){
  177. for(int i=0 ; i<26 ;i++){
  178. parole.insertElementAt(new ArrayList<String>(),i);
  179. }
  180. ciSonoComuni=false;
  181. paroleComuni= new ArrayList<String>();
  182. }
  183.  
  184. public Vector<ArrayList<String>> getParole() {
  185. return parole;
  186. }
  187.  
  188. public ArrayList<String> getParoleComuni() {
  189. return paroleComuni;
  190. }
  191.  
  192. boolean ciSonoComuni(){
  193. return ciSonoComuni;
  194. }
  195.  
  196. void setCiSonoComuni(boolean p){
  197. ciSonoComuni=p;
  198. }
  199.  
  200. void rimuoviVettore(){
  201. parole.removeAllElements();
  202. paroleComuni.clear();
  203. }
  204.  
  205. void allocaVettore(){
  206. for(int i=0 ; i<26 ;i++)
  207. parole.insertElementAt(new ArrayList<String>(),i);
  208. paroleComuni= new ArrayList<String>();
  209. }
  210.  
  211. void inserInOrdineParolaDatoUnaCella(String parola){
  212. int indVett= parola.charAt(0)-65;
  213. parole.get(indVett).add(parola);
  214. }
  215.  
  216. void ordinaVettore(ArrayList<String> arr){
  217. Collections.sort(arr);
  218. }
  219.  
  220. void cercaComuni(String parola2matr){
  221. int indVett= parola2matr.charAt(0)-65;
  222. if(parole.get(indVett).size()!=0){
  223. int left, right;
  224. left = 0;
  225. right =parole.get(indVett).size();
  226.  
  227. while (left!=right-1) {
  228. int m = (right+left)/2 ;
  229.  
  230. String sm = parole.get(indVett).get(m);
  231.  
  232. if(sm.compareTo(parola2matr)<0)
  233. left = m;
  234.  
  235. else if (sm.compareTo(parola2matr)>0)
  236. right = m;
  237. else {
  238. left = m;
  239. right = m+1;
  240. }
  241. }
  242.  
  243. if(parola2matr.equals(parole.get(indVett).get(left) )){
  244. paroleComuni.add(parola2matr);
  245. ciSonoComuni=true;
  246. }
  247. }
  248.  
  249. }
  250.  
  251. void stampaParoleInComune(){
  252. int k=0 ;
  253. for(; k<paroleComuni.size()-1; k++){
  254.  
  255. if(! paroleComuni.get(k).equals(paroleComuni.get(k+1) ) )
  256. System.out.println(paroleComuni.get(k));
  257.  
  258. }
  259.  
  260. if(paroleComuni.get(k).equals( paroleComuni.get(k-1) ) )
  261. System.out.println(paroleComuni.get(k));
  262. else {
  263. System.out.println(paroleComuni.get(k));
  264. }
  265. System.out.println();
  266.  
  267. }
  268.  
  269. }
  270. static class GrafoBoggle {
  271.  
  272. private HashMap<Integer,MaterialePerK> GrafoTavola =new HashMap<Integer, MaterialePerK>(16, 16) ;
  273. private MaterialePerK matPerK[];
  274. private ContenitorePigEwu contenitoreParole;
  275. private boolean Decr[];
  276.  
  277. GrafoBoggle(MaterialePerK mater[],ContenitorePigEwu cp){
  278. contenitoreParole=cp;
  279. matPerK=mater;
  280. for(int k=0 ;k<16 ; k++)
  281. GrafoTavola.put(k, matPerK[k]);
  282. Decr= new boolean[4];
  283. }
  284.  
  285. public ContenitorePigEwu getContenitoreParole() {
  286. return contenitoreParole;
  287. }
  288.  
  289. boolean TrovaParolePigEwu(char B[][],boolean ConfrontoP){
  290. int contaVoc=0;
  291. int Nodo_K=0;
  292. for(int i=0 ; i<Decr.length ;i++)
  293. Decr[i]=false;
  294.  
  295. while(Nodo_K<16){
  296.  
  297. if(CeVocaleAlNodo(Nodo_K,B) ){
  298. contaVoc++;
  299. Decr[0]=true;
  300. }
  301.  
  302. for(int i=0; i<GrafoTavola.get(Nodo_K).getArrayAdiac().length;i++) {
  303. int Nodo_K2=GrafoTavola.get(Nodo_K).getArrayAdiac()[i];
  304.  
  305. if(Nodo_K!=Nodo_K2){
  306.  
  307. if(CeVocaleAlNodo(Nodo_K2,B)){
  308. contaVoc++;
  309. Decr[1]=true;
  310. }
  311.  
  312. for(int u=0; u< GrafoTavola.get(Nodo_K2).getArrayAdiac().length;u++) {
  313. int Nodo_K3 = GrafoTavola.get(Nodo_K2).getArrayAdiac()[u];
  314.  
  315. if(CeVocaleAlNodo(Nodo_K3,B)){
  316. contaVoc++;
  317. Decr[2]=true;
  318. }
  319.  
  320. if(contaVoc<3 && Nodo_K3!=Nodo_K && Nodo_K3!= Nodo_K2 ){
  321. for(int y=0 ; y< GrafoTavola.get(Nodo_K3).getArrayAdiac().length; y++){
  322. int Nodo_K4= GrafoTavola.get(Nodo_K3).getArrayAdiac()[y];
  323.  
  324. if(CeVocaleAlNodo(Nodo_K4,B)) {
  325. contaVoc++;
  326. Decr[3]=true;
  327. }
  328.  
  329. if(contaVoc==2 && Nodo_K4!=Nodo_K2 && Nodo_K4!=Nodo_K && Nodo_K4!=Nodo_K3){
  330. String s = new StringBuilder().append(ConvertiNodoLettera(Nodo_K, B)).append(ConvertiNodoLettera(Nodo_K2, B)).
  331. append(ConvertiNodoLettera(Nodo_K3, B)).append(ConvertiNodoLettera(Nodo_K4, B)).toString();
  332. if(!ConfrontoP)
  333. contenitoreParole.inserInOrdineParolaDatoUnaCella(s);
  334. else
  335. contenitoreParole.cercaComuni(s);
  336. }
  337.  
  338. if(Decr[3]){
  339. contaVoc--;
  340. Decr[3]=false;
  341. }
  342.  
  343. }
  344. }
  345.  
  346. if(Decr[2]){
  347. contaVoc--;
  348. Decr[2]=false;
  349. }
  350.  
  351.  
  352. }
  353.  
  354. if(Decr[1]){
  355. contaVoc--;
  356. Decr[1]=false;
  357. }
  358.  
  359. }
  360.  
  361. }
  362.  
  363. if(Decr[0]){
  364. contaVoc--;
  365. Decr[0]=false;
  366. }
  367.  
  368. if(!ConfrontoP){
  369. int ind = ConvertiNodoLettera(Nodo_K, B)-65;
  370. contenitoreParole.ordinaVettore(contenitoreParole.getParole().get(ind));
  371. }
  372. Nodo_K++;
  373. }
  374.  
  375. if(ConfrontoP){
  376. if(contenitoreParole.ciSonoComuni()) {
  377. contenitoreParole.ordinaVettore(contenitoreParole.getParoleComuni() );
  378. contenitoreParole.stampaParoleInComune();
  379. return true;
  380. }
  381.  
  382. }
  383.  
  384. return false;
  385.  
  386. }
  387.  
  388. char ConvertiNodoLettera(int kN,char B[][]){
  389. CellaBoggle Cb= GrafoTavola.get(kN).getCellaBoggle();
  390. int R=Cb.getRiga(); int C=Cb.getCol();
  391. return B[R][C];
  392. }
  393.  
  394. boolean CeVocaleAlNodo(int Nodo_K,char B[][]){
  395. CellaBoggle Cb= GrafoTavola.get(Nodo_K).getCellaBoggle();
  396. int R=Cb.getRiga(); int C=Cb.getCol();
  397.  
  398. switch( B[R][C] ){
  399. case 'A': return true ;
  400. case 'Y': return true;
  401. case 'E': return true;
  402. case 'O': return true;
  403. case 'I': return true;
  404. case 'U': return true;
  405. };
  406. return false;
  407. }
  408.  
  409.  
  410. }
  411.  
  412. }
Success #stdin #stdout 0.12s 212928KB
stdin
D F F B    W A S U
T U G I    B R E T
O K J M    Y A P Q
K M B E    L O Y R

Z W A V    G S F U
U N C O    A H F T
Y T G I    G N A L
H G P M    B O O B

#
stdout



There are no common words for this pair of boggle boards.





There are no common words for this pair of boggle boards.