(UVA) Ant's Shopping Mall - Solution
Link to the problem: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=3942
import java.io.*;
import java.util.*;
class Main {
public void process() throws NumberFormatException, IOException {
Scanner sc = new Scanner(System.in);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int numTests = sc.nextInt();
for (int test = 0; test < numTests; test++) {
int numRows = sc.nextInt();
int numCols = sc.nextInt();
int[][] matrix = new int[numRows][numCols];
for (int i = 0; i < numRows; i++) {
String line = sc.next();
for (int j = 0; j < numCols; j++) {
matrix[i][j] = line.charAt(j)-'0';
}
}
int count = 0;
int bestCount = Integer.MAX_VALUE;
for (int j = 0; j < numCols; j++) {
int countRow = 0;
for (int i = 0; i < numRows; i++) {
if (matrix[i][j] == 1) {
int colFirstHalf = j-1;
int preCountFirstHalf = 1;
while (colFirstHalf >= 0 && matrix[i][colFirstHalf] == 1) {
colFirstHalf--;
preCountFirstHalf++;
}
int colSecondHalf = j+1;
int preCountSecondHalf = 1;
while (colSecondHalf < numCols && matrix[i][colSecondHalf] == 1) {
colSecondHalf++;
preCountSecondHalf++;
}
if (colFirstHalf == -1 && colSecondHalf == numCols) {
countRow = Integer.MAX_VALUE;
break;
}
countRow += Math.min(colFirstHalf == -1 ? Integer.MAX_VALUE : preCountFirstHalf,
colSecondHalf == numCols ? Integer.MAX_VALUE : preCountSecondHalf);
}
}
bestCount = Math.min(bestCount, countRow);
}
bestCount = (bestCount != Integer.MAX_VALUE) ? bestCount : -1;
bw.write("Case " + (test+1) + ": " + bestCount + "\n");
}
bw.flush();
bw.close();
return;
}
public static void main(String[] args) throws NumberFormatException, IOException {
Main m = new Main();
m.process();
System.exit(0);
}
}
import java.io.*;
import java.util.*;
class Main {
public void process() throws NumberFormatException, IOException {
Scanner sc = new Scanner(System.in);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int numTests = sc.nextInt();
for (int test = 0; test < numTests; test++) {
int numRows = sc.nextInt();
int numCols = sc.nextInt();
int[][] matrix = new int[numRows][numCols];
for (int i = 0; i < numRows; i++) {
String line = sc.next();
for (int j = 0; j < numCols; j++) {
matrix[i][j] = line.charAt(j)-'0';
}
}
int count = 0;
int bestCount = Integer.MAX_VALUE;
for (int j = 0; j < numCols; j++) {
int countRow = 0;
for (int i = 0; i < numRows; i++) {
if (matrix[i][j] == 1) {
int colFirstHalf = j-1;
int preCountFirstHalf = 1;
while (colFirstHalf >= 0 && matrix[i][colFirstHalf] == 1) {
colFirstHalf--;
preCountFirstHalf++;
}
int colSecondHalf = j+1;
int preCountSecondHalf = 1;
while (colSecondHalf < numCols && matrix[i][colSecondHalf] == 1) {
colSecondHalf++;
preCountSecondHalf++;
}
if (colFirstHalf == -1 && colSecondHalf == numCols) {
countRow = Integer.MAX_VALUE;
break;
}
countRow += Math.min(colFirstHalf == -1 ? Integer.MAX_VALUE : preCountFirstHalf,
colSecondHalf == numCols ? Integer.MAX_VALUE : preCountSecondHalf);
}
}
bestCount = Math.min(bestCount, countRow);
}
bestCount = (bestCount != Integer.MAX_VALUE) ? bestCount : -1;
bw.write("Case " + (test+1) + ": " + bestCount + "\n");
}
bw.flush();
bw.close();
return;
}
public static void main(String[] args) throws NumberFormatException, IOException {
Main m = new Main();
m.process();
System.exit(0);
}
}
Comments
Post a Comment