codeforces 1065D
题目链接:https://codeforces.com/problemset/problem/1065/D
题意:给你一个又1~n^2组成的n行n列的矩阵,你可以走日字型,直线,斜线,现在要求你从1走到n^2的位置,必须经过1,2,3,4and so on,请问你最短的路径长度和需要变换的方式
题解:非常明显的一个搜索,看了题解后我才明白要用dp存状态。。。。。
这题可以说是一个比较好的把搜索和dp结合起来的题了,首先我们设置dp的状态为,当棋子处于坐标(i,j)时他的当前角色为z,他换了t种角色,并且已经走过前k个点
然后就bfs搜索各种状态即可,首先把三种情况的状态存一下,然后,搜索当前走日字形的情况,搜索当前走直线的情况,搜索当前走斜线的情况,最后遍历所有状态中,走到终点时所有的情况即可
代码如下:
#include <map>
#include <set>
#include <cmath>
#include <ctime>
#include <stack>
#include <queue>
#include <cstdio>
#include <cctype>
#include <bitset>
#include <string>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <functional>
#define PI acos(-1)
#define eps 1e-8
#define fuck(x) cout<<#x<<" = "<<x<<endl;
#define FIN freopen("input.txt","r",stdin);
#define FOUT freopen("output.txt","w+",stdout);
//#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int maxn = 1e5 + ;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + ;
LL gcd(LL a, LL b) {
return b ? gcd(b, a % b) : a;
}
LL lcm(LL a, LL b) {
return a / gcd(a, b) * b;
}
LL powmod(LL a, LL b, LL MOD) {
LL ans = ;
while(b) {
if(b % )ans = ans * a % MOD;
a = a * a % MOD;
b /= ;
}
return ans;
}
double dpow(double a, LL b) {
double ans = 1.0;
while(b) {
if(b % )ans = ans * a;
a = a * a;
b /= ;
}
return ans;
} struct node {
int x, y, z, t, k;
//x,y 为坐标
//z为当前方式
//t为换了多少次
//k为经过了前k个点
node(int _x, int _y, int _z, int _t, int _k) {
x = _x, y = _y, z = _z, t = _t, k = _k;
}
};
int n;
int g[][];
int dp[][][][][];
////0表示马,1表示车,2表示斜线
//int dx1[8][2]={{-2,-1},{-2,1},{2,1},{2,-1},{-1,-2},{-1,2},{1,-2},{1,2}};
//int dx2[4][2]={{-1,0},{1,0},{0,1},{0,-1}};
//int dx3[4][2]={{-1,-1},{-1,1},{1,1},{1,-1}}; int dx1[][] = {{-, -}, {-, }, {, -}, {, }, {-, -}, {-, }, {, -}, {, }}; //日
int dx2[][] = {{-, }, {, }, {, }, {, -}}; //直线
int dx3[][] = {{-, -}, {-, }, {, -}, {, }}; //斜线
int sx, sy;
int ex, ey;
void bfs(int sx, int sy) {
memset(dp, -, sizeof(dp));
dp[sx][sy][][][] = dp[sx][sy][][][] = dp[sx][sy][][][] = ;
queue<node>q;
q.push(node(sx, sy, , , ));
q.push(node(sx, sy, , , ));
q.push(node(sx, sy, , , ));
while(!q.empty()) {
node nd = q.front();
q.pop();
int x = nd.x, y = nd.y, z = nd.z, t = nd.t, k = nd.k; for(int i = ; i < ; i++) {
if(i == z) continue;
if(dp[x][y][i][t + ][k] != -) continue;
dp[x][y][i][t + ][k] = dp[x][y][z][t][k] + ;
q.push(node(x, y, i, t + , k));
}
// cout<<x<<endl;
if(z == ) {
for(int i = ; i < ; i++) {
int nx = x + dx1[i][];
int ny = y + dx1[i][];
int nk = k;
if(nx < || nx > n || ny < || ny > n) continue;
if(g[nx][ny] == k + ) nk++;
if(dp[nx][ny][z][t][nk] != -) continue;
dp[nx][ny][z][t][nk] = dp[x][y][z][t][k] + ;
q.push(node(nx, ny, z, t, nk));
}
} if(z == ) {
for(int j = ; j <= ; j++) {
for(int i = ; i < ; i++) {
int nx = x + j * dx2[i][];
int ny = y + j * dx2[i][];
int nk = k;
if(nx < || nx > n || ny < || ny > n) continue;
if(g[nx][ny] == k + ) nk++;
if(dp[nx][ny][z][t][nk] != -) continue;
dp[nx][ny][z][t][nk] = dp[x][y][z][t][k] + ;
q.push(node(nx, ny, z, t, nk));
}
}
}
if(z == ) {
for(int j = ; j <= ; j++) {
for(int i = ; i < ; i++) {
int nx = x + j * dx3[i][];
int ny = y + j * dx3[i][];
int nk = k;
if(nx < || nx > n || ny < || ny > n) continue;
if(g[nx][ny] == k + ) nk++;
if(dp[nx][ny][z][t][nk] != -) continue;
dp[nx][ny][z][t][nk] = dp[x][y][z][t][k] + ;
//q.push(node(nx, ny, z,, y, nk));
q.push(node(nx, ny, z, t, nk));
}
}
}
}
} int main() {
#ifndef ONLINE_JUDGE
FIN
#endif
cin >> n;
for(int i = ; i <= n; i++) {
for(int j = ; j <= n; j++) {
cin >> g[i][j];
if(g[i][j] == ) {
sx = i;
sy = j;
}
if(g[i][j] == n * n) {
ex = i;
ey = j;
}
}
}
bfs(sx, sy);
int ans = INF;
for(int z = ; z < ; z++) {
for(int t = ; t < ; t++) {
if(dp[ex][ey][z][t][n * n] != -) {
ans = min(dp[ex][ey][z][t][n * n], ans);
}
}
}
int flag = ;
for(int t = ; t < ; t++) {
for(int z = ; z < ; z++) {
if(dp[ex][ey][z][t][n * n] == ans && !flag) {
cout << ans << " " << t << endl;
flag = ;
}
}
}
}
codeforces 1065D的更多相关文章
- Three Pieces CodeForces - 1065D (BFS)
链接 大意: n*n棋盘, 每个格子写有数字, 各不相同, 范围[1,n*n], 初始在数字1的位置, 可以操纵knight,bishop,rook三种棋子, 每走一步花费1, 交换棋子花费1, 问按 ...
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
- 【Codeforces 738A】Interview with Oleg
http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...
- CodeForces - 662A Gambling Nim
http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...
- CodeForces - 274B Zero Tree
http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...
- CodeForces - 261B Maxim and Restaurant
http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...
- CodeForces - 696B Puzzles
http://codeforces.com/problemset/problem/696/B 题目大意: 这是一颗有n个点的树,你从根开始游走,每当你第一次到达一个点时,把这个点的权记为(你已经到过不 ...
随机推荐
- 什么是高防服务器?如何搭建DDOS流量攻击防护系统
关于高防服务器的使用以及需求,从以往的联众棋牌到目前发展迅猛的手机APP棋牌,越来越多的游戏行业都在使用高防服务器系统,从2018年1月到11月,国内棋牌运营公司发展到了几百家. 棋牌的玩法模式从之前 ...
- 网站漏洞修复之最新版本UEditor漏洞
UEditor于近日被曝出高危漏洞,包括目前官方UEditor 1.4.3.3 最新版本,都受到此漏洞的影响,ueditor是百度官方技术团队开发的一套前端编辑器,可以上传图片,写文字,支持自定义的h ...
- 第三章 最简单的C程序设计——顺序程序设计
一.数据的表现形式及其运算 1.常量和变量 在计算机高级语言中,数据有两种表现形式:常量和变量. 1.1.常量 在程序运行过程中,其值不能被改变的量称为常量.如:5,6,32,0.111. 数值常量就 ...
- 2-Linux C语言指针与内存-学习笔记
Linux C语言指针与内存 前面我们对于: c语言的基本用法 makeFile文件的使用 main函数的详解 标准输入输出流以及错误流管道 工具与原理 指针与内存都是c语言中的要点与难点 指针 数组 ...
- ubuntu配置机器学习环境(一) ubuntu安装
第一部分:Ubuntu14.04安装 Step :安装Ubuntu Step 1.1:准备安装U盘 首先到官网下载Ubuntu的镜像,我使用的是Ubuntu 14.04.3的ISO. 然后使用Ultr ...
- TRANSLATE(转换大/小写并替换字符)
可以将字母 转换大/小 写或使用替 换规则. 要转换大/小 写,请使用 TRANSLATE 语句,用法 如下: 语法 TRANSLATE <c> TO UPPER CASE. TRANSL ...
- JS 实现AJAX封装(只限于异步)
1.AJAX 分为异步 和 同步 请求 比如你去买一个食品,但是商店暂时没有这个食品 异步:等到商品有了再来买,这个期间我可以去做别的事: 同步:一直在这里等,什么时候商品来了,买到手了,再去做别的事 ...
- ArrayMap java.lang.ArrayIndexOutOfBoundsException
错误堆栈: java.lang.ArrayIndexOutOfBoundsException: length=0; index=1 at android.support.v4.util.SimpleA ...
- stm8编程tips(stvd)
编译完成时显示程序占用的flash和ram大小 将附件压缩包中的mapinfo.exe解压到stvd的安装路径\stvd中 在工程上点右键选settings 右侧的选项卡选择Linker,将categ ...
- Linux的系统安全设置Shell脚本
#!/bin/sh # desc: setup linux system security # powered by www.lvtao.net #account setup passwd -l xf ...