hdu 6253 (bfs打表)
链接: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打表)的更多相关文章
- HDU - 6253 Knightmare (打表+拉格朗日插值)
题目链接 题意:一个马在无限大的棋盘中跳,问跳n步能跳到多少个不同的格子. 首先写个打表程序打一下n比较小的时候的表: #include<bits/stdc++.h> using name ...
- HDU 1043 Eight(反向BFS+打表+康托展开)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1043 题目大意:传统八数码问题 解题思路:就是从“12345678x”这个终点状态开始反向BFS,将各 ...
- hdu-1043(八数码+bfs打表+康托展开)
参考文章:https://www.cnblogs.com/Inkblots/p/4846948.html 康托展开:https://blog.csdn.net/wbin233/article/deta ...
- HDU1043 八数码(BFS + 打表)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1043 , 康托展开 + BFS + 打表. 经典八数码问题,传说此题不做人生不完整,关于八数码的八境界 ...
- HDU1430 BFS + 打表 + 康托展开
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1430 , 一道比较好的题. 这道题要用到很多知识,康托展开.BFS.打表的预处理还要用到一一映射,做完 ...
- 【算法系列学习三】[kuangbin带你飞]专题二 搜索进阶 之 A-Eight 反向bfs打表和康拓展开
[kuangbin带你飞]专题二 搜索进阶 之 A-Eight 这是一道经典的八数码问题.首先,简单介绍一下八数码问题: 八数码问题也称为九宫问题.在3×3的棋盘,摆有八个棋子,每个棋子上标有1至8的 ...
- hdu 1496 Equations hash表
hdu 1496 Equations hash表 题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1496 思路: hash表,将原来\(n^{4}\)降 ...
- hdu 4531 bfs(略难)
题目链接:点我 第一次不太清楚怎么判重,现在懂了,等下次再做 /* *HDU 4531 *BFS *注意判重 */ #include <stdio.h> #include <stri ...
- hdu1043 经典的八数码问题 逆向bfs打表 + 逆序数
题意: 题意就是八数码,给了一个3 * 3 的矩阵,上面有八个数字,有一个位置是空的,每次空的位置可以和他相邻的数字换位置,给你一些起始状态 ,给了一个最终状态,让你输出怎么变换才能达到目的. 思路: ...
随机推荐
- Dockerfile centos7_php5.6.36
Dockerfile: FROM centos:7 MAINTAINER www.ctnrs.com RUN yum install epel-release -y && \ yum ...
- Mysql多实例添加到开机自启的方法
Mysql多实例配置成功后,想让配置成开机自启. 首先看一下Linux启动的知识点,顺序如下. 1 加载内核2 执行init程序3 /etc/rc.d/rc.sysinit # 由init执行的第 ...
- 02-HTML之head标签
head标签 head内常用标签表 标签 类型 意义 <title></titile> 双闭合标签 定义网页标题 <style></style> 双闭合 ...
- 未能加载文件或程序集"Newtonsoft.Json, Version=4.5.0.0
这问题遇到好几次了,重新更改了引用都不好使,有的时候版本改成一致就好了,但是有的地方你不知道在哪里用了就不好排查,所性在config里面加个配置让程序运行的时候去处理得了~ 很实用,放在configu ...
- shell脚本使用记录一:操作文件
一,连接远程数据库(保证在服务器上能使用mysql命令行,至少要安装mysql客户端) #!/bin/bash HOSTNAME="ip" PORT=" USERNAME ...
- 学习WebSocket
初识WebSocket: index.html <!DOCTYPE html> <html> <head> <meta charset="UTF-8 ...
- Laravel 获取 Route Parameters (路由参数) 的 5 种方法
Laravel 获取路由参数的方式有很多,并且有个小坑,汇总如下. 假设我们设置了一个路由参数: 现在我们访问 http://test.dev/1/2 在 TestController ...
- [转帖]Windows注册表内容详解
Windows注册表内容详解 来源:http://blog.sina.com.cn/s/blog_4d41e2690100q33v.html 对 windows注册表一知半解 不是很清晰 这里学习一下 ...
- 【转帖】远程显示(操作) 服务器 GUI 程序(图形化界面) (基于 X11 Forwarding + Centos + MobaXterm)
远程显示(操作) 服务器 GUI 程序(图形化界面) (基于 X11 Forwarding + Centos + MobaXterm) https://zhuanlan.zhihu.com/p/310 ...
- CentOS7安装k8s
借鉴博客:https://www.cnblogs.com/xkops/p/6169034.html 此博客里面有每个k8s配置文件的注释:https://blog.csdn.net/qq_359048 ...