题意:

给你一个矩阵,问你按照象棋马的走法,下一步比上一步的数大,问长度最长的序列是多长,然后输出序列。如果有多个最长序列输出字典序最小的那个。类似滑雪,找出最长路径,多个答案 输出字典序最小的。

思路:

将矩阵上的数字从大到小排序,贪心找路径。。

题目:

The cows have revised their game of leapcow. They now play in the middle of a huge pasture upon which they have marked a grid that bears a remarkable resemblance to a chessboard of N rows and N columns (3 <= N <= 365).

Here's how they set up the board for the new leapcow game:

* First, the cows obtain N x N squares of paper. They write the integers from 1 through N x N, one number on each piece of paper.

* Second, the 'number cow' places the papers on the N x N squares in an order of her choosing.

Each of the remaining cows then tries to maximize her score in the game.

* First, she chooses a starting square and notes its number.

* Then, she makes a 'knight' move (like the knight on a chess board) to a square with a higher number. If she's particularly strong, she leaps to the that square; otherwise she walks.

* She continues to make 'knight' moves to higher numbered squares until no more moves are possible.

Each square visited by the 'knight' earns the competitor a single point. The cow with the most points wins the game.

Help the cows figure out the best possible way to play the game.

Input

* Line 1: A single integer: the size of the board

* Lines 2.. ...: These lines contain space-separated integers that tell the contents of the chessboard. The first set of lines (starting at the second line of the input file) represents the first row on the chessboard; the next set of lines represents the next row, and so on. To keep the input lines of reasonable length, when N > 15, a row is broken into successive lines of 15 numbers and a potentially shorter line to finish up a row. Each new row begins on its own line.

Output

* Line 1: A single integer that is the winning cow's score; call it W.

* Lines 2..W+1: Output, one per line, the integers that are the starting square, the next square the winning cow visits, and so on through the last square. If a winning cow can choose more than one path, show the path that would be the 'smallest' if the paths were sorted by comparing their respective 'square numbers'.

Sample Input

4
1 3 2 16
4 10 6 7
8 11 5 12
9 13 14 15

Sample Output

