【牛客 - 301哈尔滨理工大学软件与微电子学院第八届程序设计竞赛同步赛(高年级)】小乐乐下象棋(记忆化搜索dp,dfs)

题干:
 

小乐乐一天天就知道玩,这一天又想玩象棋。
我们都知道马走日。
现在给定一个棋盘,大小是n*m,把棋盘放在第一象限,棋盘的左下角是(0,0),右上角是(n - 1, m - 1);
小乐乐想知道,一个马从左下角(0, 0)开始,走了k步之后,刚好走到右上角(n - 1, m - 1)的方案数。

输入描述:

输入:多组样例输入,每组一行,三个整数n, m, k(1 <= n, m, k <= 200),如题目所示。

输出描述:

输出:输出答案 mod 1000000007

示例1

输入

复制

4 4 2

输出

复制

2

解题报告:

   跪了一晚上发现是因为写成了ty=x+ny[k]了,,,看来是太久不写地图的dfs了、、老毛病又犯了、、难受难受

AC代码1:(记忆化搜索)

  其实也可以直接在main函数中dp[n][m][0]=1,这样在写dfs的时候就不需要特判一下出口了、、总的来说是个不算难的好题、、

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
const int MAX = 2e5 + 5;
ll dp[205][205][205];
const ll mod = 1000000007;
int n,m,t;
int nx[9] = {-2,-1,1,2,2,1,-1,-2};
int ny[9] = {1,2,2,1,-1,-2,-2,-1};
bool ok(int x,int y) {if(x>=1&&x<=n&&y>=1&&y<=m) return 1;else return 0;
}
ll dfs(int x,int y,int res) {if(x == n && y == m && res == 0) return dp[n][m][res]=1;if(res <= 0) return 0;if(dp[x][y][res]!=-1) return dp[x][y][res];ll sum = 0;int tx,ty;for(int k = 0; k<8; k++) {tx = x + nx[k];ty = y + ny[k];if(ok(tx,ty) == 0) continue;//if(res==0) continue;sum += dfs(tx,ty,res-1);sum %= mod;}dp[x][y][res] = sum;return sum;}
int main()
{while(~scanf("%d%d%d",&n,&m,&t)) {memset(dp,-1,sizeof dp);printf("%lld\n",dfs(1,1,t)%mod);}	return 0 ;}

其中也有一个细节值得注意,,这样搜索的正确性,,首先因为你表示的状态,体现出了剩余的步数,所以不用怕走过去下一步又走回来这种情况,,因为在状态设计中这属于不同的状态,,所以没事,,再就是不用怕会出现环,,也就是绕一圈状态又绕回来了。。原因就是因为你的dfs都是res-1的操作,所以整个就是一个DAG图,不必要担心会出现未完成值的重复调用。。

AC代码2:(三种直接dp)

#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define pii pair<int,int>
#define x first
#define y second
#define mp make_pair
//#define debug
const LL mod=1e9+7;
LL f[205][205][205];
int fx[8][2]={1,2,1,-2,2,1,2,-1,-1,-2,-1,2,-2,-1,-2,1};
void B(){int n,m,K;while(cin>>n>>m>>K){memset(f,0,sizeof(f));f[1][1][0]=1;for(int k=0;k<=K;++k){for(int i=1;i<=n;++i){for(int j=1;j<=m;++j){if(f[i][j][k]){for(int o=0;o<8;++o){if(i+fx[o][0]>=1 && j+fx[o][1]>=1)(f[i+fx[o][0]][j+fx[o][1]][k+1]+=f[i][j][k])%=mod; }}}}}cout<<f[n][m][K]<<endl;}
}
int main(){B();return 0;
}#include<bits/stdc++.h>
#define mod 1000000007
using namespace std;
int dp[205][205][205];
int dis[8][2]={2,1,2,-1,-2,1,-2,-1,1,2,1,-2,-1,2,-1,-2};
int main()
{int n,m,k;while(scanf("%d%d%d",&n,&m,&k)!=EOF){memset(dp,0,sizeof(dp));dp[1][1][0]=1;for(int s=1;s<=k;s++)for(int i=1;i<=n;i++)for(int j=1;j<=m;j++){int temp=0;for(int h=0;h<8;h++){int x=i+dis[h][0];int y=j+dis[h][1];if(x>=1&&x<=n&&y>=1&&y<=m)temp+=dp[x][y][s-1],temp%=mod;}dp[i][j][s]=temp;}printf("%d\n",dp[n][m][k]);
}return 0;
}#include <bits/stdc++.h>
using namespace std;
int dp[205][205][205];
const int mod=1000000007;
int nex[8][2]={{1,2},{2,1},{-1,-2},{-2,-1},{2,-1},{1,-2},{-1,2},{-2,1}};
int main()
{int n,m,d;while(~scanf("%d %d %d",&n,&m,&d)){memset(dp,0,sizeof(dp));dp[1][1][0]=1;for(int k=1;k<=d;k++){for(int i=1;i<=n;i++)for(int j=1;j<=m;j++){for(int s=0;s<8;s++){int x=i+nex[s][0];int y=j+nex[s][1];if(x<1||y<1||x>n||y>m) continue;dp[i][j][k]=(dp[i][j][k]+dp[x][y][k-1])%mod;}}}printf("%d\n",dp[n][m][d]);}return 0;
}

看了dp的代码发现dp好像也不是很难写、、、也不是很难想,,因为这种状态也是符合递推的。。

简单的一个题,,着实又让我对动态规划有了新的理解!!!!

考虑改编:

    改编成一个n*m的方格,但是终点不是右上角的那个[n,m]点,而是新输入的两个点,,这样进行dp,,但是可能有个问题就是可能搜索的范围会大了很多啊。。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/441084.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

java 递归 时间复杂度_递归到底是怎么实现的?它的时间复杂度怎么算?

递归到底是个啥&#xff1f;常听见的一句话就是&#xff1a;自己调用自己。按照这个说法&#xff0c;写个简单的递归自己推导一下的确可以&#xff0c;但是总是有点绕&#xff0c;推着推着自己把自己陷进去了。递归函数运行时&#xff0c;实际上会进行一个压栈(思考栈的特点&am…

【牛客 - 301哈尔滨理工大学软件与微电子学院第八届程序设计竞赛同步赛(高年级)】小乐乐搭积木(状压dp)

题干&#xff1a; 小乐乐想要给自己搭建一个积木城堡。 积木城堡我们假设为n*m的平面矩形。 小乐乐现在手里有1*2&#xff0c;2*1两种地砖。 小乐乐想知道自己有多少种组合方案。 输入描述: 第一行输入整数n,m。(1<n,m<10) 输出描述: 输出组合方案数。 示例1 输…

Java 重定向 无法写入_java IO 文件读入,写入,重定向

Java代码 packagestar20110526;importjava.io.BufferedInputStream;importjava.io.BufferedOutputStream;importjava.io.BufferedReader;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileNotFoundException;importjava.io.FileOutputStream;importjava.io…

【HDU - 1078】FatMouse and Cheese (记忆化搜索dp)

题干&#xff1a; FatMouse has stored some cheese in a city. The city can be considered as a square grid of dimension n: each grid location is labelled (p,q) where 0 < p < n and 0 < q < n. At each grid location Fatmouse has hid between 0 and 10…

获取excel名称java_使用Apache POI获取大型Excel文件的Excel工作表名称

小编典典为了显示Gagravarr的评论可能意味着什么&#xff1a;该XSSFReader包含方法XSSFReader.getSheetsData其中“返回一个迭代器&#xff0c;这将让你在把所有的不同的表&#xff0c;每个表的InputStream中只有打开时开始迭代器牵强。这是给你的时候&#xff0c;每个做关闭In…

【CodeForces - 298C】Parity Game (思维,有坑)

题干&#xff1a; You are fishing with polar bears Alice and Bob. While waiting for the fish to bite, the polar bears get bored. They come up with a game. First Alice and Bob each writes a 01-string (strings that only contain character "0" and &q…

java求小数高精度_浅谈Java中的高精度整数和高精度小数

在实际编码中&#xff0c;会遇到很多高精度的事例&#xff0c;比如&#xff0c;在计算金钱的时候就需要保留高精度小数&#xff0c;这样计算才不会有太大误差&#xff1a;在下面的代码中&#xff0c;我们验证了&#xff0c;当两个float型的数字相加&#xff0c;得到的结果和我们…

【CodeForces - 298B 】Sail (模拟,题意)

题干&#xff1a; The polar bears are going fishing. They plan to sail from (sx, sy) to (ex, ey). However, the boat can only sail by wind. At each second, the wind blows in one of these directions: east, south, west or north. Assume the boat is currently…

java中能构成循环的语句_《编程导论(Java)#183;3.2.4 循环语句》

本文全然复制《编程导论(Java)3.2.4 循环语句》的内容。除【】中的说明文字。请阅读和比較其它编程教材。我知道。假设我是一个刚開始学习的人&#xff0c;《编程导论(Java)》非常不适合自学。建议同学们阅读时&#xff0c;一定选择一本其它的书同一时候看&#xff0c;或上网。…

【CodeForces - 340B 】Maximal Area Quadrilateral (计算几何,枚举,有坑)

题干&#xff1a; Iahub has drawn a set of n points in the cartesian plane which he calls "special points". A quadrilateral is a simple polygon without self-intersections with four sides (also called edges) and four vertices (also called corners)…

php网站的编辑器,5款适合PHP使用的HTML编辑器推荐

概述Web开发中&#xff0c;很多地方都会用到HTML编辑器(富文本编辑器)&#xff0c;我也用过几种&#xff0c;如UEditor、CkEditor等。这几天看了几篇文章&#xff0c;都是关于HTML编辑器的&#xff0c;写个文章记录下。推荐的编辑器simditor这个编辑器是前几天刚看到的&#xf…

【牛客 - 301哈尔滨理工大学软件与微电子学院第八届程序设计竞赛同步赛(高年级 )】小乐乐和25(模拟,技巧)

题干&#xff1a; 小乐乐特别喜欢25这个数字&#xff0c;他想把所有的数字都变成25的倍数。 现在小乐乐得到一个数字&#xff0c;想问问你最少用几次操作才可以把这个数字改造成25的倍数。 对于一次操作我们可以把相邻的两位做交换&#xff0c;比如123经过一次操作之后就可以…

怎么表示一个PHP语句块,php switch语句多个值匹配同一代码块应用示例

先说说switch()语句的格式switch(表达式){case 匹配1&#xff1a;当匹配1和表达式匹配成功执行的代码;break;case 匹配2&#xff1a;当匹配2和表达式匹配成功执行的代码;break;default&#xff1a;如果case语句没有与表达式成功所执行的代码;}理解 switch 是怎样执行的非常重要…

php查找顶级分类,php 无限级分类 获取顶级分类ID,php顶级_PHP教程

php 无限级分类 获取顶级分类ID&#xff0c;php顶级有这样一个表&#xff0c;id是分类的ID&#xff0c;name是分类名称&#xff0c;pid是上级分类的ID。现在有个分类ID&#xff0c;程序要找到它上级的上级的上级……分类的ID&#xff0c;简单说就是找出顶级分类的ID。比如“新鲜…

【POJ - 2392】Space Elevator (dp,优秀的背包问题)

题干&#xff1a; The cows are going to space! They plan to achieve orbit by building a sort of space elevator: a giant tower of blocks. They have K (1 < K < 400) different types of blocks with which to build the tower. Each block of type i has heigh…

php三种web开发技术,三种WEB开发主流技术ASP-PHP-JSP的评价

三种Web开发主流技术ASP-PHP-JSP的评价[摘要]本文从程序开发者的角度&#xff0c;对现今社会盛行的WEB开发技术进行了分析&#xff0c;旨在让WEB 开发人员、使用者、准备学习的人对WEB开发技术有更加深入的了解&#xff0c;并为WEB应用程序开发提供指导。[关键词]WEB开发技术 A…

*【CodeForces - 1088 ABC】套题比赛,A水题B模拟C构造D交互

A. Input The only line contains the integer xx (1≤x≤100)(1≤x≤100). Output You should output two integers aa and bb, satisfying the given conditions, separated by a space. If no pair of integers satisfy the conditions above, print "-1" (wit…

php 计算前几天,php计算几分钟前、几小时前、几天前的几个函数、类分享

搜索热词一、函数实现实例1&#xff1a;代码如下:function time_tran($the_time){$now_time date("Y-m-d H:i:s",time()8*60*60);$now_time strtotime($now_time);$show_time strtotime($the_time);$dur $now_time - $show_time;if($dur < 0){return $the_tim…

【CodeForces - 608D】Zuma(区间dp)

题干&#xff1a; Genos recently installed the game Zuma on his phone. In Zuma there exists a line of n gemstones, the i-th of which has color ci. The goal of the game is to destroy all the gemstones in the line as quickly as possible. In one second, Geno…

matlab没有int函数,matlab 未定义与 'char' 类型的输入参数相对应的函数 'int'。

最后你那zd句plot(int(k),double(s));里的int(k);不对。你如果想以k为横坐标&#xff0c;直接把int去掉&#xff0c;如回果非想要变成整数可以用floor(k);注&#xff1a;fix&#xff1a;向零取整 floor&#xff1a;向小取整 round&#xff1a;四舍五入 ceil&#xff1a;向...如…