Uva--825(动规,记忆化搜索)
2014-08-02 16:28:19
2014-08-02 16:25:51
Walking on the Safe Side |
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.
![\epsfbox{p825.eps}](http://uva.onlinejudge.org/external/8/p825.jpg)
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 North-South 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
思路:坑题,乍一看是个单纯的搜索题,后来上了Uva论坛才知道数据非常水,水到只用考虑每次向右、向下走的情况。。。那么直接DP就好了 dp[i][j] = dp[i - 1][j] + dp[i][j - 1]
注意初始化,即可。
1 /************************************************************************* 2 > File Name: h.cpp 3 > Author: Nature 4 > Mail: 564374850@qq.com 5 > Created Time: Wed 30 Jul 2014 09:31:21 PM CST 6 ************************************************************************/ 7 8 #include <cstdio> 9 #include <cstring> 10 #include <cstdlib> 11 #include <cmath> 12 #include <sstream> 13 #include <iostream> 14 #include <algorithm> 15 using namespace std; 16 #define ll long long 17 18 int Case; 19 int n,m; 20 int g[500][500]; 21 ll dp[500][500]; 22 string s; 23 24 int main(){ 25 int a,b; 26 char c; 27 scanf("%d",&Case); 28 getchar(); 29 getchar(); 30 while(Case--){ 31 memset(dp,0,sizeof(dp)); 32 memset(g,0,sizeof(g)); 33 dp[0][1] = 1; 34 scanf("%d%d",&n,&m); 35 getchar(); 36 for(int i = 0; i < n; ++i){ 37 getline(cin,s); 38 stringstream sin(s); 39 sin >> a; 40 while(sin >> b) 41 g[a][b] = 1; 42 } 43 for(int i = 1; i <= n; ++i){ 44 for(int j = 1; j <= m; ++j){ 45 if(g[i][j]) 46 continue; 47 dp[i][j] = dp[i - 1][j] + dp[i][j - 1]; 48 } 49 } 50 printf("%lld\n",dp[n][m]); 51 if(Case) 52 printf("\n"); 53 } 54 return 0; 55 }