UVA 题目825 Walking on the Safe Side(DP)

原创 2015年11月17日 16:28:05
Square City is a very easy place for people to walk around. The two-way streets run North-South or
East-West dividing the city into regular blocks. Most street intersections are safe for pedestrians to
cross. In some of them, however, crossing is not safe and pedestrians are forced to use the available
underground passages. Such intersections are avoided by walkers. The entry to the city park is on the
North-West corner of town, whereas the railway station is on the South-East corner.
Suppose you want to go from the park to the railway station, and do not want to walk more than
the required number of blocks. You also want to make your way avoiding the underground passages,
that would introduce extra delay. Your task is to determine the number of different paths that you can
follow from the park to the station, satisfying both requirements.
The example in the picture illustrates a city with 4 E-W streets and 5 N-S streets. Three intersections
are marked as unsafe. The path from the park to the station is 3 + 4 = 7 blocks long and there are 4
such paths that avoid the underground passages.
Input
The input begins with a single positive integer on a line by itself indicating the number
of the cases following, each of them as described below. This line is followed by a blank
line, and there is also a blank line between two consecutive inputs.
The first line of the input contains the number of East-West streets W and the number of NorthSouth
streets N. Each one of the following W lines starts with the number of an East-West street,
followed by zero or more numbers of the North-South crossings which are unsafe. Streets are numbered
from 1.
Output
For each test case, the output must follow the description below. The outputs of two
consecutive cases will be separated by a blank line.
The number of different minimal paths from the park to the station avoiding underground passages.
Sample Input
1
4 5
1
2 2
3 3 5
4
Sample Output
4
题目大意:一个t,t组测试数据,下n行,下边n行数据,每行先输入一个行号,然后后边有不定个数,表示这一行,这几个位置不能走,问从左上角走到右下角,只能向下和向右走,的方法数
ac代码

<span style="font-family: Arial, Helvetica, sans-serif;">#include<stdio.h></span>
#include<string.h>
#include<iostream>
#include<algorithm>
#define LL long long
using namespace std;
LL dp[1010][1010];
int map[1010][1010];
char str[3010];
int n,m;
void fun(char *str,int r)
{
    int i;
    int len=strlen(str);
    char *p;
    p=strtok(str," ");
    int mm;
    while(p!=NULL)
    {
       mm=atoi(p);
       map[r][mm]=1;
       p=strtok(NULL," ");
       if(p==NULL)
            break;
    }
}
void DP()
{
    int i,j;
    memset(dp,0,sizeof(dp));
    dp[1][1]=1;
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=m;j++)
        {
            if(i==1&&j==1)
                continue;
            if(map[i][j]==1)
                continue;
            else
                dp[i][j]=dp[i-1][j]+dp[i][j-1];
        }
    }
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        //int n,m;
        int i;
        scanf("%d%d",&n,&m);
        memset(map,0,sizeof(map));
        for(i=0;i<n;i++)
        {
            int r;
            scanf("%d",&r);
            gets(str);
            fun(str,r);
        }
        DP();
        printf("%lld\n",dp[n][m]);
        if(t)
            printf("\n");
    }
}



                    
版权声明:本文为博主原创文章,未经博主允许不得转载。

uva11324(tarjan缩点 + DAG图上的简单dp)

思路:先缩点,再构图,最后dp。dp[u] = max(dp[u],dfs(v) + val[u]); #include #include #include #include #includ...
  • KIJamesQi
  • KIJamesQi
  • 2015年10月02日 14:03
  • 503

[刷题]算法竞赛入门经典 3-10/UVa1587 3-11/UVa1588

题目:算法竞赛入门经典 3-10/UVa1587:Box 代码://UVa1587 - Box #include unsigned rect[6];//每个面各有一个一样的对应面,故12个边只要定义...
  • XieNaoban
  • XieNaoban
  • 2016年07月20日 14:25
  • 1664

经典状态dp题目

POJ 1185 炮兵阵地 一般这类题目列会很小10左右,我们需要枚举所有行,对于当前行,其放炮的位置只与上一行和上上行有关,我们记当前行状态和上一行状态便可转移了,dp数组开3维即可。 在枚举每...
  • waitfor_
  • waitfor_
  • 2014年01月29日 19:51
  • 2748

例题8-4 传说中的车 UVa11134

1.题目描述:点击打开链接 2.解题思路:这道题利用贪心法,由于横坐标和纵坐标没有任何关系,因此可以分别处理。对于横坐标或纵坐标,贪心策略是:先按照区间右端点从小到大排序,若相同,再按照区间左端点从大...
  • u014800748
  • u014800748
  • 2015年02月05日 21:14
  • 795

UVA dp题目汇总

UVa专题练习 A-4 10003 经典dp,可用四边形不等式优化 10029 基础dp,DAG最长路,需高效构图 10032 经典问题。子集和数问题。01背包问题 10036 能否在一个整...
  • nealgavin
  • nealgavin
  • 2013年04月21日 21:37
  • 704

第三章:UVa1588 解题报告

UVa1588 解题报告这道题可以直接模拟。因为齿轮是条状,高度只有两种,所以只要匹配字符串就可以了。两个字符串的相交区域计算高度相加是不是大于3,小于3便是合法的配对。处理A、B两个字符串时枚举B在...
  • ZpadT2
  • ZpadT2
  • 2015年05月06日 00:01
  • 982

最优矩阵链乘(动态规划dp)POJ 1651 Multiplication Puzzle

大体题意: 一个n*m的矩阵由n行m列共n*m排列而成。两个矩阵A和B可以相乘当且仅当A的列数等于B的行数。一个n*m的矩阵乘m*p的矩阵,运算量为n*m*p。 矩阵乘法不满足分配律,但满足结合律...
  • aozil_yang
  • aozil_yang
  • 2016年10月09日 20:18
  • 598

(题号)UVa OJ全部题目列表

 # ID Title Solved by Difficulty 1 100 The 3n + 1 problem 26485 1 2 102 ...
  • u013901393
  • u013901393
  • 2014年10月29日 18:14
  • 1793

UVA oj 练习水题 算法入门经典(大数系列)

最近做了很多白皮书(算法入门经典)上面的很多水题,但是考验编程的细心还是很考验的,把一些题记录在这儿 424 - Integer Inquiry  代码改的很烂,很不容易的过了: #include ...
  • y990041769
  • y990041769
  • 2013年07月07日 13:18
  • 3572

uva1601(双向BFS经典题)

题目描述:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=51163 首先用单向BFS解决,代码如下。最后在单向基础上改进成双...
  • qq_29169749
  • qq_29169749
  • 2016年05月15日 23:13
  • 1654
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:UVA 题目825 Walking on the Safe Side(DP)
举报原因:
原因补充:

(最多只允许输入30个字)