博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj 2111 Millenium Leapcow(记忆化搜索)
阅读量:5296 次
发布时间:2019-06-14

本文共 3350 字,大约阅读时间需要 11 分钟。

Description

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

41 3 2 164 10 6 78 11 5 129 13 14 15

Sample Output

72459101213 题意:给你一个矩阵,问你按照象棋马的走法,下一步比上一步的数大,问长度最长的序列是多长,然后输出序列。如果有多个最长序列输出字典序最小的那个。 这是看到的一个代码:
1 #include 
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 using namespace std; 9 typedef pair
P;10 const double eps=1e-9;11 const int maxn=200100;12 const int mod=1e9+7;13 const int INF=1e9;14 int M[370][370],dp[370][370];15 int N;16 int dx[8]= { 1,1,2,2,-1,-1,-2,-2};17 int dy[8]= { 2,-2,1,-1,2,-2,1,-1};18 P path[370][370];19 vector

res;20 int DP(int x,int y)21 {22 int &m=dp[x][y];23 if(m) return m;24 m=1;25 for(int i=0; i<8; i++)26 {27 int nx=x+dx[i];28 int ny=y+dy[i];29 if(1<=nx&&nx<=N&&1<=ny&&ny<=N&&M[nx][ny]>M[x][y])30 {31 if(m

M[nx][ny])39 {40 path[x][y]=make_pair(nx,ny);41 }42 }43 }44 }45 return m;46 }47 int main()48 {49 while(~scanf("%d",&N))50 {51 for(int i=1; i<=N; i++)52 {53 for(int j=1; j<=N; j++)54 {55 scanf("%d",&M[i][j]);56 }57 }58 int MAX=1,cnt;59 for(int i=1; i<=N; i++)60 {61 for(int j=1; j<=N; j++)62 {63 cnt=DP(i,j);64 if(MAX

View Code

知识点:

这个代码的主要想法是,如同最短路一样,path中储存的是当前节点的下一步应该走的位置,然后进行搜索直到遍历了所有的点。

 

转载于:https://www.cnblogs.com/wang-ya-wei/p/6894790.html

你可能感兴趣的文章
程序员的“机械同感”
查看>>
在16aspx.com上下了一个简单商品房销售系统源码,怎么修改它的默认登录名和密码...
查看>>
c++回调函数
查看>>
linux下Rtree的安装
查看>>
【Java】 剑指offer(53-2) 0到n-1中缺失的数字
查看>>
Delphi中ListView类的用法
查看>>
Python Web框架Django (零)
查看>>
多米诺骨牌
查看>>
Linq 学习(1) Group & Join--网摘
查看>>
asp.net 调用前台JS调用后台,后台掉前台JS
查看>>
Attribute(特性)与AOP
查看>>
第三次作业
查看>>
苹果手表:大方向和谷歌一样,硬件分道扬镳
查看>>
Competing Consumers Pattern (竞争消费者模式)
查看>>
HDUOJ ------1398
查看>>
cf--------(div1)1A. Theatre Square
查看>>
Android面试收集录15 Android Bitmap压缩策略
查看>>
PHP魔术方法之__call与__callStatic方法
查看>>
ubuntu 安装后的配置
查看>>
VSCODE更改文件时,提示:EACCES: permission denied的解决办法(mac电脑系统)
查看>>