http://acm.hdu.edu.cn/showproblem.php?

pid=1226

为了节省空间。您可以使用vis初始化数组初始化-1。

发现BFSeasy错了地方 始一直WA在这里:就是我int tp=q.front();之后立即q.pop();了,然后才去推断是不是符合条件以break,这样就不能依据q.empty()==1觉得没有找到ans 由于这里WA了

事实上也能够vis[0] == -1来推断

比較不理解的是 当n==0的时候 %n==0的时候怎么处理

//#pragma comment(linker, "/STACK:102400000,102400000")
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <map>
#include <set>
#include <queue>
using namespace std; #define ls(rt) rt*2
#define rs(rt) rt*2+1
#define ll long long
#define ull unsigned long long
#define rep(i,s,e) for(int i=s;i<e;i++)
#define repe(i,s,e) for(int i=s;i<=e;i++)
#define CL(a,b) memset(a,b,sizeof(a))
#define IN(s) freopen(s,"r",stdin)
#define OUT(s) freopen(s,"w",stdout)
const ll ll_INF = ((ull)(-1))>>1;
const double EPS = 1e-8;
const double pi = acos(-1.0);
const int INF = 100000000; const int MAXN = 5000+100;
int n,c,m;
int a[MAXN],vis[MAXN],pre[MAXN];//pre记录路径
int ans[MAXN]; queue<int>q; int num; void dfs(int u)
{
ans[num++]=vis[u];
if(pre[u] == -1)return;
dfs(pre[u]);
} void print()
{
num--;
while(!ans[num])num--;
for(int i=num;i>=0;i--)
printf("%c",ans[i]+ (ans[i]>9?'A'-10:'0') );
putchar('\n');
} void solve()
{
CL(vis,0xff);
CL(pre,0xff);
if(!n && !a[0])
{
puts("0");
return;
}
if(!n && a[0])//
{
puts("give me the bomb please");
return;
}
while(!q.empty())q.pop();
for(int i=0;i<m;i++)
if(a[i])
{
int tmp=a[i]%n;
if(vis[tmp]==-1)
{
vis[tmp]=a[i];//记录tmp由a[i]得来
pre[tmp]=-1; //
q.push(tmp);
}
}
int tp=0,flag=0;////
while(!q.empty())
{
tp=q.front();q.pop();
if(!tp){flag=1;break;}
for(int i=0;i<m;i++)
{
int tmp=(tp*c+a[i])%n;
if(vis[tmp] == -1)
{
vis[tmp]=a[i];
pre[tmp]=tp;
q.push(tmp);
}
}
}
if(!flag)
{
puts("give me the bomb please");
return;
}
num=0;
dfs(0);
if(num>500)
{
puts("give me the bomb please");
return;
}
print();
} int main()
{
//IN("hdu1226.txt");
int ncase;
scanf("%d",&ncase);
while(ncase--)
{
scanf("%d%d%d",&n,&c,&m);
char op[5];
for(int i=0;i<m;i++)
{
scanf("%s",op);
if(op[0] >='A' && op[0] <='F') a[i]=op[0]-'A'+10;
else a[i]=op[0]-'0';
}
sort(a,a+m);
solve();
}
return 0;
}

版权声明:本文博主原创文章。博客,未经同意不得转载。

hdu 1226 BFS + bfs记录路径的更多相关文章

  1. HDOJ-1043 Eight(八数码问题+双向bfs+高效记录路径+康拓展开)

    bfs搜索加记录路径 HDOJ-1043 主要思路就是使用双向广度优先搜索,找最短路径.然后记录路径,找到结果是打印出来. 使用康拓序列来来实现状态的映射. 打印路径推荐使用vector最后需要使用a ...

  2. HDU 1026 BSF+优先队列+记录路径、

    #include<iostream> #include<cmath> #include<cstring> #include<cstdio> #inclu ...

  3. Q - 迷宫问题 POJ - 3984(BFS / DFS + 记录路径)

    Q - 迷宫问题 POJ - 3984 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, ...

  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. HDU1026--Ignatius and the Princess I(BFS记录路径)

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

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

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

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

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

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

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

随机推荐

  1. HTML5 Canvas 填充与描边(Fill And Stroke)

    HTML5 Canvas 填充与描边(Fill And Stroke) 演示HTML5 Canvas Fill 与Stroke文字效果,基于Canvas如何实 现纹理填充与描边. 一:颜色填充与描边 ...

  2. WebView混合开发

    现在开发APP的方式变化,不在是传统的APP开发了,有很多的APP慢慢的转向混合模式的开发,使用WebView是传统开发模式转向混合模式的桥梁工具,结合了很多的Web前端开发界面,使得开发的速度加快, ...

  3. spring web.xml配置服务启动后执行文件

    <bean id="readXmlService" class="com.xxx.xxx.readXmlServiceImpl" init-method= ...

  4. .Net 文本框实现内容提示(仿Google、Baidu)

    原文:.Net 文本框实现内容提示(仿Google.Baidu) 1.Demo下载: 文本框实现内容提示(仿Google.Baidu).rar 2.创建数据库.表(我用的sqlserver2008数据 ...

  5. 开源一个适用iOS的数据库表结构更新机制的代码

    将前段时间开源的代码.公布一下: ARDBConfig On the iOS, provide a database table structure update mechanism, ensure ...

  6. Java 小样例:图书馆课程设计(Java 8 版)

    用 Java 模拟一个图书馆.包含创建图书.创建读者.借书.还书.列出全部图书.列出全部读者.列出已借出的图书.列出过期未还的图书等功能. 每一个读者最多仅仅能借 3 本书,每一个书最多仅仅能借 3 ...

  7. HDU 5074-Hatsune Miku(DP)

    Hatsune Miku Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) T ...

  8. python学习1(小白记录)

    python创建cocos2d-x项目注意点1. 2.7.5版本号的.配置好环境变量之后.要切换到tools文件夹下.直接运行 python create_project.py ..........这 ...

  9. 安卓SDK更新host文件地址

    之前在安装jdk时,安装进度一直很缓慢,在更新这个(两个任选,都有效)host文件地址后,瞬间就可以了. 203.208.46.146 dl.google.com 203.208.46.146 dl- ...

  10. Base64实现android端图片上传到server端

    首先要下载Base64.java文件http://iharder.sourceforge.net/current/java/base64/ 将代码复制到project中. 然后上代码: android ...