Robots
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 3531 | Accepted: 1603 |
Description
Your company provides robots that can be used to pick up litter from fields after sporting events and concerts. Before robots are assigned to a job, an aerial photograph of the field is marked with a grid. Each location in the grid that contains garbage is
marked. All robots begin in the Northwest corner and end their movement in the Southeast corner. A robot can only move in two directions, either to the East or South. Upon entering a cell that contains garbage, the robot will pick it up before proceeding.
Once a robot reaches its destination at the Southeast corner it cannot be repositioned or reused. Since your expenses are directly proportional to the number of robots used for a particular job, you are interested in finding the minimum number of robots that
can clean a given field. For example, consider the field map shown in Figure 1 with rows and columns numbered as shown and garbage locations marked with a 'G'. In this scheme, all robots will begin in location 1,1 and end in location 6, 7.
Figure 1 - A Field Map
Figure 2 below shows two possible solutions, the second of which is preferable since it uses two robots rather than three.
Figure 2 - Two Possible Solutions
Your task is to create a program that will determine the minimum number of robots needed to pick up all the garbage from a field.

Figure 1 - A Field Map
Figure 2 below shows two possible solutions, the second of which is preferable since it uses two robots rather than three.

Figure 2 - Two Possible Solutions
Your task is to create a program that will determine the minimum number of robots needed to pick up all the garbage from a field.
Input
The input consists of one or more field maps followed by a line containing -1 -1 to signal the end of the input data. A field map consists of one or more lines, each containing one garbage location, followed by a line containing 0 0 to signal the end of the
map. Each garbage location consists of two integers, the row and column, separated by a single space. The rows and columns are numbered as shown in Figure 1. The garbage locations will be given in row-major order. No single field map will have more than 24
rows and 24 columns. The sample input below shows an input file with two field maps. The first is the field map from Figure 1.
Output
The output will consist of a single line for each field map containing the minimum number of robots needed to clean the corresponding field.
Sample Input
1 2 1 4 2 4 2 6 4 4 4 7 6 6 0 0 1 1 2 2 4 4 0 0 -1 -1
Sample Output
2 1
Solution:
package id1548;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Iterator;
import java.util.LinkedList;
public class Robots {
final int MAX_ROW = 24;
BufferedReader in = null;
LinkedList<LinkedList<String>> listliststr = null;
public Robots()
{
in = new BufferedReader(new InputStreamReader(System.in));
listliststr = new LinkedList<LinkedList<String>>();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
public void start() throws IOException{
String inStr = null;
LinkedList<String> listStr = new LinkedList<String>();
inStr = in.readLine();
Iterator listofnumbers = null;
while(true){
inStr = in.readLine();
if(inStr.equals("0 0")){
listliststr.offer(listStr);
listStr = new LinkedList<String>();
continue;
}
else if(inStr.equals("-1 -1")){
break;
}else{
listStr.offer(inStr);
}
}
listofnumbers = (get_robot_numbers(listliststr.iterator())).iterator();
while(listofnumbers.hasNext()){
System.out.println(listofnumbers.next());
}
}
private LinkedList<Integer> get_robot_numbers(Iterator<LinkedList<String>> listliststr){
LinkedList<Integer> listofnumber = new LinkedList<Integer>();
while(listliststr.hasNext()){
Integer number = get_this_field_robot_number(listliststr.next().iterator());
listofnumber.offer(number);
}
return listofnumber;
}
private Integer get_this_field_robot_number(Iterator<String> strs){
int[][] field = new int[MAX_ROW][MAX_ROW];
String[] strNum = null;
String str = null;
while(strs.hasNext()){
str = strs.next();
strNum = str.split(" ");
try{
// set garbage coordinate to 1, others zero
field[Integer.parseInt(strNum[0])][Integer.parseInt(strNum[1])] = 1;
}catch(Exception e){
System.err.println("Input format error, please input number");
System.exit(0);
}
}
int number = min_robots_for_this_field(field);
return number;
}
private int min_robots_for_this_field(int[][] field){
int robotCount = 0;
while(hasGarbage(field)){
}
return robotCount;
}
private boolean hasGarbage(int[][] field){
for(int i = 0; i < MAX_ROW; i++)
for(int j =0; j < MAX_ROW; j++)
if (field[i][j] == 1)
return true;
return false;
}
class Robot{
}
}