P1004 方格取数

//别人的AC
#include <iostream>
#include <cstring>
using namespace std;

int N;
int Map[10][10];
int dp[10][10][10][10];

int main()
{
        //init
        int x,y,num;
        cin>>N;
        memset(Map,0,sizeof(Map));
        memset(dp,0,sizeof(dp));

        //input
        while(cin>>x>>y>>num,x!=0&&y!=0&&num!=0)
        {Map[x][y]=num;}

        //dp
        for(int i=1;i<=N;i++)
                for(int j=1;j<=N;j++)
                        for(int k=1;k<=N;k++)
                                for(int l=1;l<=N;l++){
                                        dp[i][j][k][l]=max(
                                        max(dp[i-1][j][k-1][l],dp[i-1][j][k][l-1]),
                                        max(dp[i][j-1][k-1][l],dp[i][j-1][k][l-1]))
                                        +Map[i][j]+Map[k][l];
                                        if(i==k&&j==l) dp[i][j][k][l]-=Map[i][j];
                                }

        //output
        cout<<dp[N][N][N][N];
}
//我的20分:-(
#include <iostream>
using namespace std;

//The block
int N,x,y;
int num[11][11]={0};
//Flag of the first search
int flag[11][11]={0};

//Move function
int move(int x, int y){
        //Add self, mark, and move another step
        int sum=num[x][y], sumx, sumy;
        flag[x][y] = 1;
        if(x<N && y<N){
                sumx = move(x+1,y);
                sumy = move(x, y+1);
        }
        else if(x==N && y<N){
                sumx = 0;
                sumy = move(x, y+1);
        }
        else if(x<N && y==N){
                sumx = move(x+1, y);
                sumy = 0;
        }
        else{
                return num[N][N];
        }

        //Add, and mark
        if(sumx>=sumy){
                sum+=sumx;
                flag[x][y+1] = 0;
        }
        else{
                sum+=sumy;
                flag[x+1][y] = 0;
        }
        return sum;
}

void del(){
        for(int i=0; i<N; i++)
                for(int j=0; j<N; j++){
                        if(flag[i][j]==1 && flag[i+1][j]==1 && flag[i][j+1]==0)
                                num[i][j]=0;
                }
}

int main(){
        //Handle input
        cin >> N;
        for(int i=0; ; i++){
                cin >> x >> y ;
                if(x==0) break;
                else cin >> num[x][y];
        }
        //First move
        int sum;
        sum = move(1,1);
        //del num selected
        del();
        //second move
        sum += move(1,1);
        //Output
        cout << sum << endl;
        return 0;
}

dp,阿吧啊吧啊把