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. 【Unity 3D】学习笔记三十七:物理引擎——碰撞与休眠

    碰撞与休眠 上一篇笔记说过,当给予游戏对象刚体这个组件以后,那么这个组件将存在碰撞的可能性.一旦刚体開始运动,那么系统方法便会监视刚体的碰撞状态.一般刚体的碰撞分为三种:进入碰撞,碰撞中,和碰撞结束. ...

  2. (step 8.2.13)hdu 1524(A Chess Game)

    题目大意 : 在一个 有向无环图顶点上面有几个棋子, 2个人轮流操作, 每次操作就是找一个棋子往它能够移 动的地方移动一格, 不能操作的人输. 输入第一行 为一个 N , 表示有 N 个顶点 0 -& ...

  3. WPF案例(-)模拟Windows7 Win+Tab切换

    原文:WPF案例(-)模拟Windows7 Win+Tab切换 一个使用Wpf模拟Windows7 Win+Tab页面切换的小程序,使用快捷键Ctrl+Down或Ctrl+Up在示例程序各个页面元素之 ...

  4. hdu3001(状压dp)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3001 题意:n 个城市已经 m 条路 以及对应路费 c,要求遍历所有城市最少的路费,每个城市不能超过2 ...

  5. Notifications(通知)

    通知 通知是能在应用的普通用户界面外显示给用户的一种消息. 当你告诉系统公布一条通知时,它首先在通知栏中表现为一枚图标. 用户打开通知抽屉后就能查看通知的细节了. 通知栏和通知抽屉都是由系统控制的区域 ...

  6. 风起看云涌,叶落品人生 - Google 搜索

    风起看云涌,叶落品人生 - Google 搜索 风起看云涌,叶落品人生

  7. eclipse 安装vrapper vim插件

    http://vrapper.sourceforge.net/update-site/stable 如果安装不上,设置下代理./window/pereference/network* - manul

  8. WPF换肤之七:异步

    原文:WPF换肤之七:异步 在WinForm时代,相信大家都遇到过这种情形,如果在程序设计过程中遇到了耗时的操作,不使用异步会导致程序假死.当然,在WPF中,这种情况也是存在的,所以我们就需要寻找一种 ...

  9. OCP读书笔记(18) - 空间管理

    OLTP 表压缩 压缩始终是非常占用CPU的过程,并且需要花费一定时间,通常,如果压缩数据,则数据必须解压缩后才能使用.虽然此要求在数据仓库环境中是可以接受的但在OLTP环境中可能无法接受 现在,在O ...

  10. EJB通过ANT提高EJB应用程序的开发效率、无状态发展本地接口bean、开发状态bean

    该jboss集成到eclipse 关掉Jboss控制台新闻Ctrl+c,在MyEclipse→Servers→Jboss可配置JBoss. 通过ANT提高EJB应用的开发效率 在HelloWorld ...