Uva 10599 - Robots(II) (dp + 记录路径)


Problem K


Robots(II)


Time Limit


1 Second

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 can be programmed to 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 making the most out
of them. Your task would be to use a robot to clean the maximum number of cells containing garbage. Now there can be many ways to do this job, so your task would be to report that number of ways and show us one such sample.

You see your robot can traverse many cells without picking up garbage, so for us a valid solution would be the sequence of cell numbers that the robot cleans. The robots only clean cells that contain garbage; but you can program them to avoid picking up
garbage from specific cells, if you would want to.

In the figure above we show a field map that has 6 rows and 7 columns. The cells in a field map are numbered in row major order starting from 1. For the example shown here, the following 7 cells contain garbage: 2 (1,2), 4 (1,4), 11 (2, 4), 13 (2,
6), 25 (4, 4), 28 (4, 7) and 41 (6, 7)
. Here cells are presented in cell_number (row, column) format. Now the maximum number of cells that can be cleaned is 5, and there are 4different ways to do
that:

<2, 4, 11, 13, 28>

<2, 4, 11, 13, 41>

<2, 4, 11, 25, 28>

<2, 4, 11, 25, 41>

Input

An input file consists of one or more field maps followed by a line containing -1 -1 to signal the end of the input data. The description of a field map starts with the number of rows and the number of columns in the grid. Then in the subsequent
lines, the garbage locations follows. The end of a field map is signaled by 0 0. 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 not be given in any specific order. And a location would not be reported twice for a field map. Please note that for all the test cases you are required to solve, the field map would be of at most 100 rows and100 columns.

Output

The output for each test case starts with the serial number (starting from 1) for that test case. Then the following integers are listed on a line: N – the maximum number of cells that the robot can clean, C – the number
of ways that these N cells can be cleaned, and N numbers describing one possible sequence of cell numbers that the robot will clean. As there can be C different such sequences and we are asking for only one
sequence any valid sequence would do. Make sure that all these 2+N integers for a test case are printed on a single line. There must be one space separating two consecutive integers and a space between the colon and the first integer on the
line. See the sample output format for a clear idea.


Sample Input


Output for Sample Input


6 7

1 2

1 4

2 4

2 6

4 4

4 7

6 6

0 0

4 4

1 1

2 2

3 3

4 4

0 0

-1 -1


CASE#1: 5 4 2 4 11 13 28

CASE#2: 4 1 1 6 11 16

题意:给你一个n*m的矩阵,然后给你k个坐标(以0,0结束)表示上面有物品,然后有

一个机器人只能往右走和往下走,起点在左上角,终点在右下角,问你,机器从起点到

终点最多能拿起多少个物品。前面要输出有多少种路径,并给出其中一种路径(用物品

的坐标表示)。

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
const int N=10020;
const int maxn=110;

int n,m,cnt,tmp,T,a[N],y[N],pre[N],way[N],dp[N];
bool vis[maxn][maxn];

void initial()
{
    memset(pre,-1,sizeof(pre));
    memset(vis,0,sizeof(vis));
    memset(dp,0,sizeof(dp));
    cnt=0;
    tmp=0;
}

void input()
{
    int x,y;
    while(scanf("%d %d",&x,&y)!=EOF)
    {
        if(x==0 && y==0)  break;
        vis[x][y]=1;
    }
}

void ready()
{
    for(int i=1; i<=n; i++)
        for(int j=1; j<=m; j++)
            if(vis[i][j])
            {
                a[cnt]=(i-1)*m+j;
                y[cnt]=j;
                cnt++;
            }
    if(!vis[n][m])
    {
        a[cnt]=n*m;
        y[cnt]=m;
        cnt++;
        tmp=0;
    }
    else tmp=1;
    for(int i=0;i<cnt;i++)  way[i]=1;
}

void print(int x)
{
    if(pre[x]==-1)  printf(" %d",a[x]);
    else
    {
         print(pre[x]);
         printf(" %d",a[x]);
    }
}

void solve()
{
    for(int i=0; i<cnt;i++)
    {
          for(int j=0;j<i;j++)
          {
              if(y[j]<=y[i])
              {
                    if(dp[i]<dp[j])
                    {
                         dp[i]=dp[j];
                         way[i]=way[j];
                         pre[i]=j;
                    }
                    else if(dp[i]==dp[j]) way[i]+=way[j];
              }
          }
          if(i==cnt-1) dp[i]+=tmp;
          else dp[i]++;
    }
    printf("CASE#%d: %d %d",++T,dp[cnt-1],way[cnt-1]);
    if(tmp) print(cnt-1);
    else   print(pre[cnt-1]);
    cout<<endl;
}

