Discussion on the efficiency of the digital game algorithm in C language example _c language

Source: Internet
Author: User

Recently did such a topic, the feeling is very interesting ~ The topic is as follows:

Problem description

Winder recently played a number game, the game is on a n*m grid, each lattice has a number, representing the value of this lattice. The player needs to move from the grid's upper-left corner to the lower-right corner, each time only to the right or down, and cannot turn back. Every time a player passes a grid, he or she can choose whether the value is added to the grid, and the initial score for each game is 0.

Winder wants to know how many points he can get in each game. But, Winder is very lazy, so you must help him to finish this matter.

Data entry

Enter the first line of two positive integers N and M (0<n, m<=15). Next there are n rows, and m integers per line.

Data output

The output line is an integer that represents the maximum score sum that the game can achieve. (Guaranteed sum is within a 32-bit integer range).

The above problem is numbergame, considering every step has and only right and left two choices, so the recursive algorithm will be very convenient, the code is as follows:

Copy Code code as follows:
#include <stdio.h>
#include <iostream>
#include <windows.h>
#pragma comment (lib, "Winmm.lib")
using namespace Std;
int j=0;
int go (int kc,int *ac,int wc,int NC)
{
if (kc>=j) return WC;
if (KC&LT;J)
{
if ((kc+1)%5==0)
Return to go (KC+NC,AC,AC[KC]+WC,NC);
Else
Return to Go (KC+1,AC,AC[KC]+WC,NC) >go (KC+NC,AC,AC[KC]+WC,NC) "Go" (KC+1,AC,AC[KC]+WC,NC): Go (KC+NC,AC,AC[KC]+WC,NC) ;
}
}
void Main ()
{
int m,n;
DWORD T1, T2;
cin>>m>>n;
int *a,i,w=0;
a=new int [m*n];
for (i=0;i<m*n;i++)
{
if (i!=0&&i%n==0) cout<<endl;
cin>>a[i];
}
J=m*n;
T1=timegettime ();
int Max=go (0,A,W,N);
cout<<max;
T2=timegettime ();
cout<< "The time it takes:" <<t2-t1;
}

Code execution time is 46MS, because the predecessor of each node on the maximum weight path can only be the node above it or its left node (except the leftmost node), so a one-dimensional array can be used to store the maximum weights of each node precursor, as follows:

Copy Code code as follows:

#include <stdio.h>
int i,j,dp[16],n,m,v;
void Main () {
scanf ("%d%d", &n,&m);
for (i=0;i<n;i++)
for (j=1;j<=m;j++) {
scanf ("%d", &v);
if (Dp[j]<dp[j-1]) dp[j] = dp[j-1];
dp[j]+= v>0?v:0;
}
printf ("%d\n", Dp[m]);
}

This code uses a similar iterative algorithm, the code execution time is 30MS, know this code efficiency is more efficient than the above code, and the code is much simpler than the former.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.