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个点的树,你从根开始游走,每当你第一次到达一个点时,把这个点的权记为(你已经到过不 ...
随机推荐
- ALVのイベントを取得する方法
概要 表示されたALVをダブルクリックした時に別画面へ遷移する方法を説明しよう.下記サンプルのように標準トランザクションへ遷移したり.別のALVを表示したりする事が可能である. サンプルコード ABA ...
- 如何保证HashMap线程安全
可使用Java 1.5推荐的java.util.concurrent包ConcurrentHashMap来实现,内部不再使用类似HashTable的synchronized同步锁,而是使用Reentr ...
- linux c fgetc()
今天练习代码的时候碰见这样一个问题: 一个文件test.txt,文件内容为 1 2 4 5 在程序中读写这个文件,修改其内容,添加一行,将文件内容变成: 1 2 3 4 5 楼主的错误代码是这样的: ...
- Django的命令操作,python
忘记时候,查看命令用:python manage.py 1 建立项目的命令: django-admin.py startproject project_name 2 在项目的目录下建立app: dja ...
- Spring MVC: 环境搭建并实现简易的HelloWorld
第一步:使用配置Tomcat服务器的Eclipse新建一个名为“TestSpringMVC”的web项目 第二步:将所使用的jar包复制到WEB-INF/lib目录下 第三步:在web.xml中配置D ...
- C# String函数
public static bool IsNullOrEmpty(string value) 如果 true 参数为 value 或空字符串 (""),则为 null:否则为 fa ...
- Linux - 信息收集
1. #!,代表加载器(解释器)的路径,如: #!/bin/bash echo "Hello Boy!" 上面的意思是说,把下面的字符(#!/bin/bash以下的所有字符)统统传 ...
- VM打开虚拟机文件报错
用VM打开以前的虚拟机文件报错 Cannot open the disk 'F:/****.vmdk' or one of the snapshot disks it depends on. 这种问题 ...
- web端常见兼容性问题整理
一.html和css 各浏览器的默认内外边距不一致问题 最明显的是ul标签内外边距问题,ul标签在IE-7中,有个默认的外边距,但是在IE8以上及其他浏览器中有个默认的内边距. 解决办法:*{marg ...
- 为Zabbix配置Nova服务、Keystone和Placement进程CPU和内存usage监控
目前已经完成了RabbitMQ和MySQL的监控项配置,还差对nova-api.nova-conductor.nova-scheduler和keystone进程CPU和内存 usage的监控,类似的轮 ...