传送门

转: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 记录路径]的更多相关文章

  1. POJ.3894 迷宫问题 (BFS+记录路径)

    POJ.3894 迷宫问题 (BFS+记录路径) 题意分析 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, ...

  2. 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 ...

  3. HDU1026--Ignatius and the Princess I(BFS记录路径)

    Problem Description The Princess has been abducted by the BEelzebub feng5166, our hero Ignatius has ...

  4. 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 ...

  5. hdu 1026 Ignatius and the Princess I(优先队列+bfs+记录路径)

    以前写的题了,现在想整理一下,就挂出来了. 题意比较明确,给一张n*m的地图,从左上角(0, 0)走到右下角(n-1, m-1). 'X'为墙,'.'为路,数字为怪物.墙不能走,路花1s经过,怪物需要 ...

  6. (简单) POJ 3414 Pots,BFS+记录路径。

    Description You are given two pots, having the volume of A and B liters respectively. The following ...

  7. 迷宫问题(bfs+记录路径)

    题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=105278#problem/K K - 迷宫问题 Time Limit:1000 ...

  8. poj 3414 Pots 【BFS+记录路径 】

    //yy:昨天看着这题突然有点懵,不知道怎么记录路径,然后交给房教了,,,然后默默去写另一个bfs,想清楚思路后花了半小时写了120+行的代码然后出现奇葩的CE,看完FAQ改了之后又WA了.然后第一次 ...

  9. sdut oj 3058 路线冲突问题(BFS+记录路径算法,回溯路径 )

    路线冲突问题 题目描述 给出一张地图,地图上有n个点,任意两点之间有且仅有一条路.点的编号从1到n. 现在兵团A要从s1到e1,兵团B要从s2到e2,问两条路线是否会有交点,若有则输出交点个数,否出输 ...

随机推荐

  1. 前端知识点总结——HTML

    HTML:HTML4.01 指的就是网页技术HTML5:HTML4.01的升级版本 1.web的基础知识 web与Internet1.Internet:全球性的计算机互联网络,因特网,互联网,交互网2 ...

  2. Javascript根据指定下标或对象删除数组元素

    删除数组元素在工作中经常会用到,本文讲解一下Javascript根据下标删除数组元素的方法,需要了解的朋友可以参考下 将一下代码放在全局js文件中: Js代码 /** *删除数组指定下标或指定对象 * ...

  3. kitti raw data development kit的使用

    run_demoVelodyne.m使用:http://blog.csdn.net/qq_33801763/article/details/78959205   https://www.cnblogs ...

  4. java POI技术之导出数据优化(15万条数据1分多钟)

    专针对导出excel2007 ,用到poi3.9的jar package com.cares.ynt.util; import java.io.File; import java.io.FileOut ...

  5. CPP-基础:cout

    C++编程语言互换流中的标准输出流,需要iostream.h支持.读为 "c out". 使用范例 //用户输入的数字由cin保存于变量a中,并通过cout输出. #include ...

  6. POI转换word doc文件为(html,xml,txt)

    在POI中还存在有针对于word doc文件进行格式转换的功能.我们可以将word的内容转换为对应的Html文件,也可以把它转换为底层用来描述doc文档的xml文件,还可以把它转换为底层用来描述doc ...

  7. shell脚本,当用sed删除某一文件里面的内容时,并追加到同一个文件会出现问题。

    shell脚本,当用sed删除某一文件里面的内容时,并追加到同一个文件会出现问题.因为初始文件和写入文件是一个文件这是失败的.需要追加到另一个文件,然后再用mv进行操作.[root@localhost ...

  8. Android Studio问题记录

    1>Android Studio中module是什么,? 答:Android Studio是基于intellij,跟eclipse不太一样.对应关系如下: intellij的project -- ...

  9. shell 管道导致的变量重置问题

    测试脚本: #!/bin/sh flag= func() { flag= } main() { func | echo "flag=$flag" } 输出显示的flag=0! 参考 ...

  10. 【转发】【composer】composer 命令行介绍

    首页 入门 下载 安装包列表 中国镜像 命令行 你已经学会了如何使用命令行界面做一些事情.本章将向你介绍所有可用的命令. 为了从命令行获得帮助信息,请运行 composer 或者 composer l ...