CSU-ACM2016暑假集训比赛5

来源:互联网 发布:itunes发生网络错误 编辑:程序博客网 时间:2024/06/05 01:59

A - A HDU 1078
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Submit

Status

Practice

HDU 1078
Description
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

A: 记忆化搜索#include<stdio.h>#include<string>#include<cstring>#include<queue>#include<algorithm>#include<functional>#include<vector>#include<iomanip>#include<math.h>#include<iostream>#include<sstream>#include<set>using namespace std;const int dX[4]= {-1,0,0,1};const int dY[4]= {0,1,-1,0};int pic[105][105],F[105][105],n,k;int dfs(int x,int y){    int MAXF=0;    if (!F[x][y])    {        for (int i=1; i<=k; i++)            for (int j=0; j<4; j++)            {                int nx=x+i*dX[j];                int ny=y+i*dY[j];                if (nx>=0&&nx<n&&ny>=0&&ny<n&&pic[nx][ny]>pic[x][y])                    MAXF=max(MAXF,dfs(nx,ny));            }        F[x][y]=MAXF+pic[x][y];    }    return F[x][y];}int main(){    while(cin>>n>>k)    {        memset(F,0,sizeof(F));        if (n==-1&&k==-1) break;        for (int i=0; i<n; i++)            for (int j=0; j<n; j++)                cin>>pic[i][j];        cout<<dfs(0,0)<<endl;    }    return 0;}

E - E HDU 2102
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Submit

Status

Practice

HDU 2102
Description
可怜的公主在一次次被魔王掳走一次次被骑士们救回来之后,而今,不幸的她再一次面临生命的考验。魔王已经发出消息说将在T时刻吃掉公主,因为他听信谣言说吃公主的肉也能长生不老。年迈的国王正是心急如焚,告招天下勇士来拯救公主。不过公主早已习以为常,她深信智勇的骑士LJ肯定能将她救出。
现据密探所报,公主被关在一个两层的迷宫里,迷宫的入口是S(0,0,0),公主的位置用P表示,时空传输机用#表示,墙用*表示,平地用.表示。骑士们一进入时空传输机就会被转到另一层的相对位置,但如果被转到的位置是墙的话,那骑士们就会被撞死。骑士们在一层中只能前后左右移动,每移动一格花1时刻。层间的移动只能通过时空传输机,且不需要任何时间。
Input
输入的第一行C表示共有C个测试数据,每个测试数据的前一行有三个整数N,M,T。 N,M迷宫的大小N*M(1 <= N,M <=10)。T如上所意。接下去的前N*M表示迷宫的第一层的布置情况,后N*M表示迷宫第二层的布置情况。
Output
如果骑士们能够在T时刻能找到公主就输出“YES”,否则输出“NO”。
Sample Input
1
5 5 14
S*#*.
.#…
…..
**.
…#.

..*.P

.*..

*..
…*.
*.#..
Sample Output
YES

E:  BFS#include<stdio.h>#include<string>#include<cstring>#include<queue>#include<algorithm>#include<functional>#include<vector>#include<iomanip>#include<math.h>#include<iostream>#include<sstream>#include<set>using namespace std;const int dX[4]= {-1,0,0,1};const int dY[4]= {0,1,-1,0};char pic[2][12][12];int used[2][12][12];struct point{    int x,y,z,time;    point(int a,int b,int c,int d):x(a),y(b),z(c),time(d) {};};int main(){    int C,N,M,T;    cin>>C;    while(C--)    {        bool flag=false;        memset(used,0,sizeof(used));        cin>>N>>M>>T;        for (int i=0; i<N; i++)            for (int j=0; j<M; j++)                cin>>pic[0][i][j];        for (int i=0; i<N; i++)            for (int j=0; j<M; j++)                cin>>pic[1][i][j];        for (int z=0; z<2; z++)            for (int i=0; i<N; i++)                for (int j=0; j<M; j++)                    if (pic[z][i][j]=='#'&&pic[z^1][i][j]=='*')                        pic[z][i][j]='*';                    else if (pic[z][i][j]=='#'&&pic[z^1][i][j]=='#')                    {                        pic[z][i][j]='*';                        pic[z^1][i][j]='*';                    }        queue<point> Q;        Q.push(point(0,0,0,0));        while (!Q.empty())        {            point u=Q.front();            //cout<<u.x<<" "<<u.y<<" "<<u.z<<" "<<u.time<<endl;            Q.pop();            used[u.z][u.x][u.y]=1;            if (u.time>T) break;            if (pic[u.z][u.x][u.y]=='P')            {                flag=true;                break;            }            for (int i=0; i<4; i++)            {                int nx=u.x+dX[i];                int ny=u.y+dY[i];                int nz=u.z;                int ntime=u.time+1;                if (nx<0||nx>=N||ny<0||ny>=M||used[nz][nx][ny])                    continue;                if (pic[nz][nx][ny]=='#')                {                    nz=nz^1;                    Q.push(point(nx,ny,nz,ntime));                }                else if(pic[nz][nx][ny]!='*')                    Q.push(point(nx,ny,nz,ntime));            }        }        if (flag) cout<<"YES"<<endl;        else cout<<"NO"<<endl;    }    return 0;}

F - F
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Submit

Status

Practice

HDU 1003
Description
Given a sequence a[1],a[2],a[3]……a[n], your job is to calculate the max sum of a sub-sequence. For example, given (6,-1,5,4,-7), the max sum in this sequence is 6 + (-1) + 5 + 4 = 14.
Input
The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line starts with a number N(1<=N<=100000), then N integers followed(all the integers are between -1000 and 1000).
Output
For each test case, you should output two lines. The first line is “Case #:”, # means the number of the test case. The second line contains three integers, the Max Sum in the sequence, the start position of the sub-sequence, the end position of the sub-sequence. If there are more than one result, output the first one. Output a blank line between two cases.
Sample Input
2
5 6 -1 5 4 -7
7 0 6 -1 1 -6 7 -5
Sample Output
Case 1:
14 1 4

Case 2:
7 1 6

F: 最大序列和 dp#include<stdio.h>#include<string>#include<cstring>#include<queue>#include<algorithm>#include<functional>#include<vector>#include<iomanip>#include<math.h>#include<iostream>#include<sstream>#include<set>using namespace std;const int MAX=100005;int main(){    int F[MAX],A[MAX],T,N,MAXF,st,en,temp;    cin>>T;    for (int a=1; a<=T; a++)    {        cin>>N;        for (int i=1; i<=N; i++)            cin>>A[i];        F[1]=A[1];        for (int i=2; i<=N; i++)            if (F[i-1]<0)                F[i]=A[i];            else                F[i]=F[i-1]+A[i];        MAXF=-2000;        for (int i=1; i<=N; i++)            if (MAXF<F[i])            {                MAXF=F[i];                en=i;            }        temp=0;        for (int i=en;i>=1;i--)        {            temp+=A[i];            if (temp==MAXF)                st=i;        }        cout<<"Case "<<a<<":"<<endl<<MAXF<<" "<<st<<" "<<en<<endl;        if (a!=T) cout<<endl;    }    return 0;}
0 0