int main()
{
    while(scanf("%d %d",&n,&m)!=EOF)
    {
        if(n==-1 && m==-1)  break;
        initial();
        input();
        ready();
        solve();
    }
    return 0;
}
时间: 2024-11-05 13:28:42

Uva 10599 - Robots(II) (dp + 记录路径)的相关文章

uva 11578 - Situp Benches(dp+输出路径)

题目连接:uva 11578 - Situp Benches 题目大意:健身房有两个仪器,初始角度为10度,每次有人使用需要交15元,每调10度需要花费10元,现在有n个人,给出每个人使用仪器的顺序和角度,保证不会同时有大于2个人序号一样,求最小花费,并且输出每个人分别使用哪一个仪器,并且所有人使用结束后,要将仪器调回10度. 解题思路:dp[i][x][y]表示第i个人,一个仪器为x,另一个仪器为y的情况,path[i][x][y]记录的是它的前一个状态,[0]为前一个仪器的夹角,[1]为另一

POJ 1141 Brackets Sequence (区间dp 记录路径)

题目大意: 给出一种不合法的括号序列,要求构造出一种合法的序列,使得填充的括号最少. 思路分析: 如果只要求输出最少的匹配括号的数量,那么就是简单的区间dp dp[i][j]表示 i - j 之间已经合法了最少添加的括号数. 转移 就是 dp[i] [j] = min  (dp[i+1][j]+1 , dp[ i+ 1] [ k -1 ] + dp[k+1] [j] (i k 位置的括号匹配)) 其次我们要记录路径,你发现  如果 dp [i] [j] 是由 dp [i+1] [j] 转移过来的

UVa 10599【lis dp,记忆化搜索】

UVa 10599 题意: 给出r*c的网格,其中有些格子里面有垃圾,机器人从左上角移动到右下角,只能向右或向下移动.问机器人能清扫最多多少个含有垃圾的格子,有多少中方案,输出其中一种方案的格子编号.格子编号是从 左上角第一个开始,一行一行的按自然数顺序编.起始行列是第一行第一列.所以例如一个格子的行列号为(ro,co),那么它的编号为bh=(ro-1)*column+co,其中column指这个格子有多少列.(我觉得原题里面有个错误,题目叙述倒数第二行应该是41(6,6)不是41(6,7)).

URAL 1244 Gentlement DP +记录路径 好题

1244. Gentlemen Time limit: 0.5 secondMemory limit: 64 MB Let's remember one old joke: Once a gentleman said to another gentleman:— What if we play cards?— You know, I haven't played cards for ten years…— And I haven't played for fifteen years…So, li

hdu 5092 Seam Carving dp+记录路径

Seam Carving Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 605    Accepted Submission(s): 253 Problem Description Fish likes to take photo with his friends. Several days ago, he found that so

uva 116 Unidirectional TSP dp + 打印路径

// uva116 Unidirectional TSP // 这题是在紫书(page 270)上看到的,个人理解就是数塔的升级版 // dp[i][j]表示从(i,j)出发到终点所达到的最大价值 // 所以很明显j是逆序的 // 状态转移方程为 // dp[i][j] = min(dp[i][j],dp[row[k]][j+1]+mp[i][j]) // rows[k]表示三行中的一行i,i-1,i+1,特判一下,排个序 // (因为多解时输出字典序最小的值) // 这题唯一比较难的地方就是打

hdu 5092 Seam Carving(DP+记录路径)

Seam Carving Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 119    Accepted Submission(s): 69 Problem Description Fish likes to take photo with his friends. Several days ago, he found that som

POJ 题目1141 Brackets Sequence(区间DP记录路径)

Brackets Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 27793   Accepted: 7885   Special Judge Description Let us define a regular brackets sequence in the following way: 1. Empty sequence is a regular sequence. 2. If S is a re

POJ 1015 Jury Compromise DP+记录路径

找每个点能转移出去的状态时要回溯到根去掉所有能转移的点来去重.. 可能这种做法在距离根距离较小的时候能用..(隐隐感觉有bug,还是人云亦云地做掉先了..) #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #include