链接:http://acm.hdu.edu.cn/showproblem.php?pid=6253

题意:

马可以往一个方向走两步,然后转个弯走一步,这样算一次动作,求问马n次动作后,能到达多少个点,重复到达的点只算一次。

思路:

一开始完全没思路,画图找了半天把自己画崩了,后面看到数据和样例感觉这应该是一道公式题,然后打了一个表。。

打表代码:

#include<bits/stdc++.h>
using namespace std;
#define ull unsigned long long struct node{
int x,y,step;
}; int dx[] = {,,-,-,,,-,-};
int dy[] = {,-,,-,,-,,-};
int ans[],vis[][];
void bfs(){
node now;
queue<node>q;
now.x = ,now.y = ,now.step = ;
ans[] = ;
vis[][] = ;
q.push(now);
while(!q.empty()){
node now = q.front();
q.pop();
if(now.step == ) continue;
node nex;
for(int i = ;i < ;i ++){
nex.x = now.x + dx[i];
nex.y = now.y + dy[i];
if(vis[nex.x][nex.y]==)
nex.step = now.step+,q.push(nex),ans[nex.step]++,vis[nex.x][nex.y]=;
}
}
for(int i = ;i <= ;i ++)
cout<<ans[i]<<" ";//ans[i+1] += ans[i];
cout<<endl;
} int main()
{
bfs();
return ;
}

这个表求得是每一步多增加的点数,可以得到以下数据

1 8 32 68 96 120 148 176 204 232 260 288 316 344 372 400 428 456 484 512 540

我们可以发现这张表从120开始后面每一次都是+28

从120开始这个序列就可以变成一个等差数列,但是题目是要我们求所有的,那就直接等差数列前n项和公式就好了,把第一项设为120或者120后面随意一个数字就好了,前n项求和后加上第一项前面的那些数的和就好了

这道题会爆long long ,我们用 unsigned long long 就好了

实现代码;

#include<bits/stdc++.h>
using namespace std;
#define ull unsigned long long
/*
struct node{
int x,y,step;
}; int dx[] = {2,2,-2,-2,1,1,-1,-1};
int dy[] = {1,-1,1,-1,2,-2,2,-2};
int ans[10000],vis[1000][1000];
void bfs(){
node now;
queue<node>q;
now.x = 500,now.y = 500,now.step = 0;
ans[0] = 1;
vis[500][500] = 1;
q.push(now);
while(!q.empty()){
node now = q.front();
q.pop();
if(now.step == 20) continue;
node nex;
for(int i = 0;i < 8;i ++){
nex.x = now.x + dx[i];
nex.y = now.y + dy[i];
if(vis[nex.x][nex.y]==0)
nex.step = now.step+1,q.push(nex),ans[nex.step]++,vis[nex.x][nex.y]=1;
}
}
for(int i = 0;i <= 20;i ++)
cout<<ans[i]<<" ";//ans[i+1] += ans[i];
cout<<endl;
} int main()
{
bfs();
return 0;
}
*/
//1 8 32 68 96 120 148 176 204 232 260 288 316 344 372 400 428 456 484 512 540
//1 9 41 109 205 325 473 649 853 1085 1345 1633 1949 2293 2665 3065 3493 3949 4433 4945 5485 int a[] = {, , , , , ,};
int main()
{
ull t,n,cas = ;
ull ans;
cin>>t;
while(t--){
cin>>n;
if(n < ) cout<<"Case #"<<cas++<<": "<<a[n]<<endl;
else{
ans = *(n-) + (n-)*(n-)*;
cout<<"Case #"<<cas++<<": "<<ans+<<endl;
}
}
return ;
}

