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, ...
随机推荐
- 依据二度人脉推荐好友sql
friend表结构 DROP TABLE IF EXISTS FRIEND; create table friend( uid bigint not null comment ' ...
- C#-gdi画图,双缓冲画图,Paint事件的触发---ShinePans
在使用gdi技术画图时,有时会发现图形线条不够流畅,或者在改变窗口大小时会闪烁不断的现象.(Use DoubleBuffer to solve it!) ...
- POJ 1184 聪明的打字员
简直难到没朋友. 双向bfs + 剪枝. 剪枝策略: 对于2--5位置上的数,仅仅有当光标在相应位置时通过swap ,up.down来改变.那么当当前位置没有达到目标状态时,left和right无意义 ...
- Java重写方法与初始化的隐患(转)
原文出处: Shawon 虽然文章标题是Java, 但几乎所有面向对象设计的语言都遵守这个初始化流程, 感谢廖祜秋liaohuqiu_秋百万指出, 之前忘记提这个了. 前言 drakeet写了个和Re ...
- Python的包管理
0.Python的包管理 在刚开始学习Python的时候比较头疼各种包的管理,后来搜到一些Python的包管理工具,比如setuptools, easy_install, pip, distribut ...
- aStar算法求第k短路
A*的概念主意在于估计函数,f(n)=g(n)+h(n),f(n)是估计函数,g(n)是n节点的当前代价,h(n)是n节点的估计代价:而实际中,存在最优的估计函数f'(n)=g'(n)+h'(n),那 ...
- ImageButton消除使用setImageDrawable造成的边框问题。
使用ImageButton的时候,如果使用setBackgroundDrawable设置图片,会对可点击范围有影响,使用setImageDrawable方法设置图片,图片小的话,可以会有,这种有白色边 ...
- SSO(Single Sign On)系列(一)--SSO简单介绍
任何类型的站点,到达一定规模之后一定会存在这种问题:比方我们有N个系统.传统方式下我们就须要有N对不同的username和password,本来这些系统的开发都能为我们带来良好的效益,用户在用的时候并 ...
- JavaScript语言基础知识10
JavaScript中间if声明: <span style="font-size:18px;"><HTML> <HEAD> <TITLE& ...
- Oracle数据库的锁类型
Oracle数据库的锁类型 博客分类: oracle Oracle数据库的锁类型 根据保护的对象不同,Oracle数据库锁可以分为以下几大类:DML锁(data locks,数据锁),用于保护 ...