Getting unexpected output in my Java program -
i'm not understanding why getting wrong answer. can't find out wrong in code. if find out problem, grateful.
here link problem:
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&itemid=8&page=show_problem&problem=37
here code:
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]; (int z = 0; z<numberofblocks;z++) { blocks[z][0] = z; //fill blocks } while(input.hasnext()) //the input { string line = input.nextline(); string cleanline = line.trim().replaceall(regex_whitespace, " ");//remove spaces if(cleanline.equals("quit")) break; else { string[] temp = cleanline.split(regex_whitespace); //now temp's first element a, third b, second move/pile, fourth over/onto int a=0; int b=0; = 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); } } (int = 0;i<blocks.length;i++) //the output { system.out.printf("%d: ", i); (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 , b in same box (or, a==b) { boolean founda = false; boolean foundb = false; (int[] block : blocks) { (int j = 0; j < block.length; j++) { if (block[j] == x) { founda = true;// in 'j' block } if (block[j] == y) { foundb = true; // b in 'j' block } if (founda && foundb) //both & b in 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 in int[] blockb = null; //the block b in boolean founda = false; //found input boolean foundb = false; //found input b int m = 0; //the number of block in int n = 0; //the number of block b in (int = 0;i<blocks.length;i++) { (int j = 0;j<blocks[i].length;j++) { if (founda) // if input found, return blocks stacked on top of block initial positions { blocks[blocks[i][j]] = new int[1]; blocks[blocks[i][j]][0] = blocks[i][j]; } if (foundb) // if input b found, return blocks stacked on top of block b initial positions { blocks[blocks[i][j]] = new int[1]; blocks[blocks[i][j]][0] = blocks[i][j]; } if (blocks[i][j]==x) //find { 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 copy of block b in 1 more element...as last element system.arraycopy(blockb, 0, tmpblock, 0, blockb.length); tmpblock[blockb.length] = blocka[blocka.length-1]; //puts onto b blocks[n] = new int[tmpblock.length]; //return block b system.arraycopy(tmpblock, 0, blocks[n], 0, tmpblock.length); blocks[m] = new int[blocka.length-1]; //return block a, 1 less element, 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 in int[] blockb = null; //the block b in boolean founda = false; //found input int m = 0; //the number of block in int n = 0; //the number of block b in (int = 0;i<blocks.length;i++) { (int j = 0;j<blocks[i].length;j++) { if (founda) // if input found, return blocks stacked on top of block initial positions { blocks[blocks[i][j]] = new int[1]; blocks[blocks[i][j]][0] = blocks[i][j]; } if (blocks[i][j]==x) //find { 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 copy of block b in 1 more element...as last element system.arraycopy(blockb, 0, tmpblock, 0, blockb.length); tmpblock[blockb.length] = blocka[blocka.length-1]; //puts on b(on top of stack containing b) blocks[n] = new int[tmpblock.length]; //return block b system.arraycopy(tmpblock, 0, blocks[n], 0, tmpblock.length); blocks[m] = new int[blocka.length-1]; //return block a, 1 less element, 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 in int[] blockb = null; //the block b in boolean foundb = false; //found input int m = 0; //the number of block in int n = 0; //the number of block b in int p = 0; //the number of blocks there in block in, after , blocks stacked on top of have been moved other block (int = 0;i<blocks.length;i++) { (int j = 0;j<blocks[i].length;j++) { if (foundb) // if input b found, return blocks stacked on top of block b initial positions { blocks[blocks[i][j]] = new int[1]; blocks[blocks[i][j]][0] = blocks[i][j]; } if (blocks[i][j]==x) //find { 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 copy of block b in more elements needed...the last elements , blocks stacked on top of system.arraycopy(blockb, 0, tmpblock, 0, blockb.length); system.arraycopy(blocka, p, tmpblock,blockb.length, blocka.length-p); //puts , blocks stacked on top of onto b blocks[n] = new int[tmpblock.length]; //return block b system.arraycopy(tmpblock, 0, blocks[n], 0, tmpblock.length); blocks[m] = new int[p]; //return block a, removing elements have been moved 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 in int[] blockb = null; //the block b in int m = 0; //the number of block in int n = 0; //the number of block b in int p = 0; //the number of blocks there in block in, after , blocks stacked on top of have been moved other block (int = 0;i<blocks.length;i++) { (int j = 0;j<blocks[i].length;j++) { if (blocks[i][j]==x) //find { 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 copy of block b in more elements needed...the last elements , blocks stacked on top of system.arraycopy(blockb, 0, tmpblock, 0, blockb.length); system.arraycopy(blocka, p, tmpblock,blockb.length, blocka.length-p); //puts , blocks stacked on top of on b(on top of stack containing b) blocks[n] = new int[tmpblock.length]; system.arraycopy(tmpblock, 0, blocks[n], 0, tmpblock.length); //return block b blocks[m] = new int[p]; system.arraycopy(blocka, 0, blocks[m], 0, p); //return block a, removing elements have been moved other block } }
i have figured out(thanks 'gilbert le blanc'),
i had 2 errors...
one pointed out gilbert le blanc..
a. had space between number , colon..
b. other error was not finsihing line..that is:
for (int = 0;i<blocks.length;i++) //the output { system.out.printf("%d : ", i); //// here 1st problem (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 2nd problem system.out.printf("\n"); } i had change to:
for (int = 0;i<blocks.length;i++) //the output { system.out.printf("%d: ", i); (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"); } } those reasons uva judge wasn't accepting answer, accepted.
thanks replies..
Comments
Post a Comment