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,问两条路线是否会有交点,若有则输出交点个数,否出输 ...
随机推荐
- C++类型强制转换<转>
转载:http://www.cnblogs.com/goodhacker/archive/2011/07/20/2111996.html C风格的强制类型转换(Type Cast)很简单,不管什么类型 ...
- 标注偏置问题(Label Bias Problem)和HMM、MEMM、CRF模型比较<转>
转自http://blog.csdn.net/lskyne/article/details/8669301 路径1-1-1-1的概率:0.4*0.45*0.5=0.09 路径2-2-2-2的概率:0. ...
- python 判断路径是否存在
import os os.path.exists(文件绝对路径)
- python常用模块之requests
一.requests 1.GET url带参数请求 >>> payload = {'key1': 'value1', 'key2': 'value2'} >>> ...
- BCB:内存泄漏检查工具CodeGuard
一.为什么写这篇东西 自己在使用BCB5写一些程序时需要检查很多东西,例如内存泄漏.资源是否有释放等等,在使用了很多工具后,发觉BCB5本身自带的工具―CodeGuard,非常不错,使用也挺方便的,但 ...
- struts1标签库
Struts提供了五个标签库,即:HTML.Bean.Logic.Template和Nested. HTML标签 : 用来创建能够和Struts 框架和其他相应的HTML 标签交互的HTML 输入表单 ...
- ios 序列化
1到底这个序列化有啥作用? 面向对象的程序在运行的时候会创建一个复杂的对象图,经常要以二进制的方法序列化这个对象图,这个过程叫做Archiving. 二进制流可以通过网络或写入文件中(来源于某教材的一 ...
- Mac屏幕亮度保存
关于保存屏幕亮度的方法,论坛上已有几种,搜索 NVRAM 会出来很多教程,在此不再详述,可以参考帖子http://www.idelta.info/archives/nvram_on_hackintos ...
- window nodejs 版本管理器 nvm-windows 教程
先去https://github.com/coreybutler/nvm-windows/releases 下载nvm-setup.zip 安装 安装的过程中会提示是否获取nodejs的管理权限,点确 ...
- 常用JavaScript正则表达式整理
在表单验证中,正则表达式书写起来特别繁琐,本文整理了15个常用的JavaScript正则表达式,其中包括用户名.密码强度.整数.数字.电子邮件地址(Email).手机号码.身份证号.URL地址. IP ...