Three Pieces CodeForces - 1065D (BFS)
大意: 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)的更多相关文章
- codeforces 1065D
题目链接:https://codeforces.com/problemset/problem/1065/D 题意:给你一个又1~n^2组成的n行n列的矩阵,你可以走日字型,直线,斜线,现在要求你从1走 ...
- Arthur and Walls CodeForces - 525D (bfs)
大意: 给定格点图, 每个'.'的连通块会扩散为矩形, 求最后图案. 一开始想得是直接并查集合并然后差分, 但实际上是不对的, 这个数据就可以hack掉. 3 3 **. .** ... 正解是bfs ...
- Police Stations CodeForces - 796D (bfs)
大意: 给定树, 有k个黑点, 初始满足条件:所有点到最近黑点距离不超过d, 求最多删除多少条边后, 使得原图仍满足条件. 所有黑点开始bfs, 贪心删边. #include <iostream ...
- Kilani and the Game CodeForces - 1105D (bfs)
沙茶bfs打了2小时... queue入队量太大了, 放函数里直接T了, 改成全局46ms #include <iostream> #include <algorithm> # ...
- Fair CodeForces - 987D (bfs)
链接 大意:给定无向图边权均为1, 每个节点有一种货物, 对于每个节点, 求出拿到$s$种不同货物的最短距离 (每种货物独立计算,并且不用返回) 因为$s$较小, 直接枚举每种货物即可 所以问题就转化 ...
- Connected Components? CodeForces - 920E (bfs)
大意:给定无向图, 求补图的连通块数 bfs模拟即可, 这里用了map存图, set维护未划分的点集, 复杂度$O(nlog^2n)$, 用链表的话可以$O(n)$ #include <iost ...
- codeforces 60B bfs
题意:给出一个六面体分为k层,每层n行m列,每个小立方体有'.'(空)与'#'(障碍)的状态,第一层某个空的位置有一个水龙头,水流每次往六个方向流动(...).最少时间水流能把立方体空的部分填满. 思 ...
- 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$张卡, 使用后达到坐 ...
- Codeforces 1105D (BFS)
题面 传送门 分析 考虑BFS while(棋盘没有满){ for 玩家 p{ 对p进行BFS,走s[p]步 } } 对于每个玩家p BFS的时候如果到了格子(x,y),就把\(vis[x][y]\) ...
随机推荐
- linux常用命令:find 命令参数详解
find一些常用参数的一些常用实例和一些具体用法和注意事项. 1.使用name选项: 文件名选项是find命令最常用的选项,要么单独使用该选项,要么和其他选项一起使用. 可以使用某种文件名模式来匹配 ...
- 查找nginx安装的路径以及相关安装操作命令
查找nginx安装的路径以及相关安装操作命令 Linux环境下,怎么确定Nginx是以那个config文件启动的? [root@localhost ~]# ps -ef | grep nginxroo ...
- 处理内容有&特殊字符thinkphp返回xml无法解析的问题<![CDATA[xxx]]>
处理内容有&特殊字符thinkphp返回xml无法解析的问题<![CDATA[xxx]]> // xml 转义特殊字符 如&'" <![CDATA[&quo ...
- iOS原生的AVFoundation扫描二维码/条形码
#import <AVFoundation/AVFoundation.h> @interface ViewController ()<AVCaptureMetadataOutputO ...
- SQL学习之Can't connect to MySQL server on localhost (10061)
最近升级 了系统,开机后连接MySQL报错,Can't connect to MySQL server on localhost (10061): 估计是升级系统清除了以前的缓存设置,网上很多方法是命 ...
- 20145227鄢曼君《网络对抗》MSF基础应用
20145227鄢曼君<网络对抗>MSF基础应用 主动攻击:ms08_067漏洞攻击实践 两台虚拟机,其中一台为kali,一台为windows xp sp3(英文版).在VMware中设置 ...
- bzoj 2118 墨墨的等式 - 图论最短路建模
墨墨突然对等式很感兴趣,他正在研究a1x1+a2y2+…+anxn=B存在非负整数解的条件,他要求你编写一个程序,给定N.{an}.以及B的取值范围,求出有多少B可以使等式存在非负整数解. Input ...
- CF 316E3 Summer Homework(斐波那契矩阵+线段树)
题目链接:http://codeforces.com/problemset/problem/316/E3 题意:一个数列A三种操作:(1)1 x y将x位置的数字修改为y:(2)2 x y求[x,y] ...
- python_实现发送邮件功能
#!/usr/bin/env python #-*- coding:utf-8 -*- from email import encoders from email.header import Head ...
- SpringBoot添加自定义消息转换器
首先我们需要明白一个概念:springboot中很多配置都是使用了条件注解进行判断一个配置或者引入的类是否在容器中存在,如果存在会如何,如果不存在会如何. 也就是说,有些配置会在springboot中 ...