Robots

Time Limit:
1000ms
Memory limit:
10000kB
题目描述
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.
输入
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.
输出
The output will consist of a single line for each field map containing the minimum number of robots needed to clean the corresponding field.
样例输入
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
样例输出
2
1

解答一:

#include <iostream>
#include <algorithm>
using namespace std;
struct node
{
  int x,y; 
}a[900];
node ans[900];
bool cmp(node a,node b)
{
 if(a.x==b.x) return a.y<b.y;
 else return a.x<b.x; 
}
int main()
{
 int i,j,len;
 len=0;
 while(scanf("%d %d",&a[len].x,&a[len].y))
 {
  if(a[len].x==-1)break;
  len++;
  while(scanf("%d %d",&a[len].x,&a[len].y))
  {
     if(a[len].x==0) break;
     len++; 
  }
  sort(a,a+len,cmp);
     int c=0;
  for(i=0;i<len;i++)
  { for(j=0;j<c;j++)
    if(a[i].x>=ans[j].x && a[i].y>=ans[j].y)//先用一个机器人去扫掉它能扫的所有垃圾
    {
     ans[j].x=a[i].x;
     ans[j].y=a[i].y;
     break; //跳出第二个for,第一个for中的i加1再进行判断
    }
   //第二个for循环执行完后,说明第一个机器人已完成工作,剩下的不能扫的垃圾派第二个机器人出动
    
    if(j==c)//(先执行)派出一个机器人并记录当前机器人所在位置
    {
     ans[j].x=a[i].x;
     ans[j].y=a[i].y;
     c++;  
    } 
  } 
  cout<<c<<endl; 
  len=0;//重新清0 
 } 
    system("pause");
    return 0;
}

解答二:

#include <iostream>
using namespace std;
int a[30][30];
int main()
{
  
   int i,j;
  
   while(cin>>i>>j)
   {
     
      memset(a,0,sizeof(a));
      if(i==-1)
         break;
      a[i][j]=1;//接收第一行数据,一开始没有写。本地测试竟然也是正确的,提交一直不对
      while(cin>>i>>j,i)
      a[i][j]=1; 
     
      int m,n,s,tmp=1;
      s=0;
      while(tmp)//假设一直存在垃圾
      {   
          tmp=0;
          m=1;n=1;
          for(i=1;i<26;i++)
          {
             for(j=1;j<26;j++)
              if(a[i][j]==1 && m<=i && n<=j)
              {
                a[i][j]=0;
                tmp=1;//除非垃圾扫完。否则一直循环
                m=i;
                n=j;
                          
              } 
          }
          s++;//当没有垃圾时也会自加一次,所以输出的时候多了一个机器人,得减掉
      } 
      cout<<s-1<<endl;           
   }
  
   //system("pause");
   return 0;   
}

posted on 2010-04-12 22:13  蓝牙  阅读(476)  评论(0编辑  收藏  举报