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个点的树,你从根开始游走,每当你第一次到达一个点时,把这个点的权记为(你已经到过不 ...
随机推荐
- Leecode刷题之旅-C语言/python-58.最后一个单词的长度
/* * @lc app=leetcode.cn id=58 lang=c * * [58] 最后一个单词的长度 * * https://leetcode-cn.com/problems/length ...
- BGP(边界网关协议)简述
BGP的起源 不同自治系统(路由域)间路由交换与管理的需求推动了EGP的发展,但是EGP的算法简单,无法选路,从而被BGP取代. 自治系统:(AS) IGP:自治系统内部协议,ospf,rip,is- ...
- 41-Individual authentication 模板
1-创建项目,进入vscode控制台,输出如下命令, uld表示指定mssqllocaldb E:\coding\netcore>dotnet new mvc -au Individual -u ...
- SAP ABAP Development Tools in Eclipseのセットアップ
手順 1. Eclipse IDE インストール 以下からダウンロード.https://tools.hana.ondemand.com/#abap※2018/1月現在 Oxygen(4.7)詳細は割愛 ...
- python2.7练习小例子(十三)
13):题目:将一个正整数分解质因数.例如:输入90,打印出90=2*3*3*5. 程序分析:对n进行分解质因数,应先找到一个最小的质数k,然后按下述步骤完成.(1)如果这个质数恰等于 ...
- 3,jieba gensim 最好别分家之最简单的相似度实现
简单的问答已经实现了,那么问题也跟着出现了,我不能确定问题一定是"你叫什么名字",也有可能是"你是谁","你叫啥"之类的,这就引出了人工智能 ...
- Android ImageSwitcher 配合Picasso解决内存溢出(OOM)问题
最近项目中用到了 ImageSwitcher 来实现图片切换,使用起来很简单,但发现当图片比较大(超过了3M)时,程序出现了内存溢出(OOM)问题而崩溃了. 原因就是图片太大了,显示到 ImageVi ...
- Java与C++进行系统间交互:Protocol Buffer
在一次项目中,因笔者负责的java端应用需要与公司C++系统进行交互,公司选定Protocol Buffer方案,故简单的了解一下 有需要的可以看一下其他作者的文章,了解一下Protobuf: htt ...
- jmeter上传视频图片附件
http上传附件一般用的Content-Type: multipart/form-data;文中是先通过fiddler抓取手机端的请求,然后通过jmeter模拟该请求,如果有接口文档,则可以跳过抓包这 ...
- CSP201512-1: 数位之和
引言:CSP(http://www.cspro.org/lead/application/ccf/login.jsp)是由中国计算机学会(CCF)发起的"计算机职业资格认证"考试, ...