Go to diff to previous submission
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; /* * To change this template, choose Tools | Templates and open the template in * the editor. */ /** * * @author cteam066 */ public class Grasshop { public static boolean inGrid(int[][] grid, int col, int row) { return (col >= 0 && col < grid[0].length && row >= 0 && row < grid.length); } public static int markNext(int[][] grid, int col, int row, int hops) { int count = 0; int[] dx = new int[]{1, 2, 2, 1, -1, -2, -2, -1}; int[] dy = new int[]{-2, -1, 1, 2, 2, 1, -1, -2}; for (int i = 0; i < 8; i++) { int col1 = col + dy[i]; int row1 = row + dx[i]; count++; grid[row1][col1] = hops; } } return count; } public static void print(int[][] grid) { for (int i = 0; i < grid.length; i++) { for (int j = 0; j < grid[0].length; j++) { if (grid[i][j] == -1) { } else { } else { } } } } } /** * @param args the command line arguments */ while (inputLine != null) { int[][] grid = new int[rows][cols]; grid[startR][startC] = -1; if (startR == endR && startC == endC) { inputLine = br.readLine(); continue; } int hops = 1; int marks = markNext(grid, startR, startC, hops); // print(grid); hops++; marks = 0; for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { if (grid[i][j] == hops - 1) { marks += markNext(grid, j, i, hops); } } } } //print(grid); if (marks == 0) { } else { } inputLine = br.readLine(); } } }
--- c4.s989.cteam095.grasshop.java.0.Grasshop.java +++ c4.s1048.cteam095.grasshop.java.0.Grasshop.java @@ -41,5 +41,5 @@ public static void print(int[][] grid) { for (int i = 0; i < grid.length; i++) { - for (int j = 0; j < grid.length; j++) { + for (int j = 0; j < grid[0].length; j++) { if (grid[i][j] == -1) { @@ -87,19 +87,18 @@ grid[endR][endC] = Integer.MAX_VALUE; - - if(startR == endR && startC == endC) { - + + if (startR == endR && startC == endC) { + System.out.println(0); inputLine = br.readLine(); continue; } - + int hops = 1; int marks = markNext(grid, startR, startC, hops); - - hops++; while (grid[endR][endC] == Integer.MAX_VALUE && marks > 0) { - // print(grid); + // print(grid); + hops++; marks = 0; @@ -108,13 +107,11 @@ if (grid[i][j] == hops - 1) { - marks += markNext(grid, i, j, hops); + marks += markNext(grid, j, i, hops); } } } - - hops++; } - // print(grid); + //print(grid); if (marks == 0) {