hdu 1226 BFS + bfs记录路径
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记录路径的更多相关文章
- HDOJ-1043 Eight(八数码问题+双向bfs+高效记录路径+康拓展开)
bfs搜索加记录路径 HDOJ-1043 主要思路就是使用双向广度优先搜索,找最短路径.然后记录路径,找到结果是打印出来. 使用康拓序列来来实现状态的映射. 打印路径推荐使用vector最后需要使用a ...
- HDU 1026 BSF+优先队列+记录路径、
#include<iostream> #include<cmath> #include<cstring> #include<cstdio> #inclu ...
- 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, ...
- 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经过,怪物需要 ...
- HDU1026--Ignatius and the Princess I(BFS记录路径)
Problem Description The Princess has been abducted by the BEelzebub feng5166, our hero Ignatius has ...
- (简单) 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.3894 迷宫问题 (BFS+记录路径)
POJ.3894 迷宫问题 (BFS+记录路径) 题意分析 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, ...
随机推荐
- CC++刚開始学习的人编程教程(9) Windows8.1安装VS2013并捆绑QT与编程助手
我们在Windows8.1安装VS2013并捆绑QT与编程助手须要下列文件. 2. 在虚拟机中开启Windows8.1 3.然后选择VS2013的安装镜像. 4.将镜像拷贝到虚拟机. 5.我们装载这个 ...
- 【Gapps】安装GooglePlay引发一系列问题
再次感谢小海的支持,感谢大家的支持! 从安装CM至如今GooglePlay,小海为我提供了非常多方案,能够说是全面支持.仅仅是出于隐私不便公开他的个人信息,仅提供一个他的博客地址http://luha ...
- asp.net mvc4中自定义404页面
原文地址:http://www.chuchur.com/asp-net-mvc4-404/ 定义404 方法当然有很多种.不同的方法所展现的形式也不一样,用户所体验也不一样.以下提供2两种 方法一: ...
- TreeSet排序
TreeSet的排序能够通过两种方法来实现: 1.通过TreeSet(Comparator<? super E> comparator) 构造方法指定TreeSet的比較器进行排序. 2. ...
- Verifying Checksum ... Bad Data CRC 错误解决
1.问题描述:使用SAM9X25 内核版本是2.6.39 在启动内核时会出现Verifying Checksum ... Bad Data CRC 错误 2.解决办法: 查看原先uboot参数: ...
- 理解Javascript的动态语言特性
原文:理解Javascript的动态语言特性 理解Javascript的动态语言特性 Javascript是一种解释性语言,而并非编译性,它不能编译成二进制文件. 理解动态执行与闭包的概念 动态执行: ...
- 恢复gvim的ctl+v可视模式设置
set nocompatiblesource $VIMRUNTIME/vimrc_example.vim"source $VIMRUNTIME/mswin.vim (注释此行)behave ...
- 使用SVNkit删除版本库的文件
源网址:http://wiki.svnkit.com/Committing_To_A_Repository Editing Operation: commiting to a repository T ...
- selenium 远程调用浏览器
共分三步: 1.selenium官网下载selenium-server-standalone.jar的最新版本号 2.启动selenium-server::::: java -jar "se ...
- QVector<QString> 显示器里面的动态数组元素QString和char *转变
QVector类是一类提供了动态数组模板. QVector<T>是Qt普通容器类的一种. 它将自己的每个对象存储在连续的内存中.能够使用索引號来高速訪问它们.QList<T>. ...