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

题意:

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

思路:

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

题目:

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;
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/310015.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

java1.8的stream_JDK1.8新特性(一):stream

搜索热词一.什么是stream&#xff1f;1.概述Java 8 API添加了一个新的抽象称为流Stream&#xff0c;可以让你以一种声明的方式处理数据。这种风格将要处理的元素集合看作一种流&#xff0c; 流在管道中传输&#xff0c; 并且可以在管道的节点上进行处理&#xff0c; 比如筛选&a…

[C++STL]常用集合算法

代码如下: #include <iostream> #include <vector> #include <numeric> #include <algorithm> using namespace std;class myPrint { public:void operator()(int val){cout << val << " ";} };void test01() {vector<int&g…

php给html传值,PHP传值到不同页面的三种常见方式及php和html之间传值问题_PHP

在项目开发中经常见到不同页面之间传值在web工作中&#xff0c;本篇文章给大家列出了三种常见的方式。接触PHP也有几个月了&#xff0c;本文总结一下这段日子中&#xff0c;在编程过程里常用的3种不同页面传值方法&#xff0c;希望可以给大家参考。有什么意见也希望大家一起讨论…

Seek the Name, Seek the Fame POJ - 2752 (理解KMP函数的失配)既是S的前缀又是S的后缀的子串

题意&#xff1a;给一个字符串S&#xff0c; 求出所有前缀pre&#xff0c;使得这个前缀也正好是S的后缀。 输出所有前缀的结束位置。 就是求前缀和后缀相同的那个子串的长度 然后从小到大输出,主要利用next数组求解。 例如 “ababcababababcabab”&#xff0c; 以下这些前缀…

2020 年了,WPF 还有前途吗?

2020年了&#xff0c;微软的技术也不断更新了, .Net Core 3.1、.Net Framework 4.8以及今年11月推出的.NET 5...win10平台也普及了很多&#xff0c;WPF可以在上面大展身手&#xff0c;可性能和内存占用还是不行&#xff0c;但是WPF强大的UI能力很吸引人。WPF已经凉了吗? 学WPF…

[C++STL]常用查找算法

代码如下: #include <iostream> #include <algorithm> #include <vector> #include <string> using namespace std;void test01() {vector<int>v;for (int i 0; i < 10; i){v.push_back(i);}vector<int>::iterator it find(v.begin(…

php asp 语法,ASP 语法

在我们的 ASP 教程中&#xff0c;每个实例都提供隐藏的 ASP 源代码。这样会使您更容易理解它们的工作原理。向浏览器写输出ASP 文件通常包含 HTML 标签&#xff0c;就像 HTML 文件。然而&#xff0c;ASP 文件也能包含服务器脚本&#xff0c;这些脚本被分隔符 包围起来。服务器脚…

#10003. 「一本通 1.1 例 4」加工生产调度(贪心)

加工生产调度 题目描述 某工厂收到了n个产品的订单&#xff0c;这n个产品分别在A、B两个车间加工&#xff0c;并且必须先在A车间加工后才可以到B车间加工。 某个产品i在A、B两车间加工的时间分别为Ai、Bi。询问怎样安排这n个产品的加工顺序&#xff0c;才能使总的加工时间最短…

不要把异常当做业务逻辑,这性能可能你无法承受

一&#xff1a;背景1. 讲故事在项目中摸爬滚打几年&#xff0c;应该或多或少的见过有人把异常当做业务逻辑处理的情况(┬&#xff3f;┬)&#xff0c;比如说判断一个数字是否为整数,就想当然的用try catch包起来&#xff0c;再进行 int.Parse&#xff0c;如果抛异常就说明不是整…

[设计模式]开闭原则

开闭原则: 对扩展开放&#xff0c;对修改关闭。 增加功能是提过增加代码来实现的&#xff0c;而不是去修改源代码。 代码如下: #include <iostream> #include <string> using namespace std;class Caculaor { public:Caculaor(int a,int b,string c):a(a),b(b…

#10010 「一本通 1.1 练习 6」糖果传递 (数学+贪心)

