2015-07-13 3 views
-2

Я не понимаю, почему я получаю неправильный ответ. Не могу узнать, что не так в моем коде. Если бы кто-нибудь мог выяснить проблему, я был бы очень благодарен.Получение неожиданного вывода в моей программе Java

Вот ссылка на проблему:

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=37

Вот мой код:

import java.util.Scanner; 

class Main 
{ 


public static void main(String[] args) 
{ 


    Scanner input = new Scanner(System.in); 

    String REGEX_WHITESPACE = "\\s+"; 

    while(input.hasNext())   //The input 
    { 
    String numberOfBlocksInput = input.nextLine(); 

    int numberOfBlocks = Integer.parseInt(((numberOfBlocksInput.trim().replaceAll(REGEX_WHITESPACE, " ")).split(REGEX_WHITESPACE))[0]); 

    if (numberOfBlocks>25) 
    { 
    return; 
    } 

    int[][] blocks; 
    blocks = new int[numberOfBlocks][1]; 

    for (int z = 0; z<numberOfBlocks;z++) 
    { 
    blocks[z][0] = z;       //Fill up the Blocks 
    } 


    while(input.hasNext())   //The input 
    { 
     String line = input.nextLine(); 
     String cleanLine = line.trim().replaceAll(REGEX_WHITESPACE, " ");//Remove All Spaces 

     if(cleanLine.equals("quit")) 
      break; 

     else 
     { 
      String[] temp = cleanLine.split(REGEX_WHITESPACE); //Now temp's first element is a, third is b, second is move/pile, fourth is over/onto 

      int a=0; 
      int b=0; 
      a = Integer.parseInt(temp[1]); 
      b = Integer.parseInt(temp[3]); 
      String command = ""; 
      command = temp[0]+temp[2]; 


      if (command.equals("moveonto")) 
      moveOnto(a,b,blocks); 
      else if (command.equals("moveover")) 
      moveOver(a,b,blocks);  
      else if (command.equals("pileonto")) 
      pileOnto(a,b,blocks);  
      else if (command.equals("pileover")) 
      pileOver(a,b,blocks); 

     } 


    } 


    for (int i = 0;i<blocks.length;i++) //The output 
    { 
     System.out.printf("%d: ", i); 

     for (int j = 0;j<blocks[i].length;j++) 
     { 
     System.out.printf("%d", blocks[i][j]); 

     if (i+1<blocks.length && j+1<blocks[i].length) 
      System.out.printf(" "); 
     } 
     if (i+1<blocks.length) 
      System.out.printf("\n"); 
    } 
    } 


} 

public static boolean InTheSameBox(int x, int y, int blocks[][]) // Checks if a and b are in the same box (or, a==b) 
{ 
    boolean foundA = false; 
    boolean foundB = false; 

    for (int[] block : blocks) 
    { 
     for (int j = 0; j < block.length; j++) 
     { 
      if (block[j] == x) 
      { 
       foundA = true;// a is in 'j' block 
      } 
      if (block[j] == y) 
      { 
       foundB = true; // b is in 'j' block 
      } 

      if (foundA && foundB) //Both a & b is in the same block 
      { 
       return true; 
      } 
     } 

     foundA = false; 
     foundB = false; 
    } 

    return false; 
} 


public static void moveOnto(int x, int y, int blocks[][]) 
{ 

    if (InTheSameBox(x,y,blocks)) 
    { 
    return; 
    } 

    int[] blockA = null;   //The block a is in 
    int[] blockB = null;   //The block b is in 
    boolean foundA = false;  //Found input a 
    boolean foundB = false;  //Found input b 
    int m = 0;     //The number of block a is in 
    int n = 0;     //The number of block b is in 

    for (int i = 0;i<blocks.length;i++) 
    { 
     for (int j = 0;j<blocks[i].length;j++) 
    { 

     if (foundA) // If input a is found, return any blocks that are stacked on top of block a to their initial positions 
     { 
      blocks[blocks[i][j]] = new int[1]; 
      blocks[blocks[i][j]][0] = blocks[i][j]; 
     } 

     if (foundB) // If input b is found, return any blocks that are stacked on top of block b to their initial positions 
     { 
      blocks[blocks[i][j]] = new int[1]; 
      blocks[blocks[i][j]][0] = blocks[i][j]; 
     } 

     if (blocks[i][j]==x) //Find a 
     { 
      foundA = true; 
      blockA = new int[j+1]; 
      System.arraycopy(blocks[i], 0, blockA, 0, j+1); 
      m = i; 
     } 


     if (blocks[i][j]==y) //Find b 
     { 
      foundB = true; 
      blockB = new int[j+1]; 
      System.arraycopy(blocks[i], 0, blockB, 0, j+1); 
      n = i; 
     } 




    } 
     foundA = false; 
     foundB = false; 

    } 


    int[] tmpBlock = new int[blockB.length+1];   //Make a copy of block b is in with one more element...as the last element will be a 
    System.arraycopy(blockB, 0, tmpBlock, 0, blockB.length); 
    tmpBlock[blockB.length] = blockA[blockA.length-1]; //puts a onto b 

    blocks[n] = new int[tmpBlock.length];    //Return the block b 
    System.arraycopy(tmpBlock, 0, blocks[n], 0, tmpBlock.length); 

    blocks[m] = new int[blockA.length-1];    //Return the block a, with one less element, as a have been removed 
    System.arraycopy(blockA, 0, blocks[m], 0, blockA.length-1); 

} 


public static void moveOver(int x, int y, int blocks[][]) 
{ 
    if (InTheSameBox(x,y,blocks)) 
    { 
    return; 
    } 

    int[] blockA = null;   //The block a is in 
    int[] blockB = null;   //The block b is in 
    boolean foundA = false;  //Found input a 
    int m = 0;     //The number of block a is in 
    int n = 0;     //The number of block b is in 

    for (int i = 0;i<blocks.length;i++) 
    { 
     for (int j = 0;j<blocks[i].length;j++) 
    { 

     if (foundA)    // If input a is found, return any blocks that are stacked on top of block a to their initial positions 
     { 
      blocks[blocks[i][j]] = new int[1]; 
      blocks[blocks[i][j]][0] = blocks[i][j]; 
     } 


     if (blocks[i][j]==x) //Find a 
     { 
      foundA = true; 
      blockA = new int[j+1]; 
      System.arraycopy(blocks[i], 0, blockA, 0, j+1); 
      m = i; 
     } 


     if (blocks[i][j]==y) //Find b 
     { 
      blockB = new int[blocks[i].length]; 
      System.arraycopy(blocks[i], 0, blockB, 0, blocks[i].length); 
      n = i; 
     } 




    } 
     foundA = false; 

    } 


    int[] tmpBlock = new int[blockB.length+1];   //Make a copy of block b is in with one more element...as the last element will be a 
    System.arraycopy(blockB, 0, tmpBlock, 0, blockB.length); 
    tmpBlock[blockB.length] = blockA[blockA.length-1]; //puts a over b(on top of stack containing b) 

    blocks[n] = new int[tmpBlock.length];    //Return the block b 
    System.arraycopy(tmpBlock, 0, blocks[n], 0, tmpBlock.length); 

    blocks[m] = new int[blockA.length-1];    //Return the block a, with one less element, as a have been removed 
    System.arraycopy(blockA, 0, blocks[m], 0, blockA.length-1); 

} 


public static void pileOnto(int x, int y, int blocks[][]) 
{ 
    if (InTheSameBox(x,y,blocks)) 
    { 
    return; 
    } 

    int[] blockA = null;   //The block a is in 
    int[] blockB = null;   //The block b is in 
    boolean foundB = false;  //Found input a 
    int m = 0;     //The number of block a is in 
    int n = 0;     //The number of block b is in 
    int p = 0;     //The number of blocks there will be in block a is in, after a and blocks stacked on top of a have been moved to other block 

    for (int i = 0;i<blocks.length;i++) 
    { 
     for (int j = 0;j<blocks[i].length;j++) 
    { 


     if (foundB)     // If input b is found, return any blocks that are stacked on top of block b to their initial positions 
     { 
      blocks[blocks[i][j]] = new int[1]; 
      blocks[blocks[i][j]][0] = blocks[i][j]; 
     } 

     if (blocks[i][j]==x)  //Find a 
     { 
      blockA = new int[blocks[i].length]; 
      System.arraycopy(blocks[i], 0, blockA, 0, blocks[i].length); 
      m = i; 
      p = j; 
     } 


     if (blocks[i][j]==y)  //Find b 
     { 
      foundB = true; 
      blockB = new int[j+1]; 
      System.arraycopy(blocks[i], 0, blockB, 0, j+1); 
      n = i; 
     } 

    } 

     foundB = false; 
    } 


    int[] tmpBlock = new int[blockB.length+(blockA.length-p)];    //Make a copy of block b is in with more elements as needed...the last elements will be a and blocks stacked on top of a 
    System.arraycopy(blockB, 0, tmpBlock, 0, blockB.length); 
    System.arraycopy(blockA, p, tmpBlock,blockB.length, blockA.length-p); //puts a and blocks stacked on top of a onto b 

    blocks[n] = new int[tmpBlock.length];         //Return the block b 
    System.arraycopy(tmpBlock, 0, blocks[n], 0, tmpBlock.length); 

    blocks[m] = new int[p];            //Return the block a, removing elements that have been moved to other block 
    System.arraycopy(blockA, 0, blocks[m], 0, p); 


} 


public static void pileOver(int x, int y, int blocks[][]) 
{ 

    if (InTheSameBox(x,y,blocks)) 
    { 
    return; 
    } 

    int[] blockA = null;   //The block a is in 
    int[] blockB = null;   //The block b is in 
    int m = 0;     //The number of block a is in 
    int n = 0;     //The number of block b is in 
    int p = 0;     //The number of blocks there will be in block a is in, after a and blocks stacked on top of a have been moved to other block 

    for (int i = 0;i<blocks.length;i++) 
    { 
     for (int j = 0;j<blocks[i].length;j++) 
    { 


     if (blocks[i][j]==x) //Find a 
     { 
      blockA = new int[blocks[i].length]; 
      System.arraycopy(blocks[i], 0, blockA, 0, blocks[i].length); 
      m = i; 
      p = j; 
     } 


     if (blocks[i][j]==y) //Find b 
     { 
      blockB = new int[blocks[i].length]; 
      System.arraycopy(blocks[i], 0, blockB, 0, blocks[i].length); 
      n = i; 
     } 

    } 

    } 



    int[] tmpBlock = new int[blockB.length+(blockA.length-p)];    //Make a copy of block b is in with more elements as needed...the last elements will be a and blocks stacked on top of a 
    System.arraycopy(blockB, 0, tmpBlock, 0, blockB.length); 
    System.arraycopy(blockA, p, tmpBlock,blockB.length, blockA.length-p); //puts a and blocks stacked on top of a over b(on top of stack containing b) 

    blocks[n] = new int[tmpBlock.length]; 
    System.arraycopy(tmpBlock, 0, blocks[n], 0, tmpBlock.length);   //Return the block b 

    blocks[m] = new int[p]; 
    System.arraycopy(blockA, 0, blocks[m], 0, p);       //Return the block a, removing elements that have been moved to other block 

} 


} 
+0