7
2
4
5
9
10
12
13
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define inf 0x3f3f3f3f
const int M=400;
int pre[M][M],dp[M][M],w[M][M],m;/*w在该点走到不能再走,走了多少步,pre记录路径*/
int e[8][2]={2,1,1,2,-1,-2,-2,-1,1,-2,-1,2,2,-1,-2,1};/*记忆化+暴力+care字典序*/
struct node
{int x,y,z;bool operator<(const node &tt)const//sort默认为从大到小排序,优先队列默认为从小到大。{return tt.z<z;}
}s[M*M];/*care*/
int main()
{while(~scanf("%d",&m)){int k=0;for(int i=0;i<m;i++)for(int j=0;j<m;j++){scanf("%d",&dp[i][j]);s[k].x=i;s[k].y=j;s[k++].z=dp[i][j];}sort(s,s+k);/*由大到小找,便于输出记录的路径,最后记录的是起始点的坐标,需要注意字典序*/int ans=-1,a,b,cc=inf;for(int i=0;i<k;i++)/*遍历每一个点,作为起始点,因为该点以前点都是比该点小大的,只能往前走*/{int ma=-1,ant,mi;for(int j=0;j<8;j++){int u=s[i].x+e[j][0],v=s[i].y+e[j][1];if(u<0||v<0||u>=m||v>=m)continue;if(ma<w[u][v]||(ma==w[u][v]&&ant>dp[u][v]))//一方面是控制字典序,另一方面走一步越小,可能走的步数越多{ma=w[u][v];ant=dp[u][v];mi=j;}}w[s[i].x][s[i].y]=ma+1;pre[s[i].x][s[i].y]=mi;if(ans<ma+1||(ans==ma+1&&cc>dp[s[i].x][s[i].y]))/*控制字典序*/{cc=dp[s[i].x][s[i].y];ans=ma+1;a=s[i].x;b=s[i].y;}}printf("%d\n",ans);for(int i=1;i<=ans;i++){printf("%d\n",dp[a][b]);int kk=pre[a][b];a+=e[kk][0];b+=e[kk][1];}}return 0;
}

Millenium Leapcow POJ - 2111 (千禧年跳牛)(贪心找最长路径,记忆化)相关推荐

  1. POJ - 2111 Millenium Leapcow(从最大出发到最小,再反向记录路径)

    题目链接:https://cn.vjudge.net/contest/321389#problem/F 翻译: 任意寻一个点,按照国际象棋的走法,同时下一个点的值必须大于前一个点的值,找到一条最长路径 ...

  2. (区间dp 或 记忆化搜素 )Brackets -- POJ -- 2955

    http://poj.org/problem?id=2955 Description We give the following inductive definition of a "reg ...

  3. 牛客假日团队赛5 F 随机数 BZOJ 1662: [Usaco2006 Nov]Round Numbers 圆环数 (dfs记忆化搜索的数位DP)...

    链接:https://ac.nowcoder.com/acm/contest/984/F 来源:牛客网 随机数 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言6 ...

  4. subsequence 1(牛客多校第五场记忆化搜索+组合数学)

    链接:https://ac.nowcoder.com/acm/contest/885/G 来源:牛客网 题目描述 You are given two strings s and t composed ...

  5. 牛客题霸 [ 求路径] C++题解/答案

    牛客题霸 [ 求路径] C++题解/答案 题目描述 一个机器人在m×n大小的地图的左上角(起点,下图中的标记"start"的位置). 机器人每次向下或向右移动.机器人要到达地图的右 ...

  6. 【POJ - 1661】Help Jimmy(记忆化搜索,dp)

    题干: Help Jimmy" 是在下图所示的场景上完成的游戏. 场景中包括多个长度和高度各不相同的平台.地面是最低的平台,高度为零,长度无限. Jimmy老鼠在时刻0从高于所有平台的某处开 ...

  7. 【POJ - 2376】Cleaning Shifts (贪心)

    题干: Farmer John is assigning some of his N (1 <= N <= 25,000) cows to do some cleaning chores ...

  8. Bailian2753 菲波那契数列(POJ NOI0202-1755)【数列+记忆化递归】

    问题链接:POJ NOI0202-1755 菲波那契数列. 菲波那契数列 总时间限制: 1000ms 内存限制: 65536kB 描述 菲波那契数列是指这样的数列: 数列的第一个和第二个数都为1,接下 ...

  9. 【牛客练习赛13】 A B C D【康拓展开】 E【DP or 记忆化搜索】 F 【思维】

    A 幸运数字Ⅰ 链接:https://www.nowcoder.com/acm/contest/70/A 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K, ...

最新文章

  1. div布局的几点体会
  2. php判断字符串是否为IP,php 判断IP为有效IP地址的方法
  3. java多线程11.非阻塞同步机制
  4. 51nod1600-Simple KMP【SAM,树链剖分】
  5. Druid-目前最好的连接池
  6. 最小的JAVA WEB SERVER源程序 可正常運行
  7. 读取图片测试_精品:固态硬盘进阶知识:寿命篇:(一)22TB地狱级写入测试...
  8. 如何主持计算机教师座谈会,计算机学院召开期中教学评价教师座谈会和学生座谈会...
  9. ie8以ie7方式解析
  10. +0.5(加0.5)配合int()实现四舍五入
  11. java正则表达式详解
  12. PLC属于电子计算机吗,plc是什么
  13. 自己动手写操作系统学习笔记(一)---MBR,PBR,BPB,文件系统
  14. Hash散列算法详细解析(六)
  15. HDFS与HBASE的动态节点的扩容(增删)小白级(一)
  16. 手持云台 1.前期准备
  17. 战舰世界闪击战游戏攻略
  18. 迭代器的定义与自定义一个迭代器
  19. 3D折纸效果怎么实现?
  20. python爬取分析深圳二手房房价

热门文章

  1. 如何在IE浏览器里面定位到关键字的位置(页面代码)和这个关键字位置模块的请求
  2. C和指针之字符串之strlen、strcpy、 strcat、strcmp使用总结
  3. Android之HandlerThread源码分析和简单使用(主线程和子线程通信、子线程和子线程通信)
  4. Android之MVP 模式:简单易懂的介绍方式
  5. Android之访问网络,使用HttpURLConnection还是HttpClient?
  6. k8s 手动恢复redis 集群_二进制手动部署k8s-1.14高可用集群(二、集群部署)
  7. 第7章 C控制语句:分支和跳转
  8. 你的输入法都暴露了些啥?
  9. 批作业是小学老师的一大乐趣 | 今日最佳
  10. 高斯、柯西、拉格朗日都还在的话,他们应该最喜欢这个公众号