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

Re:代码升级,我同学这样写的AC了,为什么我的还是超时?

Posted by kanxue2002 at 2013-04-02 19:33:56 on Problem 1088
In Reply To:Re:还是超时,我已经记忆化搜索了,代码附上,求建议啊 Posted by:kanxue2002 at 2013-03-30 18:11:44
#include <cstdlib>
#include <iostream>
#include <stdio.h>
#include <cstring>
using namespace std;
int map[105][105];
int len[105][105];//最大高度
int dir[4][2]={{1,0},{0,-1},{-1,0},{0,1}};//四方向数组
int m,n;//行,列
int find(int x,int y){//x,y为当前坐标,h为当前点的高度
    if(len[x][y]>0)
        return len[x][y];
	int tx,ty,t;
	for(int k=0;k<4;++k){
        tx=x+dir[k][0];
		ty=y+dir[k][1];
		if(tx>=1&&ty>=1&&tx<=m&&ty<=n){
            if(map[x][y]>map[tx][ty]){
                t=find(tx,ty);
            if(len[x][y]<=t)
                len[x][y]=t+1;
            }
		}//行越界判断列越界判断
	}
	return len[x][y];
}
int main()
{
    int i,j;
	int max;//全地图最大高度
	while(scanf("%d%d",&m,&n)&&m&&n){
		max=-1;
		memset(len,0,sizeof(len));
		for(i=1;i<=m;++i)
			for(j=1;j<=n;++j){
				scanf("%d",&map[i][j]);//输入地图
			}
		for(i=1;i<=m;++i)
			for(j=1;j<=n;++j){
				find(i,j);//深搜
				if(max<len[i][j])
					max=len[i][j];//求全地图最大高度
				}
		printf("%d\n",max+1);
	}
    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