题目链接(hpuacm):vjudge.net/contest/245538
G - 滑雪
Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激。可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你。Michael想知道载一个区域中最长的滑坡。区域由一个二维数组给出。数组的每个数字代表点的高度。下面是一个例子
1 2 3 4 5 16 17 18 19 6 15 24 25 20 7 14 23 22 21 8 13 12 11 10 9
一个人可以从某个点滑向上下左右相邻四个点之一,当且仅当高度减小。在上面的例子中,一条可滑行的滑坡为24-17-16-1。当然25-24-23-...-3-2-1更长。事实上,这是最长的一条。
Input
输入的第一行表示区域的行数R和列数C(1 <= R,C <= 100)。下面是R行,每行有C个整数,代表高度h,0<=h<=10000。
Output
输出最长区域的长度。
Sample Input
5 5 1 2 3 4 5 16 17 18 19 6 15 24 25 20 7 14 23 22 21 8 13 12 11 10 9
Sample Output
25
这里没有给起始位置,所以用两个for循环dfs,每次让ans保持最大长度。
#include<bits/stdc++.h>
using namespace std;
int arr[105][105];
int dp[105][105];
int b[4][2] = {1,0,-1,0,0,1,0,-1};
int r, c;
int dfs(int x, int y)
{
int res = 1;
if(dp[x][y]) return dp[x][y];
for(int i = 0; i < 4; i++)
{
int dx = x + b[i][0];
int dy = y + b[i][1];
if(dx>=0&&dx<r&&dy>=0&&dy<c&&arr[x][y]>arr[dx][dy])
{
res = max(res, dfs(dx, dy) + 1);
}
}
dp[x][y] = res;
return res;
}
int main()
{
cin>>r>>c;
memset(dp, 0, sizeof dp);
for(int i = 0; i < r; i++)
for(int j = 0; j < c; j++)
{
cin>>arr[i][j];
}
int ans = 0;
for(int i = 0; i < r; i++)
for(int j = 0; j < c; j++)
{
ans = max(ans, dfs(i, j));
}
cout<<ans<<endl;
return 0;
}
H - Function Run Fun
We all love recursion! Don't we?
Consider a three-parameter recursive function w(a, b, c):
if a <= 0 or b <= 0 or c <= 0, then w(a, b, c) returns:
1
if a > 20 or b > 20 or c > 20, then w(a, b, c) returns:
w(20, 20, 20)
if a < b and b < c, then w(a, b, c) returns:
w(a, b, c-1) + w(a, b-1, c-1) - w(a, b-1, c)
otherwise it returns:
w(a-1, b, c) + w(a-1, b-1, c) + w(a-1, b, c-1) - w(a-1, b-1, c-1)
This is an easy function to implement. The problem is, if implemented directly, for moderate values of a, b and c (for example, a = 15, b = 15, c = 15), the program takes hours to run because of the massive recursion.
Input
The input for your program will be a series of integer triples, one per line, until the end-of-file flag of -1 -1 -1. Using the above technique, you are to calculate w(a, b, c) efficiently and print the result.
Output
Print the value for w(a,b,c) for each triple.
Sample Input
1 1 1 2 2 2 10 4 6 50 50 50 -1 7 18 -1 -1 -1
Sample Output
w(1, 1, 1) = 2 w(2, 2, 2) = 4 w(10, 4, 6) = 523 w(50, 50, 50) = 1048576 w(-1, 7, 18) = 1
当然是直接按照题意敲喽,然后优化一下,恩...
#include<bits/stdc++.h>
using namespace std;
int dp[30][30][30];
long long w(long long a, long long b, long long c)
{
if(a<=0||b<=0||c<=0) return 1;
if(a>20||b>20||c>20) return w(20, 20, 20);
if(dp[a][b][c])
return dp[a][b][c];
if(a<b&&b<c)
dp[a][b][c] = w(a, b, c-1) + w(a, b-1, c-1) - w(a, b-1, c);
else
dp[a][b][c] = w(a-1, b, c) + w(a-1, b-1, c) + w(a-1, b, c-1) - w(a-1, b-1, c-1);
return dp[a][b][c];
}
int main()
{
long long z, x, c;
memset(dp, 0, sizeof dp);
while(cin>>z>>x>>c)
{
if(z==-1&&x==-1&&c==-1)
break;
printf("w(%lld, %lld, %lld) = %lld\n", z, x, c, w(z, x, c));
}
return 0;
}
J - Hex-a-bonacci
Given a code (not optimized), and necessary inputs, you have to find the output of the code for the inputs. The code is as follows:
int a, b, c, d, e, f;
int fn( int n ) {
if( n == 0 ) return a;
if( n == 1 ) return b;
if( n == 2 ) return c;
if( n == 3 ) return d;
if( n == 4 ) return e;
if( n == 5 ) return f;
return( fn(n-1) + fn(n-2) + fn(n-3) + fn(n-4) + fn(n-5) + fn(n-6) );
}
int main() {
int n, caseno = 0, cases;
scanf("%d", &cases);
while( cases-- ) {
scanf("%d %d %d %d %d %d %d", &a, &b, &c, &d, &e, &f, &n);
printf("Case %d: %d\n", ++caseno, fn(n) % 10000007);
}
return 0;
}
Input
Input starts with an integer T (≤ 100), denoting the number of test cases.
Each case contains seven integers, a, b, c, d, e, f and n. All integers will be non-negative and 0 ≤ n ≤ 10000 and the each of the others will be fit into a 32-bit integer.
Output
For each case, print the output of the given code. The given code may have integer overflow problem in the compiler, so be careful.
Sample Input
5
0 1 2 3 4 5 20
3 2 1 5 0 1 9
4 12 9 4 5 6 15
9 8 7 6 5 4 3
3 4 3 2 54 5 4
Sample Output
Case 1: 216339
Case 2: 79
Case 3: 16636
Case 4: 6
Case 5: 54
好吧,我承认我超时了555。我以为只对每个fn取余就行,还是老实用dp加速一下。
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e7 +7;
int a, b, c, d, e, f;
int dp[10005];
int fn( int n )
{
if( n == 0 ) return a;
if( n == 1 ) return b;
if( n == 2 ) return c;
if( n == 3 ) return d;
if( n == 4 ) return e;
if( n == 5 ) return f;
if(dp[n]) return dp[n];
return dp[n] = (fn(n-1)%maxn + fn(n-2)%maxn + fn(n-3)%maxn + fn(n-4)%maxn + fn(n-5)%maxn + fn(n-6)%maxn)%maxn;
}
int main()
{
int n, caseno = 0, cases;
scanf("%d", &cases);
while(cases--)
{
memset(dp, 0, sizeof dp);
scanf("%d %d %d %d %d %d %d", &a, &b, &c, &d, &e, &f, &n);
printf("Case %d: %d\n", ++caseno, fn(n)%maxn);
}
return 0;
}
L - FatMouse and Cheese
FatMouse has stored some cheese in a city. The city can be considered as a square grid of dimension n: each grid location is labelled (p,q) where 0 <= p < n and 0 <= q < n. At each grid location Fatmouse has hid between 0 and 100 blocks of cheese in a hole. Now he's going to enjoy his favorite food.
FatMouse begins by standing at location (0,0). He eats up the cheese where he stands and then runs either horizontally or vertically to another location. The problem is that there is a super Cat named Top Killer sitting near his hole, so each time he can run at most k locations to get into the hole before being caught by Top Killer. What is worse -- after eating up the cheese at one location, FatMouse gets fatter. So in order to gain enough energy for his next run, he has to run to a location which have more blocks of cheese than those that were at the current hole.
Given n, k, and the number of blocks of cheese at each grid location, compute the maximum amount of cheese FatMouse can eat before being unable to move.
Input
There are several test cases. Each test case consists of
a line containing two integers between 1 and 100: n and k
n lines, each with n numbers: the first line contains the number of blocks of cheese at locations (0,0) (0,1) ... (0,n-1); the next line contains the number of blocks of cheese at locations (1,0), (1,1), ... (1,n-1), and so on.
The input ends with a pair of -1's.
Output
For each test case output in a line the single integer giving the number of blocks of cheese collected.
Sample Input
3 1 1 2 5 10 11 6 12 12 7 -1 -1
Sample Output
37
注意它可以最多走k步,在dfs中需加一层循环。这里给个链接https://blog.csdn.net/jingdianitnan/article/details/41546005
#include <bits/stdc++.h>
using namespace std;
int arr[105][105];
int dp[105][105];
int b[4][2] = {1,0,-1,0,0,1,0,-1};
int n, k;
int dfs(int x, int y)
{
if(dp[x][y]) return dp[x][y];
int ans = 0;
for(int j = 1; j <= k; j++)
{
for(int i = 0; i < 4; i++)
{
int dx = x + b[i][0]*j;
int dy = y + b[i][1]*j;
if(dx>=0&&dx<n&&dy>=0&&dy<n&&arr[dx][dy]>arr[x][y])
{
int tmp = dfs(dx, dy);
if(ans<tmp) ans = tmp;
}
}
}
dp[x][y] = ans + arr[x][y];
return dp[x][y];
}
int main()
{
while(cin>>n>>k&&n!=-1&&k!=-1)
{
memset(dp, 0, sizeof dp);
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
cin>>arr[i][j];
}
}
cout<<dfs(0,0)<<endl;
}
return 0;
}