Description

有一幅n*n的方格图,n <=100,每个点上有一个值。

从(1,1)出发,走到(n,n),只能走上下左右。

每走一步花费t,每走三步需要花费走完三步后到达格子的值。

求最小花费的值。

Sample Input

4 2

30 92 36 10

38 85 60 16

41 13 5 68

20 97 13 80

Sample Output

31


这题有两种思想,可以强行上一个三维广搜,不过我们也可以用奇技淫巧将第三维省去,因为走三步总共只有16种状态。所以我们就可以强上广搜了。最后只要把那些可以在3步内可以到(n,n)的点特判一下就好了

#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define inf 0x7f7f7f7f
using namespace std;
typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
inline int read(){
int x=0,f=1;char ch=getchar();
for (;ch<'0'||ch>'9';ch=getchar()) if (ch=='-') f=-1;
for (;ch>='0'&&ch<='9';ch=getchar()) x=(x<<1)+(x<<3)+ch-'0';
return x*f;
}
inline void print(int x){
if (x>=10) print(x/10);
putchar(x%10+'0');
}
const int N=1e2;
const int dx[16]={-3,-2,-2,-1,-1,-1,0,0,0,0,1,1,1,2,2,3};
const int dy[16]={0,-1,1,-2,0,2,-3,-1,1,3,-2,0,2,-1,1,0};
struct AC{
int x,y;
void join(int a,int b){x=a,y=b;}
}h[N*N*16+10];
int map[N+10][N+10],dis[N+10][N+10];
bool vis[N+10][N+10];
int n,t;
int in_map(int x,int y){return x>0&&x<=n&&y>0&&y<=n;}
void Bfs(int x,int y){
int head=1,tail=1;
memset(dis,63,sizeof(dis));
h[1].join(x,y),vis[x][y]=1,dis[x][y]=0;
for (;head<=tail;head++){
int nx=h[head].x,ny=h[head].y;
for (int i=0;i<16;i++){
int tx=nx+dx[i],ty=ny+dy[i];
if (!in_map(tx,ty)) continue;
if (dis[tx][ty]>dis[nx][ny]+map[tx][ty]+3*t){
dis[tx][ty]=dis[nx][ny]+map[tx][ty]+3*t;
if (!vis[tx][ty]) h[++tail].join(tx,ty),vis[tx][ty]=1;
}
}
vis[nx][ny]=0;
}
}
int main(){
n=read(),t=read();
for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) map[i][j]=read();
Bfs(1,1);
for (int x=0;x<3;x++)
for (int y=0;y<3;y++)
if (x+y<3)
dis[n][n]=min(dis[n][n],dis[n-x][n-y]+t*(x+y));
printf("%d\n",dis[n][n]);
return 0;
}

