题目链接: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的更多相关文章

  1. Three Pieces CodeForces - 1065D (BFS)

    链接 大意: n*n棋盘, 每个格子写有数字, 各不相同, 范围[1,n*n], 初始在数字1的位置, 可以操纵knight,bishop,rook三种棋子, 每走一步花费1, 交换棋子花费1, 问按 ...

  2. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  3. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  4. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  5. 【Codeforces 738A】Interview with Oleg

    http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...

  6. CodeForces - 662A Gambling Nim

    http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...

  7. CodeForces - 274B Zero Tree

    http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...

  8. CodeForces - 261B Maxim and Restaurant

    http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...

  9. CodeForces - 696B Puzzles

    http://codeforces.com/problemset/problem/696/B 题目大意: 这是一颗有n个点的树,你从根开始游走,每当你第一次到达一个点时,把这个点的权记为(你已经到过不 ...

随机推荐

  1. Leecode刷题之旅-C语言/python-100相同的树

    /* * @lc app=leetcode.cn id=100 lang=c * * [100] 相同的树 * * https://leetcode-cn.com/problems/same-tree ...

  2. python爬取豆瓣流浪地球影评,生成词云

    代码很简单,一看就懂. (没有模拟点击,所以都是未展开的) 地址: https://movie.douban.com/subject/26266893/reviews?rating=&star ...

  3. 多线程编程之Apue3rd_Chapter15.10之posix信号量

    看了APUE的chapter15,只重点看了15.10,学习了posix信号量.Posix信号量比起xsi信号量的优点是性能更好,在Linux3.2.0平台上性能提升很大.其中命名信号量使用方法如下. ...

  4. CentOS(Linux)安装KETTLE教程 并配置执行定时任务

    1,首先是安装jdk,并设置环境变量 采用yum安装可不设置环境变量 2,下载kettle https://sourceforge.net/projects/pentaho/files/Data%20 ...

  5. mtools使用-1

    mtools是什么? mtools 是一组非常好用的 MongoDB 日志分析工具 ,由MongoDB Inc 官方工程师所写. 组成部分 mlogfilter :按时间切片日志文件,合并日志文件,过 ...

  6. Python自动化运维——文件内容差异对比

    Infi-chu: http://www.cnblogs.com/Infi-chu/ 模块:difflib 安装:Python版本大于等于2.3系统自带 功能:对比文本之间的差异,而且支持输出可读性比 ...

  7. R语言学习笔记(一):mode, class, typeof的区别

    要了解这三个函数的区别,先了解numeric, double与integer. 在r中浮点数有两个名字叫numeric与double. double是指它的类型(type)名字,numeric是指它的 ...

  8. 为什么我要放弃javaScript数据结构与算法(第二章)—— 数组

    第二章 数组 几乎所有的编程语言都原生支持数组类型,因为数组是最简单的内存数据结构.JavaScript里也有数组类型,虽然它的第一个版本并没有支持数组.本章将深入学习数组数据结构和它的能力. 为什么 ...

  9. Java线程和多线程(九)——死锁

    Java中的死锁指的就是一种多于两个线程永远阻塞的特殊状况.Java中的死锁状态至少需要多于两个线程以及资源的时候才会产生.这里,我写了一个产生死锁的程序,并且讲下如何分析死锁. 首先来看一下产生死锁 ...

  10. LeetCode:14. Longest Commen Prefix(Easy)

    1. 原题链接 https://leetcode.com/problems/longest-common-prefix/description/ 2. 题目要求 给定一个字符串数组,让你求出该数组中所 ...