http://acm.nefu.edu.cn/JudgeOnline/problemshow.php?problem_id=627
description |
Recently Raven is addicted to paper cutting games. There is a rectangle-shape paper. Its length is N, and its width is M. On the face side, there are letters in each cell (capital letters A-Z). There are scores on the back side of each letter. A word can be made up of adjacent letters in the paper. Note that adjacency is defined as one of the four following directions only (left, right, up and down). Now a word will be given to you. Please cut this word in the paper to get the highest score of the word. The score of the word is the sum of the score of the letters in the word.
|
input |
|
output |
|
sample_input |
|
sample_output |
|
代码:
#include <string.h> #include <stdio.h> #include <iostream> using namespace std; int b[1005][1005]; char a[1005][1005],c[30]; int tt,ans,m,maxx,dd,n; void dfs(int x,int y,int k) { if(k>=dd) { if(maxx<ans) maxx=ans; return; } int tx[]={1,-1,0,0}; int ty[]={0,0,-1,1}; for(int i=0;i<4;i++) { int xx=tx[i]+x; int yy=ty[i]+y; if(a[xx][yy]==c[k]&&xx>=0&&xx<n&&yy>=0&&yy<m) { ans+=b[xx][yy]; dfs(xx,yy,k+1); ans-=b[xx][yy]; } } } int main() { int t; scanf("%d",&t); while(t--) { memset(c,0,sizeof(c)); memset(a,0,sizeof(a)); scanf("%d%d",&n,&m); getchar(); for(int i=0;i<n;i++) scanf("%s",a[i]); for(int i=0;i<n;i++) for(int j=0;j<m;j++) { scanf("%d",&b[i][j]); } scanf("%s",c); dd=strlen(c); int d; maxx=-1; for(int i=0;i<n;i++) for(int j=0;j<m;j++) if(a[i][j]==c[0]) { ans=b[i][j]; dfs(i,j,1); } printf("%d\n",maxx); } return 0; }