[Usaco2017 Feb]Why Did the Cow Cross the Road I (Gold)的更多相关文章

  1. [Usaco2017 Feb]Why Did the Cow Cross the Road II (Gold)

    Description 上下有两个长度为n.位置对应的序列A.B, 其中数的范围均为1~n.若abs(A[i]-B[j])<= 4,则A[i]与B[j]间可以连一条边. 现要求在边与边不相交的情 ...

  2. [Usaco2017 Feb]Why Did the Cow Cross the Road III (Gold)

    Description 给定长度为2N的序列,1~N各处现过2次,i第一次出现位置记为ai,第二次记为bi,求满足ai < aj < bi < bj的对数 Sample Input ...

  3. 4990: [Usaco2017 Feb]Why Did the Cow Cross the Road II 线段树维护dp

    题目 4990: [Usaco2017 Feb]Why Did the Cow Cross the Road II 链接 http://www.lydsy.com/JudgeOnline/proble ...

  4. 4989: [Usaco2017 Feb]Why Did the Cow Cross the Road

    题面:4989: [Usaco2017 Feb]Why Did the Cow Cross the Road 连接 http://www.lydsy.com/JudgeOnline/problem.p ...

  5. [BZOJ4990][Usaco2017 Feb]Why Did the Cow Cross the Road II dp

    4990: [Usaco2017 Feb]Why Did the Cow Cross the Road II Time Limit: 10 Sec  Memory Limit: 128 MBSubmi ...

  6. [BZOJ4989][Usaco2017 Feb]Why Did the Cow Cross the Road 树状数组维护逆序对

    4989: [Usaco2017 Feb]Why Did the Cow Cross the Road Time Limit: 10 Sec  Memory Limit: 256 MBSubmit:  ...

  7. [bzoj4994][Usaco2017 Feb]Why Did the Cow Cross the Road III_树状数组

    Why Did the Cow Cross the Road III bzoj-4994 Usaco-2017 Feb 题目大意:给定一个长度为$2n$的序列,$1$~$n$个出现过两次,$i$第一次 ...

  8. BZOJ4997 [Usaco2017 Feb]Why Did the Cow Cross the Road III

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ4997 题意概括 在n*n的区域里,每一个1*1的块都是一个格子. 有k头牛在里面. 有r个篱笆把格 ...

  9. BZOJ4994 [Usaco2017 Feb]Why Did the Cow Cross the Road III 树状数组

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ4994 题意概括 给定长度为2N的序列,1~N各处现过2次,i第一次出现位置记为ai,第二次记为bi ...

随机推荐

  1. Nexus搭建Maven私有仓库

    原文:http://blog.csdn.net/rickyit/article/details/54927101 前言 Nexus Repository Manager is a Javaapplic ...

  2. Org-mode五分钟教程ZZZ

    Table of Contents 1 源起 2 简介 2.1 获取 org-mode 2.2 安装 3 基础用法 3.1 创建一个新文件 3.2 简单的任务列表 3.3 使用标题组织一篇文章 3.4 ...

  3. soapUI系列之—-01 介绍soapUI简介,groovy 简介

    1.soapui简介 SoapUI是一个自由和开放源码的跨平台功能测试解决方案.通过一个易于使用的图形界面和企业级功能,SoapUI让您轻松,快速创建和执行自动化功能.回归.合规和负载测试.在一个测试 ...

  4. C# 性能优化 之 秒表 Stopwatch。 Dapper一个和petapoco差不多的轻量级ORM框架

    Sweet小马 小马同学的编程日记. C# 性能优化 之 秒表 Stopwatch. 生词解释:Diagnostics[,daɪəg'nɑstɪks] n.诊断学 using System.Diagn ...

  5. iOS开发人员:事实上你还有非常多东西须要学

    iOS 新特性总结(since iOS6) iOS 6 1.废除viewDidUnLoad 收到内存警告须要到didReceiveMemoryWarning中处理 [小技巧] -(void)didRe ...

  6. POJ 1936 All in All(串)

    All in All Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 27537 Accepted: 11274 Descript ...

  7. eclipse中导入其它的webproject遇到和解决的问题

    注:下面为我从网上搜来的方法.经使用及学习后整理. 学习javaweb有段时间了.对于导入新项目.遇到好多问题.但终于成功了. 错误1:string cannot be resolved to a t ...

  8. Sublime Text 3.3143最新注册码

    Sublime Text3之前用的是老版本,昨天手贱点了更新,就要重新激活,网上找了好多都是之前版本的,以下给干货 —– BEGIN LICENSE —– TwitterInc User Licens ...

  9. hdu4737A Bit Fun 线段树

    //给一串序列,问有多少对[i,j]使得 //[i,j]区间的全部数的或的值小于m //能够知道'或'操作的加(a|b)>=max(a,b) //能够枚举区间的右边r,找左边第一个不满足的位置 ...

  10. HDU OJ u Calculate e

    这是一道简单的数学计算问题   主义好输出格式就好 #include<stdio.h> int main() { printf("n e\n- -----------\n&quo ...