练习2
1.
cat.in
3 10 2
3 1 4 10
6 3 5 9 7 8 9
5 4 5 3 6 9
cat.out
8
题解
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int n,h,d;
int dp[100]; //高度为i时吃到的最多柿子
int mp[100][100];
int g[100];
int main(){
int a[100];
cin >>n>>h>>d;
for(int i = 1;i<=n;i++){
cin >>a[i];
for(int j = 1;j<=a[i];j++){
int p;
cin >>p;
mp[i][p]++;
}
}
for(int i = h;i>=1;i--){
int temp = 0;
if(i+d<=h){
temp = dp[i+d];
// cout <<temp<<endl;
}
for(int j = 1;j<=n;j++){
g[j] = max(g[j],temp) + mp[j][i];
dp[i] = max(dp[i],g[j]);
}
}
cout <<dp[1]<<endl;
return 0;
}
————————————————————————————————————————
2
不等数列
(seq.c/.cpp/.pas)
Description:
将1到n任意排列,然后在排列的每两个数之间根据他们的大小关系插入“>”和“<”。问在所有排列中,有多少个排列恰好有k个“<”。答案对2012取模。
Input
输入文件名为(seq.in)。
第一行2个整数n,k。
Output
输出文件名为(seq.out)。
一个整数表示答案。
seq.in
5 2
seq.out
66
Hint
对于30%的数据:n <= 10
对于100%的数据:k < n <= 1000
题解
#include <stdio.h>
#include <string.h>
#define MOD 2012
int n,k;
int f[1200][1200];
void pre_()
{
f[0][0]=1;
f[1][0]=1;
}
int main()
{
freopen("seq.in","r",stdin);
freopen("seq.out","w",stdout);
pre_();
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++)
{
f[i][0]=1;
for(int j=1;j<=k;j++)
{
f[i][j]=((f[i-1][j]*(j+1))%MOD+((i-j)*f[i-1][j-1])%MOD)%MOD;
f[i][i-1]=1;
}
}
printf("%d\n",f[n][k]%MOD);
return 0;
}
————————————————————————————————
3
清理垃圾
(clean.c/.cpp/.pas)
Description:
Candy家里总共有n个垃圾等待处理,每个垃圾对于Candy和飘飘乎居士处理的时间都是不同的,而且每个垃圾只需要一个人处理。当然,Candy和飘飘乎居士可以同时处理不同的垃圾。记两人中耗费最长时间为最后总时间。Candy希望能够尽快的处理完所有的垃圾,因此,他想要知道处理完这些垃圾最少需要耗费多少时间?
Input
输入文件名为(clean.in)。
第一行一个正整数n,表示一共有n个垃圾需要处理
接下来一个2*n的矩阵。
矩阵第一行第i个数表示candy处理第i个垃圾所需消耗的时间
矩阵第二行第i个数表示飘飘乎居士处理第i个垃圾所需消耗的时间
Output
输出文件名为(clean.out)。
一行,最后耗费的时间
clean.in
5
2 4 1 4 5
2 1 3 4 1
clean.out
5
答案https://blog.csdn.net/Z_Mendez/article/details/47344307?biz_id=102&utm_term=dp%E5%9F%BA%E7%A1%80%E4%B9%A0%E9%A2%98&utm_medium=distribute.pc_search_result.none-task-blog-2allsobaiduweb~default-4-47344307&spm=1018.2118.3001.4187
————————————————————————————————————————
4
有一个大小为N x M的园子,雨后积水。八连通的积水被认为是连在一起的。请求出园子里一共有多少水洼?
水洼如下:
w
1
2
3
输入:
10 12
W…WW.
.WWW…WWW
…WW…WW.
…WW.
…W…
…W…W…
.W.W…WW.
W.W.W…W.
.W.W…W.
…W…W.
1
2
3
4
5
6
7
8
9
10
11
输出:
3
答案https://blog.csdn.net/qq_43469554/article/details/97615308?biz_id=102&utm_term=dfs%E9%A2%98%E7%9B%AE&utm_medium=distribute.pc_search_result.none-task-blog-2allsobaiduweb~default-0-97615308&spm=1018.2118.3001.4187
题解
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
char s[100][100];
int n,m,ans;
bool vis[100][100];
int dir[8][2] = {{-1,0},{0,-1},{1,0},{0,1},{-1,-1},{1,-1},{1,1},{-1,1}};
void dfs(int x,int y){
vis[x][y] = true;
for(int i = 0;i<8;i++){
int tx = x+dir[i][0];
int ty = y+dir[i][1];
if(tx>=0&&tx<n&&ty>=0&&ty<m&&!vis[tx][ty]&&s[tx][ty] == 'W'){
dfs(tx,ty);
}
}
}
int main(){
cin>>n>>m;
for(int i = 0;i<n;i++){
cin >>s[i];
}
for(int i = 0;i<n;i++){
for(int j = 0;j<m;j++){
if(s[i][j] == 'W' && !vis[i][j]){
dfs(i,j);
ans++;
}
}
}
cout <<ans<<endl;
return 0;
}
——————————————————————————————————-————
5
原题链接:https://www.luogu.com.cn/problem/P1605
题解
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int mp[101][101];
bool vis[101][101];
int ans;
int n,m,t,sx,sy,tx,ty;
int dir[4][2] = {{-1,0},{0,-1},{1,0},{0,1}};
void dfs(int x,int y){
vis[x][y] = true;
if(x == tx && y == ty){
ans++;
return;
}
for(int i = 0;i<4;i++){
int xx = x+dir[i][0];
int yy = y+dir[i][1];
if(xx>=1&&xx<=n&&yy>=1&&yy<=m&&mp[xx][yy] != 1&&!vis[xx][yy]){
if(xx == tx && yy == ty){ //减枝
ans++;
return;
}
vis[xx][yy] = true;
//cout <<xx<<" "<<yy<<endl;
dfs(xx,yy);
vis[xx][yy] = false;
}
}
vis[x][y] = false;
}
int main(){
cin >>n>>m>>t;
cin>>sx>>sy>>tx>>ty;
int dx,dy;
for(int i = 0;i<t;i++){
cin>>dx>>dy;
mp[dx][dy] = 1;
}
dfs(sx,sy);
cout <<ans<<endl;
return 0;
}
————————————————————————————————————
6
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=2563
答案https://blog.csdn.net/weixin_44668898/article/details/104115884?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522159594557519195265945232%2522%252C%2522scm%2522%253A%252220140713.130102334…%2522%257D&request_id=159594557519195265945232&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2alltop_click~default-4-104115884.first_rank_ecpm_v3_pc_rank_v4&utm_term=dfs%E9%A2%98%E7%9B%AE&spm=1018.2118.3001.4187
题解
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
bool vis[101][101];
int ans;
int n,c;
int dir[4][2] = {{-1,0},{0,-1},{0,1}};
void dfs(int x,int y,int step){
vis[x][y] = true;
if(step == n){
ans++;
return;
}
for(int i = 0;i<3;i++){
int xx = x+dir[i][0];
int yy = y+dir[i][1];
if(!vis[xx][yy]){
vis[xx][yy] = true;
//cout <<xx<<" "<<yy<<endl;
dfs(xx,yy,step+1);
vis[xx][yy] = false;
}
}
}
int main(){
cin >>c;
while(c--){
memset(vis,0,sizeof(vis));
ans = 0;
cin >> n;
dfs(50,50,0);
cout <<ans<<endl;
}
return 0;
}
———————————————————————————————————
7
poj1426
过