Below is my solution for Problem B: Lawnmower
/*
* @author niraj.nijju
*/
package gcj.TwelveThirteen;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;
public class Lawnmower {
public static void main(String [] args){
try{
FileInputStream fstream = new FileInputStream("B-small-practice.in");
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
strLine = br.readLine();
int T = Integer.valueOf(strLine);
// System.out.println("T: "+T);
int i=1;
while(T-- > 0){
strLine = br.readLine();
String [] nm = strLine.split(" ");
int n = Integer.valueOf(nm[0].trim());
int m = Integer.valueOf(nm[1].trim());
String [][] lawn = new String[n+1][m+1];
for(int j=0;j<n;j++){
String ms = br.readLine();
ms+= " 0";
lawn[j]=ms.split(" ");
}
if(canDo(lawn,n,m) ){
System.out.println ("Case #"+i++ +": YES");
}else{
System.out.println ("Case #"+i++ +": NO");
}
}
in.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
e.printStackTrace();
}
}
public static Boolean canDo(String [][] lawn, int n, int m){
getLargest(lawn,n,m);
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(Integer.valueOf(lawn[i][j])<Integer.valueOf(lawn[i][m])
&& Integer.valueOf(lawn[i][j]) <Integer.valueOf(lawn[n][j]) )
return false;
}
}
return true;
}
public static void getLargest(String [][] lawn,int n, int m){
for(int i=0;i<n;i++){
int hMax=0;
for(int j=0;j<m;j++){
int val=Integer.valueOf(lawn[i][j]);
if(hMax < val){
hMax = val;
}
}
lawn[i][m]=String.valueOf(hMax);
}
for(int i=0;i<m;i++){
int vMax=0;
for(int j=0;j<n;j++){
int val=Integer.valueOf(lawn[j][i]);
if(vMax < val){
vMax = val;
}
}
lawn[n][i]=String.valueOf(vMax);
}
}
}
This comment has been removed by the author.
ReplyDeleteHi, i don't understand how do you get the height x of the square, can you explain it please , thanks
ReplyDelete