C + + pen questions snake problem

Source: Internet
Author: User

Greedy snake

Now there is a square rectangle of n*m (n,m=100), a random value on each square of the rectangle, a cute little snake starting from the upper left corner of the rectangle, each move can only move one block, to the right or down, and every time to reach a greedy little snake will bar the value of the location to eat a whole, Stop until the lower right corner is reached. And the greedy little snake is not afraid to die, it just want to eat the most, and output path.
#include <iostream>#include<string>#include<cstring>#include<vector>#include<sstream>#include<algorithm>#include<stdlib.h>#include<stdio.h>#include<stack>#include<ctime>using namespaceStd;stack<int>St;intvalue[ -][ -],best[ -][ -],n,m;//value that stores the values of each lattice//Best store goes to the maximum value of the current grid//m,n for lattice size 100*100voidInitValue () { for(intI=0; i<m;i++)         for(intj=1; j<n;j++) {srand (Unsigned (Time (NULL))); VALUE[I][J]= (rand ()% ( +-1+1))+1;//[A, b]} value[0][0]=0;}intSearchmaxvalue () {best[0][0]=0;  for(intI=1; i<m;i++) {best[i][0]=value[i][0]+best[i-1][0]; }     for(intj=1; j<n;j++) {best[0][j]+=value[0][j]+best[0][j-1]; }     for(intI=1; i<m;i++)         for(intj=1; j<n;j++) {Best[i][j]=value[i][j]+max (best[i-1][j],best[i][j-1]); }    returnbest[m-1][n-1];}void Pu (int n, int m) {St.push (n);        St.push (m);        if (n = = 0 && m = = 0) return;        else if (n = = 0 && m > 0) Pu (n, m-1);        else if (n > 0 && m = = 0) Pu (n-1, M);            else {if (Best[n-1][m] > Best[n][m-1]) Pu (n-1, M);        else Pu (n, m-1); }}voidprint () {Pu ( About, About);  while(!St.empty ()) {cout<<st.top () <<"__";        St.pop (); cout<<St.top ();        St.pop (); cout<<Endl; }}intMain () {m= -; N= -;   InitValue (); intBestvalue=Searchmaxvalue (); cout<<Bestvalue;   Print (); //after initializing the value matrix, calculate the maximum value matrix and then output the path}

There's something wrong with the path output. Himself has not solved, after the update, dynamic planning problems, lock pen questions.

The red part is a bit of a problem for reference. Hope Guide

C + + pen questions snake problem

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.