重生 | ||||||
| ||||||
Description | ||||||
Leyni是一名囚犯,因被监狱官陷害被捉入一个n * n正方形的监狱中。奸诈的监狱官决定把Leyni关在监狱最里面的房间里让他自生自灭,该牢房在整座监狱的左上角。Leyni为了重生决定逃走,而监狱的出口是在整座监狱的右下角,Leyni为了逃至出口必须要经过他当时身处牢房的下方或右方的牢房。但是每个牢房都会有一定数量(小于100)的看守人员把守每个牢房,Leyni必须击败身处牢房里所有的看守人员才能进入下一间牢房。由于Leyni力量有限,他需要找到一条看守人数最少的路径到达出口。现给出整数n(1 <= n <= 1000) 和监狱各牢房人数,请你算出Leyni要获得重生,至少要击败的看守人员的人数。 | ||||||
Input | ||||||
输入数据有多组,每组第一行是一个正整数n(1 <= n <= 1000),接着有n | ||||||
Output | ||||||
对于每组测试数据: | ||||||
Sample Input | ||||||
4 1 1 1 1 1 2 2 2 1 2 2 2 1 1 1 1 | ||||||
Sample Output | ||||||
7
|
#include<stdio.h>
#include<string>
using namespace std;
int a[1005][1005];
int dp[1005][1005];
int main()
{
int n;
while(scanf("%d",&n)!=EOF && n!=1)
{
int i,j;
for(i = 1; i <= n; ++i)
{
for(j = 1;j <= n; ++j)
{
scanf("%d",&a[i][j]); //读入数据
dp[i][j]=a[i][j];
}
}
for(j = n-1;j >= 1; --j)
{
dp[n][j]+=dp[n][j+1]; //处理第n行
dp[j][n]+=dp[j+1][n]; //处理第n列
}
for(i = n-1; i >= 1; --i )
{
for(j = n-1 ;j >= 1; --j )
{
dp[i][j]+=min(dp[i][j+1],dp[i+1][j]); //状态转移方程
}
}
printf("%d\n",dp[1][1]); //最小
}
return 0;
}