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个点的树,你从根开始游走,每当你第一次到达一个点时,把这个点的权记为(你已经到过不 ...
随机推荐
- socket编程基础1——hostent、in_addr、gethostbyname、inet_ntoa
1. struct hostent结构体 struct hostent { char *h_name; char **h_aliases; int h_addrtype; int h_length; ...
- c语言中 *p++ 和 (*p)++ 和 *(p++) 和 *(++p) 和++(*p)和 *(p--)和 *(--p)有什么区别?
*p++是指下一个地址; (*p)++是指将*p所指的数据的值加一; /******************解释**********************/ ->C编译器认为*和++是同优先级 ...
- R语言学习笔记(八):零碎知识点(16-20)
16--complete.cases( ) complete.case()可以判断对象中是否数据完全,然后返回TRUE, FALSE 这一函数在去除数据框中缺失值时很有用. > d kids a ...
- java使用urlConnection抓取部分数据乱码
使用urlconnection做抓取的同学应该一开始都是使用这个吧.OK回到正题来..... 在内容己有中文.英文己正常显示,仍然会有部分中文或英文出现乱码,这是为什么呢?这个问题一直在心里盘旋... ...
- 查看sql 作业明细及运行记录
--查看作业明细及状态 select j.name 'Job名', j.description '描述', j.ENABLED job_enabled, cast(js.last_run_date a ...
- 也谈js传值和传址
通常的认识就是基本的数值元素是传值,对象等复杂结构传址,无需争论,一试便知. 首先是数值 var a = 1 var b = a a = 2 console.log(a) console.log(b) ...
- Fiddler 发送post 请求失败
今天服务端同事,让我发一个post 请求.然后呢,一直有问题.告诉我签名失败. 后来换了其他的在线模拟post,都是可以的. 后来找到原因了, post 请求,必须要有Content-Type 和 C ...
- Django 2.0官方文档中文 渣翻 总索引(个人学习,欢迎指正)
Django 2.0官方文档中文 渣翻 总索引(个人学习,欢迎指正) 置顶 2017年12月08日 11:19:11 阅读数:20277 官方原文: https://docs.djangoprojec ...
- 在PXC中重新添加掉线节点
Preface When we add a new node into PXC structure,it will estimate the mothed(IST/SST) to tr ...
- 「日常训练」 Finite or not? (CFR483D2C)
题意(Codeforces 984C) 给定p,q,b" role="presentation">p,q,bp,q,b,问pq" role="p ...