poj1426 - Find The Multiple [bfs 记录路径]
转:http://blog.csdn.net/wangjian8006/article/details/7460523
(比较好的记录路径方案)
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<string> #define N 1000005
#define M 9
#define mod 1000000007
//#define p 10000007
#define mod2 1000000000
#define ll long long
#define LL long long
#define eps 1e-6
#define inf 1000000
#define maxi(a,b) (a)>(b)? (a) : (b)
#define mini(a,b) (a)<(b)? (a) : (b) using namespace std; int n;
typedef struct
{
int v;
int yu;
int num;
int pre;
}PP; int tot;
int vis[];
PP ans[N]; void ini()
{
tot=;
memset(vis,,sizeof(vis));
} void out(int now)
{
// for(int i=0;i<=1;i++)
//printf(" v=%d pre=%d\n",ans[i].v,ans[i].pre);
//printf(" now=%d v=%d pre=%d\n",now,ans[now].v,ans[now].pre);
if(ans[now].pre!=-){ out(ans[now].pre); }
printf("%d",ans[now].v);
} void bfs()
{
queue<PP>q;
PP te,nt;
te.v=;
te.pre=-;
te.num=;
te.yu=%n;
ans[].v=;
ans[].pre=-;
tot=;
vis[]=;
q.push(te); while(q.size()>=)
{ te=q.front();
q.pop();
// printf(" v=%d yu=%d num=%d pre=%d\n",te.v,te.yu,te.num,ans[te.num].pre);
if(te.yu==){
//printf(" num=%d\n",te.num);
out(te.num);
printf("\n");
return;
}
nt.pre=te.num;
nt.yu=(te.yu*)%n;
if(vis[nt.yu]==){
vis[nt.yu]=;
nt.num=tot;
ans[tot].v=;
ans[tot].pre=te.num;
tot++; nt.v=;
q.push(nt);
}
nt.yu=(te.yu*+)%n;
if(vis[nt.yu]==){
vis[nt.yu]=;
nt.num=tot;
ans[tot].v=;
ans[tot].pre=te.num;
tot++;
nt.v=;
q.push(nt);
}
}
} void solve()
{
bfs();
} int main()
{
//freopen("data.in","r",stdin);
// freopen("data.out","w",stdout);
//scanf("%d",&T);
//for(int ccnt=1;ccnt<=T;ccnt++)
//while(T--)
while(scanf("%d",&n)!=EOF)
{
if(n==) break;
ini();
solve();
// out();
} return ;
}
poj1426 - Find The Multiple [bfs 记录路径]的更多相关文章
- POJ.3894 迷宫问题 (BFS+记录路径)
POJ.3894 迷宫问题 (BFS+记录路径) 题意分析 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, ...
- Codeforces-A. Shortest path of the king(简单bfs记录路径)
A. Shortest path of the king time limit per test 1 second memory limit per test 64 megabytes input s ...
- HDU1026--Ignatius and the Princess I(BFS记录路径)
Problem Description The Princess has been abducted by the BEelzebub feng5166, our hero Ignatius has ...
- hdu 1026 Ignatius and the Princess I (bfs+记录路径)(priority_queue)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1026 Problem Description The Princess has been abducted ...
- hdu 1026 Ignatius and the Princess I(优先队列+bfs+记录路径)
以前写的题了,现在想整理一下,就挂出来了. 题意比较明确,给一张n*m的地图,从左上角(0, 0)走到右下角(n-1, m-1). 'X'为墙,'.'为路,数字为怪物.墙不能走,路花1s经过,怪物需要 ...
- (简单) POJ 3414 Pots,BFS+记录路径。
Description You are given two pots, having the volume of A and B liters respectively. The following ...
- 迷宫问题(bfs+记录路径)
题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=105278#problem/K K - 迷宫问题 Time Limit:1000 ...
- poj 3414 Pots 【BFS+记录路径 】
//yy:昨天看着这题突然有点懵,不知道怎么记录路径,然后交给房教了,,,然后默默去写另一个bfs,想清楚思路后花了半小时写了120+行的代码然后出现奇葩的CE,看完FAQ改了之后又WA了.然后第一次 ...
- sdut oj 3058 路线冲突问题(BFS+记录路径算法,回溯路径 )
路线冲突问题 题目描述 给出一张地图,地图上有n个点,任意两点之间有且仅有一条路.点的编号从1到n. 现在兵团A要从s1到e1,兵团B要从s2到e2,问两条路线是否会有交点,若有则输出交点个数,否出输 ...
随机推荐
- Java数据结构面试题
1.栈和队列的共同特点是(只允许在端点处插入和删除元素) 4.栈通常采用的两种存储结构是(线性存储结构和链表存储结构) 5.下列关于栈的叙述正确的是(D) A.栈是非线性结构B.栈是一种树状 ...
- 毛毛虫组【Beta】Scrum Meeting 3
第三天 日期:2019/6/25 前言 第三次会议: 时间:6月25日 地点:教10-A511 内容:此次会议主要是对项目验收做准备工作. 1.1 今日完成任务情况以及遇到的问题. 今日完成任务情况: ...
- 响应式Web设计- Viewport
什么是Viewport? viewport是用户网页的可视区域, 翻译为中文可以叫做"视区". 设置Viewport 一个常用的针对移动网页优化过的页面的Viewport meta ...
- NSLayoutConstraint.constraintsWithVisualFormat详解,以及AlignAllCenterY
NSLayoutConstraint.constraintsWithVisualFormat详解,以及AlignAllCenterY 转载2015-07-08 18:02:02 鉴于苹果官方文档的解释 ...
- bash编程的信号捕获:
bash编程的信号捕获: kill -l KILL无法捕捉: trap 'COMMAND' SIGNAL, 信号捕捉用于:在中途中止时做一些清理操作. 一. trap捕捉到信号之后,可以 ...
- Java的BigDecimal容易出现的坑
BigDecimal一般是用来做要求比较高的精准计算的.前几天在使用的时候遇到一个大坑,记录下. 这个问题产生是使用BigDecimal做除法(divide)运算,这个类的divide方法存在三个常用 ...
- MySQL 查询优化之 Multi-Range Read
MySQL 查询优化之 Multi-Range Read MRR的工作原理 MRR开启与关闭 使用MRR示例 参考文档 在存储引擎中未缓存的大表,使用辅助索引的range scan检索数据, 可能会导 ...
- 本地开发环境中部署已经写好的magento2.0项目
环境:apache2.4.25+php7.0.16+mysql5.7 (注意版本搭配,详细可以看magento2.0官网看配置) apache最好使用80端口,host文件配置本地虚拟域名 php.i ...
- js中小数精度问题
js中小数的取值为近似值,可能比实际值大,也可能比实际值小,进行“四舍五入”得到的 例如:alert(0.1+0.2);值为0.300000004 alert(0.2+0.7);值为1.899 ...
- shell-code-3-echo用法&printf用法
××××××××××××××××××××××××××××××下面是echo××××××××××××××××××××××××××××××× # read 命令从标准输入(即执行时,键盘的输入)中读取一行 ...