File tree Expand file tree Collapse file tree 3 files changed +135
-0
lines changed
Expand file tree Collapse file tree 3 files changed +135
-0
lines changed Original file line number Diff line number Diff line change @@ -333,6 +333,62 @@ Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激。可是为
333333
33433425
335335
336+ ``` cpp
337+ #include < stdio.h>
338+ #include < iostream>
339+ #include < algorithm>
340+ #include < cstring>
341+ using namespace std ;
342+
343+ #define Max 100
344+ int map[ Max] [ Max ] , length[ Max] [ Max ] ;
345+ int n=0, m=0;
346+ int max_length=0;
347+ int search(int a, int b);
348+
349+ int main ()
350+ {
351+ while(cin>>n>>m)
352+ {
353+ max_length = 0;
354+ for(int i=0;i<n;i++)
355+ for(int j=0; j<m; j++)
356+ cin>>map[i][j];
357+ memset (length, 0, sizeof(length));
358+ for(int i=0; i<n; i++)
359+ for(int j=0; j<m; j++)
360+ search(i,j);
361+ cout<<max_length<<endl;
362+ }
363+ return 0;
364+ }
365+
366+ int search (int a, int b)
367+ {
368+ if(length[ a] [ b ] !=0)
369+ return length[ a] [ b ] ;
370+ int max=0;
371+ if(a-1>=0 && map[ a] [ b ] > map[ a-1] [ b ] )
372+ {
373+ if(max<search(a-1,b))
374+ max=search(a-1,b);
375+ }
376+ if(b-1>=0 && map[ a] [ b-1 ] < map[ a] [ b ] )
377+ if(max < search(a,b-1))
378+ max = search(a, b-1);
379+ if(b+1<m && map[ a] [ b+1 ] < map[ a] [ b ] )
380+ if(max < search(a, b+1))
381+ max = search(a, b+1);
382+ if(a+1<n && map[ a] [ b ] > map[ a+1] [ b ] )
383+ if(max < search(a+1,b))
384+ max = search(a+1,b);
385+ length[ a] [ b ] = max + 1;
386+ if(max_length<length[ a] [ b ] )
387+ max_length = length[ a] [ b ] ;
388+ return length[ a] [ b ] ;
389+ }
390+ ```
391+
336392
337393
338394
Original file line number Diff line number Diff line change 1+ #include < iostream>
2+ #include < math.h>
3+ using namespace std ;
4+ #define LL long long
5+ #define MAX 310
6+
7+ int T, N;
8+ int MIN;
9+ int flag;
10+ int number[10 ], son[10 ], ans[10 ], ans_num;
11+
12+ void Dfs (int bit, int sum, int num)
13+ { // bit表示当前还剩余的位数,sum当前的累积和,num是当前的深度
14+ if (sum > T)
15+ return ; // 无法裁剪,直接返回
16+ if (bit == 0 )
17+ { // bit为0,说明裁剪完成,开始判定是否是最优解
18+ if (T - sum == MIN)
19+ {
20+ flag = -1 ;
21+ return ; // 有多个最优解,打印异常
22+ }
23+ if (T - sum > MIN)
24+ return ; // 不是最优解,剪枝返回
25+ for (int i = 0 ; i < num; i++)
26+ ans[i] = son[i]; // 相对最优解的存储,将裁剪的每一段纸的数据存储在相对优解中
27+ MIN = T - sum; // 更新最优解
28+ ans_num = num; // 更新最游记的深度, 即裁剪的纸的份数
29+ flag = 1 ; // flag = 1 表示存在一个解
30+ return ; // 结束这个搜索
31+ }
32+ int temp = 0 ;
33+ for (; bit > 0 ; bit--)
34+ { // 搜索核心,给出剩余纸张的位数,模拟剪裁
35+ temp = temp * 10 + number[bit]; // 用temp来模拟所有可能的裁剪方式
36+ son[num] = temp; // son数组记录了当前裁剪方法中的每一段纸的值
37+ Dfs (bit - 1 , sum + temp, num + 1 ); // 对剩余的知进行下一次搜索,纸张份数加1
38+ }
39+ }
40+ int main ()
41+ {
42+ while (cin >> T >> N)
43+ {
44+ if (T == 0 && N == 0 )
45+ break ;
46+ if (T == N)
47+ {
48+ cout << T << " " << N << endl;
49+ continue ;
50+ }
51+ int num = 0 ;
52+ MIN = 1 << 30 ;
53+ flag = 0 ;
54+ while (N)
55+ {
56+ number[++num] = N % 10 ;
57+ N /= 10 ;
58+ }
59+ Dfs (num, 0 , 0 );
60+ if (flag == 0 )
61+ {
62+ cout << " error" << endl;
63+ }
64+ else
65+ {
66+ if (flag == -1 )
67+ cout << " rejected" << endl;
68+ else
69+ {
70+ cout << T - MIN << " " ;
71+ for (int i = 0 ; i < ans_num - 1 ; i++)
72+ cout << ans[i] << " " ;
73+ cout << ans[ans_num - 1 ] << endl;
74+ }
75+ }
76+ }
77+ return 0 ;
78+ }
79+ // references: https://blog.csdn.net/iwts_24/article/details/79728793
You can’t perform that action at this time.
0 commit comments