какой ответ вы получаете? –

+0

Пожалуйста, по крайней мере, правильно отформатируйте/отформатируйте код. –

+0

У вас есть пробел между номером и двоеточием. Судья хочет, чтобы двоеточие следовало за номером. –

ответ

0

Я понял это (благодаря 'Гилберта Le Blanc'),

У меня было 2 ошибки ...

один был указан Gilbert Le Blanc ..

a. У меня было пробел между номером и толстой кишкой.

b. другая ошибка, я не finsihing в line..that является:

for (int i = 0;i<blocks.length;i++) //The output 
{ 
    System.out.printf("%d : ", i); //// Here was the 1st Problem 

    for (int j = 0;j<blocks[i].length;j++) 
    { 
    System.out.printf("%d", blocks[i][j]); 

    if (i+1<blocks.length && j+1<blocks[i].length) 
     System.out.printf(" "); 
    } 
    if (i+1<blocks.length)   ////Here was the 2nd Problem 
     System.out.printf("\n"); 
} 

мне пришлось изменить, что:

for (int i = 0;i<blocks.length;i++) //The output 
{ 
    System.out.printf("%d: ", i); 

    for (int j = 0;j<blocks[i].length;j++) 
    { 
    System.out.printf("%d", blocks[i][j]); 

    if (i+1<blocks.length && j+1<blocks[i].length) 
     System.out.printf(" "); 
    } 

     System.out.printf("\n"); 
} 
} 

Это были причины УФА судья не принимал мой ответ, Теперь это принято.

Спасибо за ваши ответы ..

Смежные вопросы