题目描述 原题来自&#xff1a;HAOI 2008 有 n个小朋友坐成一圈&#xff0c;每人有 ai 颗糖果。每人只能给左右两人传递糖果。每人每次传递一颗糖果的代价为 1 。求使所有人获得均等糖果的最小代价。 输入格式 第一行有一个整数 &#xff0c;n表示小朋友个数&#xff1b; …

ASP.NET Core在Docker下面生成简易验证码

背景 验证码这个功能是十分常见的&#xff0c;各大系统的登录页面都会有。今天介绍一下最为普通的验证码。无论最终给到前端的是图片格式的验证码还是base64格式的验证码&#xff0c;其实都离不开这样的一步操作&#xff0c;都要先在后台生成一个图片。就个人经验来说&#xff…

[设计模式]迪米特法则

迪米特法则 又叫最少知识法则 类中的成员属性和成员方法&#xff0c;如果不需要对外暴露&#xff0c;就不要设成public。 代码如下: #include <iostream> #include <string> using namespace std;class AbstractBuilding { public:virtual void sale() 0; };cl…

#10017 「一本通 1.2 练习 4」传送带+三分套三分

题目描述 原题来自&#xff1a;SCOI 2010 在一个 2 维平面上有两条传送带&#xff0c;每一条传送带可以看成是一条线段。两条传送带分别为线段 AB和线段CD 。lxhgww 在 AB上的移动速度为 P &#xff0c;在 CD上的移动速度为 Q &#xff0c;在平面上的移动速度R 。 现在 l…

java世博会,反应原生失去的世博会

我用react-init创建的我的RN项目工作正常&#xff0c;直到我做 npm install 才能让 react-native-elements 与 react-native-vector-icons 一起工作 . 我无法链接&#xff0c;它说我有反应本地而不是react-native-cli . 所以我卸载并反应原生并全局安装cli .现在 npm start 告诉…

程序员过关斩将--从用户输入手机验证码开始

点击“蓝字”关注我们吧菜菜哥&#xff0c;请教个问题呗&#xff1f;说说看&#xff0c;能否解决不敢保证哦最近做的App业务中&#xff0c;有很多敏感操作需要用户输入手机验证码这没问题&#xff0c;手机验证码主要是为了验证当前操作人的有效性&#xff0c;有什么问题呢&…

Ticket Game CodeForces - 1215D(博弈题,巴什博弈思维)

题意&#xff1a;两个人玩游戏&#xff0c;通过轮流填数字&#xff08;0~9&#xff09;&#xff0c;若最终左右两边的和相等&#xff0c;后手赢&#xff0c;否则先手赢。起始有部分数字和空格。 官方题解&#xff1a; 题解翻译&#xff1a; 让我们把余额表示为左半部分数字和…

matlab中图像轮廓变细,Matlab中,用bwmorph函数提取二进制图像的轮廓

Matlab中bwmorph函数在提取二进制图像的轮廓如下&#xff1a;语法: BW2 bwmorph(BW1,operation) &#xff1b;BW2 bwmorph(BW1,operation,n) &#xff1b; n为次数&#xff1b;Operation的参数可以有多种选择&#xff0c;现归纳如下&#xff1a;‘bother’&#xff1a;闭包…

[设计模式]合成复用原则

合成复用原则:继承和组合&#xff0c;优先使用组合。 这样写&#xff0c;每开一种车&#xff0c;就要弄一个新的Person类。 代码如下: #include <iostream> using namespace std;class AbstractCar { public:virtual void run() 0; };class DaZhong :public AbstractC…

帮 vs2019 找回丢失的 SDK

缘起 前一段时间&#xff0c;有网友遇到一个奇怪的问题&#xff0c;说他机器上的 vs2019 编译 C 工程报错。我当时一听就有两个怀疑&#xff1a;工程设置不对。vs2019 没装好。因为新建一个最简单的工程&#xff0c;编译也报一样的错误&#xff0c;所以可以排除工程设置的问题了…