[Usaco2017 Feb]Why Did the Cow Cross the Road
来源:互联网 发布:手机直播全套源码 编辑:程序博客网 时间:2024/06/08 17:36
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 0x7f7f7f7fusing 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;}
阅读全文
0 0
- 4992: [Usaco2017 Feb]Why Did the Cow Cross the Road
- bzoj4995: [Usaco2017 Feb]Why Did the Cow Cross the Road
- bzoj4989: [Usaco2017 Feb]Why Did the Cow Cross the Road
- Bzoj4989 [Usaco2017 Feb]Why Did the Cow Cross the Road
- [Usaco2017 Feb]Why Did the Cow Cross the Road
- [Usaco2017 Feb]Why Did the Cow Cross the Road II
- [Usaco2017 Feb]Why Did the Cow Cross the Road III
- bzoj 4993: [Usaco2017 Feb]Why Did the Cow Cross the Road II
- BZOJ 4993: [Usaco2017 Feb]Why Did the Cow Cross the Road II LCS
- BZOJ 4990: [Usaco2017 Feb]Why Did the Cow Cross the Road II 树状数组优化LCS
- BZOJ 4989: [Usaco2017 Feb]Why Did the Cow Cross the Road 逆序对
- 4994: [Usaco2017 Feb]Why Did the Cow Cross the Road III
- bzoj 4990: [Usaco2017 Feb]Why Did the Cow Cross the Road II
- bzoj4993 [Usaco2017 Feb]Why Did the Cow Cross the Road II
- bzoj4994 [Usaco2017 Feb]Why Did the Cow Cross the Road III
- bzoj4993: [Usaco2017 Feb]Why Did the Cow Cross the Road II
- bzoj4994: [Usaco2017 Feb]Why Did the Cow Cross the Road III
- bzoj 4994: [Usaco2017 Feb]Why Did the Cow Cross the Road III
- TP事务提交
- GeoStru.Slope.2018.25.6.1275土壤岩石边坡
- 《java核心技术卷一》第三章
- 微信小程序
- 高并发架构
- [Usaco2017 Feb]Why Did the Cow Cross the Road
- ZooKeeper实战应用之【统一配置管理】
- python变量类型和容器
- test
- postgresql实现跨数据库查询,采用dblink的形式
- Jacobian矩阵和Hessian矩阵
- MMU内存管理单元
- Redis在Centos7下的集群安装
- IOS每日精选源码,边缘识别导航条管理高仿今日头条语音查询汇率源码