链接

大意: n*n棋盘, 每个格子写有数字, 各不相同, 范围[1,n*n], 初始在数字1的位置, 可以操纵knight,bishop,rook三种棋子, 每走一步花费1, 交换棋子花费1, 问按顺序遍历完[1,n*n]的最少花费和最少花费下的最少交换次数

这题码了1个小时, WA on test 86, 没看出来是哪里错了, 不想改了

设状态(z,x,y)表示到达点(x,y)时棋子为z的最少花费, 每次BFS求出最短路, 再DP两次求出最少花费

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <queue>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define x first
#define y second
using namespace std;
typedef pair<int,int> pii; const int N = 15, INF = 0x3f3f3f3f;
int n;
pii a[N*N];
int f[N*N][3], g[N*N][3], d[N*N][3][3];
const int dx[]={-2,-1,1,2,2,1,-1,-2};
const int dy[]={1,2,2,1,-1,-2,-2,-1};
const int dx1[]={1,-1,1,-1};
const int dy1[]={1,1,-1,-1};
struct _ {int z,x,y;};
int dis[3][N][N], vis[3][N][N]; int chk(int x, int y) {
return 1<=x&&x<=n&&1<=y&&y<=n;
} int bfs(int z1, int x1, int y1, int z2, int x2, int y2) {
queue<_> q;
q.push({z1,x1,y1});
memset(vis,0,sizeof vis);
memset(dis,INF,sizeof dis);
dis[z1][x1][y1] = 0;
while (!q.empty()) {
auto u = q.front();q.pop();
if (vis[u.z][u.x][u.y]) continue;
vis[u.z][u.x][u.y] = 1;
int w = dis[u.z][u.x][u.y]+1;
REP(i,0,7) {
int xx=u.x+dx[i], yy=u.y+dy[i];
if (!chk(xx,yy)) continue;
dis[0][xx][yy] = min(dis[0][xx][yy], w+(u.z!=0));
q.push({0,xx,yy});
}
REP(i,0,3) REP(j,1,n) {
int xx=u.x+j*dx1[i],yy=u.y+j*dy1[i];
if (!chk(xx,yy)) continue;
dis[1][xx][yy] = min(dis[1][xx][yy], w+(u.z!=1));
q.push({1,xx,yy});
}
REP(i,1,n) {
dis[2][i][u.y] = min(dis[2][i][u.y], w+(u.z!=2));
q.push({2,i,u.y});
dis[2][u.x][i] = min(dis[2][u.x][i], w+(u.z!=2));
q.push({2,u.x,i});
}
}
return dis[z2][x2][y2];
} int main() {
scanf("%d", &n);
REP(i,1,n) REP(j,1,n) {
int t;
scanf("%d", &t);
a[t] = {i,j};
}
REP(i,2,n*n) {
REP(j,0,2) f[i][j] = INF;
REP(j,0,2) REP(k,0,2) {
d[i][j][k] = bfs(k,a[i-1].x,a[i-1].y,j,a[i].x,a[i].y);
f[i][j] = min(f[i][j], f[i-1][k]+d[i][j][k]);
}
}
REP(i,2,n*n) {
REP(j,0,2) g[i][j] = INF;
REP(j,0,2) REP(k,0,2) {
if (f[i-1][k]+d[i][j][k]==f[i][j]) {
g[i][j] = min(g[i][j], g[i-1][k]+(j!=k));
}
}
}
int ans1=INF, ans2=INF;
REP(i,0,2) ans1=min(ans1,f[n*n][i]);
REP(i,0,2) if (ans1==f[n*n][i]) ans2=min(ans2,g[n*n][i]);
printf("%d %d\n", ans1, ans2);
}

