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. 【Swift】—— 中国课程

    Apple宣布更换oc武器语言 Swift ,在牛叉的中国开发者社区9几天之内,以完成.这是福音许多中国的开发商啊,兴奋的发现之后,写存储在日志,马上防止忘记: GitHub开源项目:<The ...

  2. PHP 报告分拣和生产理念

    原则排序报告 见一宝.一只猫的排序,我想照猫画虎,鼓捣自己一个. watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd3VqaWFuZ3dlaTU2Nw==/f ...

  3. hdu1513(最长公共子序列)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1513 题意:将一个字符串转变为回文串的最少添加字符个数 分析:只要想到将字符串逆序后与原字符串求最长公 ...

  4. qt安装遇到的错误

    /usr/bin/ld: cannot find -lXrender collect2: ld returned 1 exit status make[1]: *** [../../../../lib ...

  5. AMDU恢复ASM磁盘组数据(測)

    --umount ASMCMD> umoung -a asmdg commands:         md_backup, md_restor         lsattr, setattr   ...

  6. viewpager与子view的事件冲突解决

    问题: 对android的事件机制一直不怎么了解,最近android项目中运用viewpager+listview (就是viewpager的子view中嵌套了listview),出现了触摸手势冲突 ...

  7. html网页特殊符号代码

    HTML特殊字符编码大全:往网页中输入特殊字符,需在html代码中加入以&开头的字母组合或以&#开头的数字.下面就是以字母或数字表示的特殊符号大全.                   ...

  8. coco2d-x 基于视口的地图设计

    <pre name="code" class="plain"> 基于视口的地图设计 DionysosLai 2014-06-14 第三人称游戏,玩家 ...

  9. RedGate 工具SQLTEST 1.0.15.1

    原文:RedGate 工具SQLTEST 1.0.15.1 RedGate 工具SQLTEST 1.0.15.1 SQL TEST1.0.15.1的破解可以参考这篇文章:http://www.cnbl ...

  10. HSQL

    Whenever I connect to HSQLDB from my application deployed on eclipse Juno, it throws an exception as ...