Poj 1088 題解

水題dp,其實就是記憶化搜索

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
int f[110][110];
int a[110][110],n,m;
void Input()
{
  scanf("%d%d",&n,&m);
  for(int i=1;i<=n;i++)
	for(int j=1;j<=m;j++)
	  scanf("%d",&a[i][j]);
}
int find_ans(int x,int y)
{
  if(f[x][y]!=0) return f[x][y];
  int ans=1;
  if(a[x+1][y]<a[x][y]&&x<n)
	ans=max(ans,find_ans(x+1,y)+1);
  if(a[x][y+1]<a[x][y]&&y<n)
	ans=max(ans,find_ans(x,y+1)+1);
  if(a[x-1][y]<a[x][y]&&x>1)
	ans=max(ans,find_ans(x-1,y)+1);
  if(a[x][y-1]<a[x][y]&&y>1)
	ans=max(ans,find_ans(x,y-1)+1);
  return f[x][y]=ans;
}
int maxx=0;
void Solve()
{
  maxx=0;
  memset(f,0,sizeof(f));
  for(int i=1;i<=n;i++)
	for(int j=1;j<=m;j++)
	  maxx=max(maxx,find_ans(i,j));
}
void Output()
{
  printf("%d\n",maxx);
}
int main()
{
  Input();
  Solve();
  Output();
  return 0;
}


發佈了39 篇原創文章 · 獲贊 3 · 訪問量 9997
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章