Three Pieces CodeForces - 1065D (BFS)的更多相关文章

  1. codeforces 1065D

    题目链接:https://codeforces.com/problemset/problem/1065/D 题意:给你一个又1~n^2组成的n行n列的矩阵,你可以走日字型,直线,斜线,现在要求你从1走 ...

  2. Arthur and Walls CodeForces - 525D (bfs)

    大意: 给定格点图, 每个'.'的连通块会扩散为矩形, 求最后图案. 一开始想得是直接并查集合并然后差分, 但实际上是不对的, 这个数据就可以hack掉. 3 3 **. .** ... 正解是bfs ...

  3. Police Stations CodeForces - 796D (bfs)

    大意: 给定树, 有k个黑点, 初始满足条件:所有点到最近黑点距离不超过d, 求最多删除多少条边后, 使得原图仍满足条件. 所有黑点开始bfs, 贪心删边. #include <iostream ...

  4. Kilani and the Game CodeForces - 1105D (bfs)

    沙茶bfs打了2小时... queue入队量太大了, 放函数里直接T了, 改成全局46ms #include <iostream> #include <algorithm> # ...

  5. Fair CodeForces - 987D (bfs)

    链接 大意:给定无向图边权均为1, 每个节点有一种货物, 对于每个节点, 求出拿到$s$种不同货物的最短距离 (每种货物独立计算,并且不用返回) 因为$s$较小, 直接枚举每种货物即可 所以问题就转化 ...

  6. Connected Components? CodeForces - 920E (bfs)

    大意:给定无向图, 求补图的连通块数 bfs模拟即可, 这里用了map存图, set维护未划分的点集, 复杂度$O(nlog^2n)$, 用链表的话可以$O(n)$ #include <iost ...

  7. codeforces 60B bfs

    题意:给出一个六面体分为k层,每层n行m列,每个小立方体有'.'(空)与'#'(障碍)的状态,第一层某个空的位置有一个水龙头,水流每次往六个方向流动(...).最少时间水流能把立方体空的部分填满. 思 ...

  8. Board Game CodeForces - 605D (BFS)

    大意: 给定$n$张卡$(a_i,b_i,c_i,d_i)$, 初始坐标$(0,0)$. 假设当前在$(x,y)$, 若$x\ge a_i,y\ge b_i$, 则可以使用第$i$张卡, 使用后达到坐 ...

  9. Codeforces 1105D (BFS)

    题面 传送门 分析 考虑BFS while(棋盘没有满){ for 玩家 p{ 对p进行BFS,走s[p]步 } } 对于每个玩家p BFS的时候如果到了格子(x,y),就把\(vis[x][y]\) ...

随机推荐

  1. eclipse里error报错Target runtime com.genuitec.runtime.generic.jee60 is not defined.

    eclipse里error报错Target runtime com.genuitec.runtime.generic.jee60 is not defined. eclipse里error报错解决办法 ...

  2. MongoDB ----基于分布式文件存储的数据库

    参考: http://www.cnblogs.com/huangxincheng/category/355399.html http://www.cnblogs.com/daizhj/category ...

  3. sqlserver 判断各种不存在

    判断数据库是否存在 if exists (select * from dbo.sysobjects where name = '数据库名') --drop database [数据库名] 判断表是否存 ...

  4. Python Web学习笔记之并发编程的孤儿进程与僵尸进程

    1.前言 之前在看<unix环境高级编程>第八章进程时候,提到孤儿进程和僵尸进程,一直对这两个概念比较模糊.今天被人问到什么是孤儿进程和僵尸进程,会带来什么问题,怎么解决,我只停留在概念上 ...

  5. Python Web学习笔记之Python多线程基础

    多线程理解 多线程是多个任务同时运行的一种方式.比如一个循环中,每个循环看做一个任务,我们希望第一次循环运行还没结束时,就可以开始第二次循环,用这种方式来节省时间. python中这种同时运行的目的是 ...

  6. Centos 更改系统时间

    .date //查看本地 .hwclock --show //查看硬件的时间 .如果硬件的时间是对不上,那就对硬件的时间进行修改 .hwclock --set --date '2222-22-22 2 ...

  7. mamcached+magent构建memcached集群

    cat /etc/redhat-release CentOS release 6.7 (Final) 防火墙.selinux 关闭 192.168.12.30 安装libevent和memcached ...

  8. Thinkphp5 引入第三方类库的方法

    原文链接:http://www.zhaisui.com/article/42.html

  9. 常用maven命令总结

    常用Maven命令: mvn -v //查看版本 mvn archetype:create //创建 Maven 项目 mvn compile //编译源代码 mvn test-compile //编 ...

  10. linux内核启动参数解析及添加

    1.环境: ubuntu16.04 Linux jello 4.4.0-89-generic #112-Ubuntu SMP Mon Jul 31 19:38:41 UTC 2017 x86_64 x ...