1358 棋盘游戏[状压dp]
2024-08-24 02:29:43
题目描述 Description
这个游戏在一个有10*10个格子的棋盘上进行,初始时棋子位于左上角,终点为右下角,棋盘上每个格子内有一个0到9的数字,每次棋子可以往右方或下方的相邻格子移动,求一条经过数字之和最小且经过0到9的所有数字的合法路径,输出其长度。(经过的数字包括左上角和右下角)
输入描述 Input Description
输入包含10行,每行10个数字,以空格隔开,表示棋盘格子上的权值。数据保证存在合法路径。
输出描述 Output Description
输出所求路径的权值和。
样例输入 Sample Input
0 1 2 3 4 5 6 7 8 9
1 1 1 1 1 1 1 1 1 0
2 1 1 1 1 1 1 1 1 0
3 1 1 1 1 1 1 1 1 0
4 1 1 1 1 1 1 1 1 0
5 1 1 1 1 1 1 1 1 0
6 1 1 1 1 1 1 1 1 0
7 1 1 1 1 1 1 1 1 0
8 1 1 1 1 1 1 1 1 0
9 1 1 1 1 1 1 1 1 5
样例输出 Sample Output
50
数据范围及提示 Data Size & Hint
【样例解释】
先一直向右走到第一行末尾,再竖直向下走位最优路径。
#include<cstdio>
#include<cstring>
#include<iostream>
#define EF if(ch==EOF) return x;
using namespace std;
const int N=;
int n,g[N][N],f[<<N][N][N];
inline int read(){
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;EF;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
//f[S][i][j]已经走过S集合中的点(0-9),当前在(i,j)的最小花费
void dp(){
memset(f,0x3f,sizeof f);
f[<<g[][]][][]=g[][];
for(int S=;S<(<<);S++){
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
if(S&(<<g[i][j]))
f[S|(<<g[i+][j])][i+][j]=min(f[S|(<<g[i+][j])][i+][j],f[S][i][j]+g[i+][j]),
f[S|(<<g[i][j+])][i][j+]=min(f[S|(<<g[i][j+])][i][j+],f[S][i][j]+g[i][j+]);
}
}
}
printf("%d\n",f[(<<)-][n][n]);
}
int main(){
n=;
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
g[i][j]=read();
}
}
dp();
return ;
}
最新文章
- 使用vlc播放器做rtsp流媒体服务器
- 深入浅出JS的封装与继承
- iOS自动化编译
- 在ubuntu上搭建开发环境3---解决Y470一键系统重装之后恢复ubuntu引导启动的方法
- HDU 3974 Assign the task(dfs建树+线段树)
- cf C. Knight Tournament
- C#解决MDI窗体闪屏的方法
- python基础之 optparse.OptionParser
- android端从服务器抓取的几种常见的数据的处理方式
- UVa 10491 - Cows and Cars
- 求助帖:android开发初期:为什么我在活动二设置的singInstance模式跑到活动三去了???
- [Vue] vue2.0
- day 24 面向对象之继承及属性查找顺序
- Java课程课后作业02之动手动脑
- PLSQL基础知识-图片
- 从头到尾使用Geth的说明-1-安装
- day 29 socketsetserver 模块
- pringboot+mybatis+redis+cookie单点登录
- leetCode题解 Reverse Words in a String III
- mysql hibernate 关于默认值
热门文章
- Python强制抛出自定义异常
- 揭开Altera公司支持OpenCL的设计工具的神秘面纱
- location 设定某个文件的过期时间,并不记录访问日志
- ngBind {{}} ngBindTemplate
- linux命令之fuser
- 解决:Access denied for user &#39;root&#39;@&#39;localhost&#39; (using password: YES)
- jsp error-page没有生效
- 实现itoa()
- Unix系统编程()brk,sbrk
- Linux网络编程wait()和waitpid()的讲解