hdu 6253 (bfs打表)的更多相关文章

  1. HDU - 6253 Knightmare (打表+拉格朗日插值)

    题目链接 题意:一个马在无限大的棋盘中跳,问跳n步能跳到多少个不同的格子. 首先写个打表程序打一下n比较小的时候的表: #include<bits/stdc++.h> using name ...

  2. HDU 1043 Eight(反向BFS+打表+康托展开)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1043 题目大意:传统八数码问题 解题思路:就是从“12345678x”这个终点状态开始反向BFS,将各 ...

  3. hdu-1043(八数码+bfs打表+康托展开)

    参考文章:https://www.cnblogs.com/Inkblots/p/4846948.html 康托展开:https://blog.csdn.net/wbin233/article/deta ...

  4. HDU1043 八数码(BFS + 打表)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1043 , 康托展开 + BFS + 打表. 经典八数码问题,传说此题不做人生不完整,关于八数码的八境界 ...

  5. HDU1430 BFS + 打表 + 康托展开

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1430 , 一道比较好的题. 这道题要用到很多知识,康托展开.BFS.打表的预处理还要用到一一映射,做完 ...

  6. 【算法系列学习三】[kuangbin带你飞]专题二 搜索进阶 之 A-Eight 反向bfs打表和康拓展开

    [kuangbin带你飞]专题二 搜索进阶 之 A-Eight 这是一道经典的八数码问题.首先,简单介绍一下八数码问题: 八数码问题也称为九宫问题.在3×3的棋盘,摆有八个棋子,每个棋子上标有1至8的 ...

  7. hdu 1496 Equations hash表

    hdu 1496 Equations hash表 题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1496 思路: hash表,将原来\(n^{4}\)降 ...

  8. hdu 4531 bfs(略难)

    题目链接:点我 第一次不太清楚怎么判重,现在懂了,等下次再做 /* *HDU 4531 *BFS *注意判重 */ #include <stdio.h> #include <stri ...

  9. hdu1043 经典的八数码问题 逆向bfs打表 + 逆序数

    题意: 题意就是八数码,给了一个3 * 3 的矩阵,上面有八个数字,有一个位置是空的,每次空的位置可以和他相邻的数字换位置,给你一些起始状态 ,给了一个最终状态,让你输出怎么变换才能达到目的. 思路: ...

随机推荐

  1. Go源码编译安装

    参考文档1:https://www.cnblogs.com/majianguo/p/7258975.html 参考文档2:http://www.loongson.cn/news/company/456 ...

  2. Jq相关常用操作

    1.select下拉列表操作 $(".kstitle").live('change', function () { var workType = $(this).val(); // ...

  3. 关于XLL加载项动态加载、卸载的演示及XLL函数自定义类型注册的演示

    1.在XLL中,把函数定义成不同的类型,在Excel中的实际效果也不同,具体如下: pxMacroType value                                          ...

  4. semantic-ui 分段

    分段:用于创建一组相关联的内容. 1.创建一个基础的分段 在class中加一个segment的class即可 <div class="ui segment"> < ...

  5. 转:VIM选择文本块/复制/粘贴

    VIM选择文本块/复制/粘贴 - lcj_cjfykx的专栏 - CSDN博客https://blog.csdn.net/lcj_cjfykx/article/details/9091569

  6. 1px实现方案

    JS处理 首先,可以通过 window.devicePixelRatio 拿到设备的像素比,然后给 html 标签加上的相应的样式. function retina () { // 高分辨率屏幕处理 ...

  7. Yii的操作提示框

    效果如图 HTML + CSS<style> div.error{ background: #FFE0E0; border: 2px solid #FFA0A0; padding: 10p ...

  8. 【学亮IT手记】PL/SQL游标编程

    游标提供了一种从表中检索数据并进行操作的灵活手段,主要用在服务器上,处理由客户端发送给服务器端的sql语句,或者是批处理.存储过程.触发器中的数据处理请求. 显式游标 是由用户声明和操作的一种游标,通 ...

  9. Jenkins配置权限管理

    借鉴博客:https://www.cnblogs.com/Eivll0m/p/6734076.html 懒得写了,照上面是配置成功了,弄了权限角色与用户的配置

  10. vue中的适配:px2rem

    这应该是vue项目在适配移动端时候,最简单的方法之一下面是基本步骤(使用cnpm)1.下载并引入lib-flexible cnpm install --save lib-flexible 在main. ...