| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
哈哈,记得处理起点,忘了中间啦,haha~~~ 附AC代码嗯,先Sort排序所有点的高度,从大到小,然后强制从前面的点来转移。
为了解决字典序最小的问题,需要注意两个地方:
1.Ans存最后的起点,那么我们从前往后扫描时,只要有>=当前解的解,就应当更新答案。这样就保证了最优解的前提下,起点最小。
2.中间的DP值在转移时,也要注意一下当新的解==当前解的情况。由于我是按照移动列表(move[]数组)转移,所以只能根据其指向的块的高度判断一下是否是更小的字典序。
然后,感受一下39行的代码吧!
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int N = 366;
const int INF = 0x3f3f3f3f;
const int mv[8][2] = { {1,2},{-1,2},{1,-2},{-1,-2},{2,1},{-2,1},{2,-1},{-2,-1} };
struct King {
pair<int, int> pos, pre;
int height, dynamic;
friend bool operator < (King a, King b) {
return a.height > b.height;
}
}K[N*N];
int n, map[N][N], tot = 0;
pair<int, int> ans;
signed main(void) {
scanf("%d", &n);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
scanf("%d", &K[++tot].height),
K[tot].dynamic = 1,
K[tot].pos = make_pair(i, j);
sort(K + 1, K + 1 + tot);
for (int i = 1; i <= tot; i++)
map[K[i].pos.first][K[i].pos.second] = i;
for (int i = 1; i <= tot; i++) {
for (int j = 0; j < 8; j++)
if (K[i].pos.first + mv[j][0] > 0 && K[i].pos.first + mv[j][0] <= n && K[i].pos.second + mv[j][1] > 0 && K[i].pos.second + mv[j][1] <= n)
if (map[K[i].pos.first + mv[j][0]][K[i].pos.second + mv[j][1]]<i && (K[map[K[i].pos.first + mv[j][0]][K[i].pos.second + mv[j][1]]].dynamic + 1>K[i].dynamic|| K[map[K[i].pos.first + mv[j][0]][K[i].pos.second + mv[j][1]]].dynamic + 1==K[i].dynamic&&map[K[i].pos.first + mv[j][0]][K[i].pos.second + mv[j][1]]>map[K[i].pre.first][K[i].pre.second]))
K[i].dynamic = K[map[K[i].pos.first + mv[j][0]][K[i].pos.second + mv[j][1]]].dynamic + 1, K[i].pre.first = K[i].pos.first + mv[j][0], K[i].pre.second = K[i].pos.second + mv[j][1];
if (K[i].dynamic >= K[map[ans.first][ans.second]].dynamic)ans = K[i].pos;
}
printf("%d\n", K[map[ans.first][ans.second]].dynamic);
while (ans.first&&ans.second)printf("%d\n", K[map[ans.first][ans.second]].height), ans = K[map[ans.first][ans.second]].pre;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator