本文主要是介绍uva 10913 Walking on a Grid,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
原题:
You will be given a square grid of size N × N. The top-left square has a coordinate of (1, 1) and that of bottom-right is (N,N). Your job is to walk from (1, 1) to (N,N). Very easy, right? Thats why you have to follow some rules when walking.
1. You can only move left, right or down.
2. (i,j − 1) is left of (i,j), (i,j + 1) is right of (i,j) and (i + 1,j) is down of (i,j).
3. You can never move outside the grid.
4. You can not step on a cell more than once.
5. Every cell has an integer associated with it.
6. You have to make sure the sum of integers of the path is maximized.
7. You can step on at most k negative integers from source to destination.
Input
Each case will start with two integers N and k. N ≤ 75 and k ≤ 5. Each of the next N lines will
contain N integers each given in row major order. That is, the first integer of the first row is (1, 1) and the last integer of last row is (N,N). Input terminates with two zeros on a line.
Output
For every case output the case number. If it’s not possible to reach the destination meeting the above rules then output ‘impossible’, else print the maximum sum of integers of the path.
Sample Input
4 1
1 2 3 -5
-10 6 0 -1
-10 -10 -10 2
0 0 0 1
4 0
1 2 3 -5
-10 6 0 -1
-10 -10 -10 2
0 0 0 1
0 0
Sample Output
Case 1: 11
Case 2: impossible
中文:
给你一个N×N的格子,每个格子里有数。现在问你,从(1,1)走到(N,N)。在路过k负数的情况下,最大路径的和是多少?只可以向下、向右、向左走。
#include<bits/stdc++.h>using namespace std;int inf=INT_MIN;
int mat[101][101];
int dp[101][101][10][10];
bool mark[101][101][10][10],vis[101][101];
int N,K;
int dir[3][2]= {{0,1},{1,0},{0,-1}};///0:right;1:down;2:left;bool judge(int i, int j)
{if (i<1 || i>N)return false;if (j<1 || j>N)return false;return true;
}int dfs(int i,int j,int k,int d)
{if(mark[i][j][k][d]){return dp[i][j][k][d];}if(k==0){mark[i][j][k][d]=1;dp[i][j][k][d]=inf;return inf;}if(i==N&&j==N){mark[i][j][k][d]=1;dp[i][j][k][d]=mat[i][j];return mat[i][j];}dp[i][j][k][d]=inf;for(int x=0; x<3; x++){int gi=i+dir[x][0];int gj=j+dir[x][1];if((x==0&&d==2)||(x==2&&d==0))continue;if(!vis[gi][gj]&&judge(gi,gj)){vis[gi][gj]=true;int temp;if(mat[i][j]<0)temp=dfs(gi,gj,k-1,x);elsetemp=dfs(gi,gj,k,x);if(temp!=inf)dp[i][j][k][d]=max(dp[i][j][k][d],temp+mat[i][j]);vis[gi][gj]=false;}}mark[i][j][k][d]=true;return dp[i][j][k][d];
}
int main()
{ios::sync_with_stdio(false);int t=0;while(cin>>N>>K,N+K){for(int i=1; i<=N; i++)for(int j=1; j<=N; j++)cin>>mat[i][j];memset(mark,false,sizeof(mark));memset(vis,false,sizeof(vis));if(mat[N][N]<0)K--;int res=dfs(1,1,K+1,1);cout<<"Case "<<++t<<": ";if(res==inf)cout<<"impossible"<<endl;elsecout<<res<<endl;}return 0;
}
思路:
刚开始看错题了,以为只能向下向右走。结果交了好几次都都不对,后来仔细读题才知道原来知道可以横向两个方向走。
方格的动态规划问题解决的套路比较普遍
首先考虑状态,肯定有两个维数作为状态。其次,由于要求走k个负数,所以负数的个数也要作为状态。
目前可以得到的状态为
dp[i][j][k]表示在第i行第j列走了k个负数的时候得到的最大值。但是,有个问题,考虑样例的数据。
4 1
1 2 3 -5
-10 6 0 -1
-10 -10 -10 2
0 0 0 1当k=0时得到的结果如下,x表示无法到达
1 3 6 x
x 12 9 x
x x x x
x x x x
k=1的时候
x x x 1
? 由于第2行第一列的元素可以从右侧过来,也可以从上方得到结果。但是右侧的结果还没有搜索到,无法得到正确答案。
此时增加一个状态,和某些广搜的题目类似,用来记录是否遍历的数组一定要选的恰当,同理动态规划问题。增加的状态是当前来的方向d,也就是从哪个方向移动到现在位置。
那么转移方程为
当mat[i][j]<0 时
dp[i][j][k][d]=max(dp[i][j][k][d],dfs(gi,gj,k-1,x)+mat[i][j]) (gi和gj是3个方向的走法,x是方向)
mat[i][j]>=0
dp[i][j][k][d]=max(dp[i][j][k][d],dfs(gi,gj,k,x)+mat[i][j])
这篇关于uva 10913 Walking on a Grid的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!