zoukankan      html  css  js  c++  java
  • wiki1169-传纸条(dp)

    http://wikioi.com/problem/1169/

    四维数组和三维数组;

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<algorithm>
    #include<bitset>
    #include<iomanip>
    
    using namespace std;
    int num[ 55 ][ 55 ] ;
    int dp[ 55 ][ 55 ][ 55 ][ 55 ] ;
     
    int main()
    {
    	int n , m ;
    	while( scanf( "%d%d" , &n , &m ) != EOF )
    	{
    		memset( dp , 0 , sizeof( dp ) ) ;
    		for( int i = 1 ; i <= n ; ++i )
    			for( int j = 1 ; j <= m ; ++j )
    				scanf( "%d" , &num[ i ][ j ] ) ;
    		for( int i1 = 1 ; i1 <= n ; ++i1 )
    			for( int j1 = 1 ; j1 <= m ; ++j1 )
    				for( int i2 = 1 ; i2 <= n ; ++i2 )
    					for( int j2 = 1 ; j2 <= m ; ++j2 )
    					{
    						int temp = max( max( dp[ i1 - 1 ][ j1 ][ i2 -1 ][ j2 ] , dp[ i1 - 1][ j1  ][ i2 ][ j2 - 1] ) , max( dp[ i1 ][ j1 -1 ][ i2 -1 ][ j2 ] , dp[ i1 ][ j1 - 1 ][ i2 ][ j2 - 1 ])) ;
    						if( i1 == i2 && j1 == j2 )	
    							dp[ i1 ][ j1 ][ i2 ][ j2 ] = num[ i1 ][ j1 ] + temp ;
    						else
    							dp[ i1 ][ j1 ][ i2 ][ j2 ] = num[ i1 ][ j1 ] + num[ i2 ][ j2 ] + temp ;
    					}
    		printf( "%d
    " , dp[ n ][ m ][ n ][ m ] ) ;
    	} 
    	return 0 ;
    }
    



    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<algorithm>
    #include<bitset>
    #include<iomanip>
    
    using namespace std;
    
    int m , n , mapp[ 51 ][ 51 ] , f[ 101 ][ 51 ][ 51 ] ;
    
    void dp()
    {
    	int i , j , k , s , t , Max = 0 ;
    	for( i = 1 ; i <= m + n - 1 ; ++i )
    	{
    		t = ( i > m ) ? m : i ;
    		s = ( i > n ) ? n : i ;
    		for( j = i - s + 1 ; j <= t ; ++j )
    		{
    			for( k = i - s + 1 ; k <= t ; ++k )
    				if( j != k || i == m + n - 1 )
    				{
    					Max = 0 ;
    					if( Max < f[ i - 1 ][ j ][ k ] )
    						Max = f[ i - 1 ][ j ][ k ] ;
    					if( Max < f[ i - 1 ][ j - 1 ][ k ] )
    						Max = f[ i - 1 ][ j - 1 ][ k ] ;
    					if( Max < f[ i - 1 ][ j - 1 ][ k - 1 ] )
    						Max = f[ i - 1 ][ j - 1 ][ k - 1 ] ;
    					if( Max < f[ i - 1 ][ j ][ k - 1 ] )
    						Max = f[ i - 1 ][ j ][ k - 1] ;
    					f[ i ][ j ][ k ] = Max + mapp[ j ][ i - j + 1 ] + mapp[ k ][ i - k + 1 ] ;
    				}
    		} 
    	
    	}
    }
    int main()
    {
    	while( cin >> m >> n  )
    	{	
    		int  i , j ;
    		for( i = 1 ; i <= m ; ++i )
    			for( j = 1 ; j <= n ; ++j )
    				cin >> mapp[ i ][ j ] ;
    		dp() ;
    		cout << f[ m + n - 1 ][ m ][ m ] << endl ;
    	}
    	return 0 ;
    }
    


  • 相关阅读:
    IOS数组
    caffe-fasterrcnn程序理解
    pytorch官网上两个例程
    python:批量修改文件名批量修改图片尺寸
    faster rcnn报错:TypeError: slice indices must be integers or None or have an __index__ method
    faster-rcnn原理讲解
    caffe + ssd网络训练过程
    运行Keras版本的Faster R-CNN(1)
    Ubuntu16.04 faster-rcnn+caffe+gpu运行环境配置以及解决各种bug
    ubuntu16+caffe fast-rcnnCPU运行步骤
  • 原文地址:https://www.cnblogs.com/javawebsoa/p/3236913.html
Copyright © 2011-2022 走看看