Zoj2235 poj1928 hdu1355 the peanuts

Source: Internet
Author: User

There is no greedy difficulty. The policy question has been given. Data is pre-processed and sorted from large to small by the number of peanuts.


/******************************************************************************* # Author : Neo Fung # Email : neosfung@gmail.com # Last modified: 2012-02-24 19:13 # Filename: ZOJ2235 POJ1928 HDU1355 The Peanuts.cpp # Description :  ******************************************************************************/#ifdef _MSC_VER#define DEBUG#define _CRT_SECURE_NO_DEPRECATE#endif#include <fstream>#include <stdio.h>#include <iostream>#include <string.h>#include <string>#include <limits.h>#include <algorithm>#include <math.h>#include <numeric>#include <functional>#include <ctype.h>#define MAX 3000using namespace std;struct NODE{  int x,y,p;  bool operator<(const NODE &t) const  {    return p>t.p;  }}node[MAX];int main(void){#ifdef DEBUG    freopen("../stdin.txt","r",stdin);  freopen("../stdout.txt","w",stdout); #endif  int ncases,n,m,time,temp;scanf("%d",&ncases);while(ncases--){scanf("%d%d%d",&n,&m,&time);int cnt=0;for(int i=1;i<=n;++i)for(int j=1;j<=m;++j){scanf("%d",&temp);if(temp){node[cnt].x=i;node[cnt].y=j;node[cnt++].p=temp;}}sort(node,node+cnt);int ans=0;temp=0;if(node[0].x*2+1<=time){ans=node[0].p;temp=node[0].x+1;for(int i=1;i<cnt;++i)if(temp+(abs(node[i].x-node[i-1].x)+abs(node[i].y-node[i-1].y)+1) + node[i].x<=time){temp+=(abs(node[i].x-node[i-1].x)+abs(node[i].y-node[i-1].y)+1);ans+=node[i].p;}elsebreak;}printf("%d\n",ans);}  return 0;}

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.