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 的矩阵,上面有八个数字,有一个位置是空的,每次空的位置可以和他相邻的数字换位置,给你一些起始状态 ,给了一个最终状态,让你输出怎么变换才能达到目的. 思路: ...
随机推荐
- Codeblocks 遇到的问题 Cannot open output file, permission denied
Codeblocks下运行C++的程序时,偶尔会出现 Cannot open output file, permission denied 的问题,导致不能够编译. 在 Stack Overflow ...
- scrapy框架原理学习
Scrapy框架原理: 参考出处:https://cuiqingcai.com/3472.html 整个Scrapy的架构图: Scrapy Engine: 这是引擎,负责Spiders.ItemPi ...
- C语言之运算符、表达式和语句
#include<stdio.h> #define ADJUST 7.31 int main(void) { const double SCALE = 0.333; double shoe ...
- elk之查询方式(4种)
es 在查询时, 可以指定搜索类型为下面四种: QUERY_THEN_FETCH QUERY_AND_FEATCH DFS_QUERY_THEN_FEATCH DFS_QUERY_AND_FEATCH ...
- sql定时备份
老规矩,直接上代码: ) set @name='C:\Backup\MyStudy_'+ ),)+'.bak' BACKUP DATABASE[MyStudy]TO DISK=@name WITH N ...
- ES使用C#添加和更新文档
ElasticSearch 使用C#添加和更新文档 这是ElasticSearch 2.4 版本系列的第四篇: 第一篇:ES1:Windows下安装ElasticSearch 第二篇:ES2:Elas ...
- 封装day.js
封装day.js import dayjs from 'dayjs' import 'dayjs/locale/zh-cn' import relativeTime from 'dayjs/plugi ...
- C#设计模式之7:适配器模式
适配器模式 使用适配器模式的一个重要的点是首先要识别出什么代码(接口)是已经存在的,什么代码(接口)是新的,需要去适配的.适配器的作用是让旧的(现有的)接口能够匹配新的系统(要去适配的). 比如有下面 ...
- [转帖]Windows批处理(cmd/bat)常用命令小结
Windows批处理(cmd/bat)常用命令小结 非常值得学习的文档 先放这里 有时间做实验, 转载自:“趣IT”微信公共号 前言 批处理文件(batch file)包含一系列 DOS命令,通常用于 ...
- [转帖]Linux下fork函数及pthread函数的总结
Linux下fork函数及pthread函数的总结 https://blog.csdn.net/wangdd_199326/article/details/76180514 fork Linux多进程 ...