1 solutions
-
0
C :
#include<stdio.h> #include<string.h> int max(int a,int b) { return(a > b? a:b); } ///f[i,j]=max{f[i-1,j],f[i,j-1]}+map[i,j]; ///初始化f[1,1]=map[1,1]; int c[1005][1005]; int a[1005][1005]; int main() { int n; int i,j; scanf("%d",&n); memset(c,0,sizeof(c)); for(i = 1;i <= n;i++) for(j = 1;j <= n;j++) { scanf("%d",&a[i][j]); } c[1][1] = a[1][1]; for(i = 1;i <= n;i++) for(j = 1;j <= n;j++) { c[i][j] = a[i][j]+max(c[i-1][j],c[i][j-1]); } printf("%d\n",c[n][n]); /*for(i = 1;i <= n;i++) { for(j = 1;j <= n;j++) printf("%d ",c[i][j]); printf("\n"); }*/ return 0; }
C++ :
#include<iostream> //#include<fstream> using namespace std; const int N(1005); int a[N][N],f[N][N]; int main() { // ifstream cin("exam2.in"); // ofstream cout("exam2.out"); int n; cin>>n; for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) cin>>a[i][j]; f[2][1]=a[1][1]; for (int k=3;k<=n+n;k++) for (int x=1;x<=n;x++) if (k-x>=1&&k-x<=n) { if (x==1) f[k][x]=f[k-1][x]+a[x][k-x]; else if (k-x==1) f[k][x]=f[k-1][x-1]+a[x][k-x]; else f[k][x]=max(f[k-1][x-1],f[k-1][x])+a[x][k-x]; } cout<<f[2*n][n]<<endl; //system("pause"); return 0; }
Pascal :
var a:array[0..1001,0..1001]of longint; n,i,j:integer; begin readln(n); for i:=1 to n do for j:=1 to n do read(a[i,j]); for i:=1 to n do for j:=1 to n do if a[i-1,j]>a[i,j-1] then a[i,j]:=a[i,j]+a[i-1,j] else a[i,j]:=a[i,j]+a[i,j-1]; writeln(a[n,n]); end.
- 1
Information
- ID
- 730
- Time
- 1000ms
- Memory
- 128MiB
- Difficulty
- (None)
- Tags
- # Submissions
- 0
- Accepted
- 0
- Uploaded By