Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

记忆化搜索怎么过不了了..

Posted by wjsbaq at 2007-08-10 15:55:29 on Problem 1088
虽然40K memory的肯定是填表(递推),但是记忆化搜索肯定可以过的...我没加任何优化,0MS过了..程序也就44行,这还包括我习惯的空行.

写法其实很简单,标准DFS模板加上记忆就可以了

这里是程序,没过的可以看看

#include <stdio.h>

int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};

int map[101][101],d[101][101];

int xborder,yborder;

int f(int x,int y){
 if(d[x][y]) return d[x][y];
 int i,rec=0,t=0,tx,ty;
 for(i=0;i<4;i++){
  tx=x+dx[i];
  ty=y+dy[i];
  if(map[tx][ty]>=map[x][y] || tx<1 || ty<1 || tx>xborder || ty>yborder) continue;
  t=f(tx,ty);
  if(t>rec) rec=t;
 }
 rec+=1;
 d[x][y]=rec;
 return rec;
}

int main(){
 freopen("pku1088.in","r",stdin);
 freopen("pku1088.out","w",stdout);
 scanf("%d %d\n",&xborder,&yborder);
 int i,j;
 for(i=1;i<=xborder;i++){
  for(j=1;j<=yborder;j++)
   scanf("%d",&map[i][j]);
  scanf("\n");
 }
 int now=0,best=0;
 for(i=1;i<=xborder;i++)
  for(j=1;j<=yborder;j++){
   now=f(i,j);
   if(now>best) best=now;
  }
 printf("%d",best);
 return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator