P2196 挖地雷

原题链接

题外话

自己一开始一看样例,觉得是数字三角形问题,然后果断上去对线,然后发现自己不会写最长路径的每个点的编号,于是白给。。。

题意

让你找出可以挖出来地雷数最多的一条路径

思路

网上有记忆化搜索,也有DP
我这种是参考了这位仁兄的代码,写出来的,~其实就是稍微修改了格式~
这位仁兄的思路也是DP ,只不过是最长不下降子序列的思路,(真是思路清奇)

代码

///*
//正在播放《フリージア》
//1:21  ━━━━━━●─────   5:35
//   ?   ?   ??   ?   ?
//```````'`...```````''`````````````'````````````````'`.`''
//```````''..`';;'```''```'''''''''''''`````````````````'':
//.````''''':;;!!:````'````'''''''''''``````````````````'':
//``''''''':;;;;;'```'``````''```````````____________```'':
//`````````:;;!;'```````````'```````'```|   所以说   |'``'':
//```````'|$&$%:````````````'```````````|不要停下来啊|''''':
//````'''!$&&&|'```````````'''::''::''''/ (指AC)  |':'':::
//````'':|&&&$!'`````'''''''::.....`;!;'/_________|''``'::
//  ....'|&&@$!'........```:!;'....`:;:```````````````````'
//..````;$&&&$!:''``````'':|%%!::;|%$$!::::::::''::::::::::
//``````!&&@&&|:'````````':|$$$$$$$$$|:':::::::::::::::::::
//`````:%&@@@@@@@@&&&@@@@&&&&@@@@@@@&&&|::::::::':::::::::;
//`````.```':|$@@@@@@@@@@@@@@@@@@@@@@@@###@@&&$|;:::'::::::
//````````````';|$&@@@@@@@@@###@@@@@@########@@@@$!''''::::
//`````````..````:|%$@@@@@#########@#########@@@@&!''''::::
//`````````````````:|&########################@@@$;::::::::
//``````````````````:!$@########################@%;:::'::::
//``````````..``````':|&#######################@@&!''''''::
//''''::'''`.`''''''':|@#######################@@&|:'`.`';!
//:::::::::``'''''';%@######################@@##@@&!::'';;;
//::;::::::`.''''';%@@@@####################$%@##@@%;:'':;!
//:;;;;::::``':;%@@@#########################&%&##@@|:'';;!
//;;!;;;;;;'`::;%@#############################@@##@$!'';!!
//;;;;;;;;:``':::::;|$@############################@$!'`;!!
//::;;;;;;:'`'::::::;!$@#######################&&@$$$;``:;;
//`````````..````````'|@#####################$;!$$$&@@|''':
//'''''''''''''':'''''|@#########@&@##########@@####@@&%|!!
//''''''''':'''::'':''!&########&!|&@##########&&####&%|!||
//:::::'''::::::::::::!&########|:;|$@#########@&###&%||||!
//:::::::'''''':::::::!&#######@!:;!!$@########@$&##@%||||!
//
//                    だからよ...止まるじゃねえぞ
// */




#include <vector>
#include <algorithm>
#include <string>
#include<cstring>
#include <iostream>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <unordered_map>
#include <bitset>
#include <cassert>
#include <chrono>
#include <random>
#include <iomanip>
#include <unordered_set>
#include <ctime>
#include <chrono>
using namespace std;
// #define  ll long long
const int N =1e6+10;
#define PII pair<int , int > 
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define pb push_back
#define sz(x) (int)(x).size()
typedef long long ll;
typedef long double ld;
mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
ll n , m ,t ;

#define __i __int128
//ll mod = 1e9+7;
int f[1100][1100]={0};
int s[1100][1100];
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
vector<int >v;
int dp(int x,int cnt )
{
	
}


int main(){
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
	cin >>n;
	for(int i=1 ;i<=n;i++){
		cin >>s[i][1];
		s[i][2]=s[i][1];
		s[i][3]=0;
	}
	for(int i=1;i<n;i++){
		for(int j=i+1;j<=n;j++){
			cin>>m;
			if(m)f[i][j]=1;
		}
	}
	for(int i=n-1;i>=1;i--){
		int l=0 ,r =0 ;
		for(int j= i+1;j<=n;j++)
			if(f[i][j] && s[j][2]>r){
				r = s[j][2];
				l = j;
			}
		if(r !=0){
			s[i][2] =s[l][2]+s[i][1];
			s[i][3] = l;
		}
	}
	int ans =0 ;int fir =0 ;
	for(int i=1;i<=n;i++){
		if(ans < s[i][2]){
			ans = s[i][2];
			fir = i;
		}
	}
	while(fir>0){
		cout<<fir<<" ";
		fir = s[fir][3];
	}cout<<endl;
	cout<<ans<<endl;
    return 0;

}

原文地址:https://www.cnblogs.com/gaohaoy/p/12527526.html