POJ3278&&1426&&3126&&3087&&3414
接上次的,标签是BFS专题
其实无论是Deepth还是Breadth都是Search
3278
1426
找一个十进制数(我刚开始看样例以为是二进制数),使得它每一位上都是1或0,且是n的倍数(不包括0)
BFS表搜之后就打表即可
BFS CODE
#include<cstdio>
#include<cstring>
using namespace std;
const int MAX_SIZE=105;
int n,len[MAX_SIZE<<1];
bool q[1048580][MAX_SIZE];
inline char tc(void)
{
static char fl[100000],*A=fl,*B=fl;
return A==B&&(B=(A=fl)+fread(fl,1,100000,stdin),A==B)?EOF:*A++;
}
inline void read(int &x)
{
x=0; char ch=tc();
while (ch<'0'||ch>'9') ch=tc();
while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=tc();
}
inline bool check(bool *bl,int len,int k)
{
int res=0;
for (register int i=1;i<=len;++i)
res=(res*10+bl[i])%k;
return !res;
}
inline void print(bool *bl,int len)
{
for (register int i=1;i<=len;++i)
putchar(bl[i]+'0');
putchar('\n');
}
inline void BFS(void)
{
q[1][1]=1; len[1]=1;
int head=0,tail=1;
bool bl[MAX_SIZE];
while (head<tail)
{
memcpy(bl,q[++head],sizeof(bl));
if (check(bl,len[head],n)) { print(bl,len[head]); return; }
memcpy(q[++tail],bl,sizeof(bl)); len[tail]=len[head]+1; q[tail][len[tail]]=0;
memcpy(q[++tail],bl,sizeof(bl)); len[tail]=len[head]+1; q[tail][len[tail]]=1;
}
}
int main()
{
//freopen("CODE.in","r",stdin); freopen("CODE.out","w",stdout);
read(n);
while (n)
{
BFS();
read(n);
}
return 0;
}
打表 CODE
#include<string>
#include<iostream>
using namespace std;
const string ans[201]=
{
"",
"1",
"10",
"111",
"100",
"10",
"1110",
"1001",
"1000",
"111111111",
"10",
"11",
"11100",
"1001",
"10010",
"1110",
"10000",
"11101",
"1111111110",
"11001",
"100",
"10101",
"110",
"110101",
"111000",
"100",
"10010",
"1101111111",
"100100",
"1101101",
"1110",
"111011",
"100000",
"111111",
"111010",
"10010",
"11111111100",
"111",
"110010",
"10101",
"1000",
"11111",
"101010",
"1101101",
"1100",
"1111111110",
"1101010",
"10011",
"1110000",
"1100001",
"100",
"100011",
"100100",
"100011",
"11011111110",
"110",
"1001000",
"11001",
"11011010",
"11011111",
"11100",
"100101",
"1110110",
"1111011111",
"1000000",
"10010",
"1111110",
"1101011",
"1110100",
"10000101",
"10010",
"10011",
"111111111000",
"10001",
"1110",
"11100",
"1100100",
"1001",
"101010",
"10010011",
"10000",
"1111111101",
"111110",
"101011",
"1010100",
"111010",
"11011010",
"11010111",
"11000",
"11010101",
"1111111110",
"1001",
"11010100",
"10000011",
"100110",
"110010",
"11100000",
"11100001",
"11000010",
"111111111111111111",
"100",
"101",
"1000110",
"11100001",
"1001000",
"101010",
"1000110",
"100010011",
"110111111100",
"1001010111",
"110",
"111",
"10010000",
"1011011",
"110010",
"1101010",
"110110100",
"10101111111",
"110111110",
"100111011",
"111000",
"11011",
"1001010",
"10001100111",
"11101100",
"1000",
"11110111110",
"11010011",
"10000000",
"100100001",
"10010",
"101001",
"11111100",
"11101111",
"11010110",
"11011111110",
"11101000",
"10001",
"100001010",
"110110101",
"100100",
"10011",
"100110",
"1001",
"1111111110000",
"11011010",
"100010",
"1100001",
"11100",
"110111",
"11100",
"1110001",
"11001000",
"10111110111",
"10010",
"1110110",
"1010100",
"10101101011",
"100100110",
"100011",
"100000",
"11101111",
"11111111010",
"1010111",
"1111100",
"1111110",
"1010110",
"11111011",
"10101000",
"10111101",
"111010",
"1111011111",
"110110100",
"1011001101",
"110101110",
"100100",
"110000",
"100101111",
"110101010",
"11010111",
"11111111100",
"1001111",
"10010",
"100101",
"110101000",
"1110",
"100000110",
"1001011",
"1001100",
"1010111010111",
"110010",
"11101111",
"111000000",
"11001",
"111000010",
"101010",
"110000100",
"1101000101",
"1111111111111111110",
"111000011",
"1000"
};
int n;
int main()
{
std::ios::sync_with_stdio(false);
cin>>n;
while (n)
{
cout<<ans[n]<<endl;
cin>>n;
}
return 0;
}
3126
给出一个四位数,你每次可以替换其中一位上的数字,但是必须满足操作后的数是质数
埃氏筛(不会欧拉筛)之后BFS即可
CODE
#include<cstdio>
#include<cstring>
using namespace std;
const int N=10000;
bool prime[N],vis[N];
int n,a,b,q[N],step[N];
inline char tc(void)
{
static char fl[100000],*A=fl,*B=fl;
return A==B&&(B=(A=fl)+fread(fl,1,100000,stdin),A==B)?EOF:*A++;
}
inline void read(int &x)
{
x=0; char ch=tc();
while (ch<'0'||ch>'9') ch=tc();
while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=tc();
}
inline void write(int x)
{
if (x/10) write(x/10);
putchar(x%10+'0');
}
inline void get_prime(void)
{
for (register int i=2;i<N;++i)
if (prime[i]) for (register int j=i<<1;j<N;j+=i) prime[j]=0;
}
inline int BFS(int a,int b)
{
q[1]=a; step[a]=0; vis[a]=1;
int head=0,tail=1;
while (head<tail)
{
int now=q[++head];
if (now==b) return step[head];
for (register int radix=1;radix<=1000;radix*=10)
for (register int i=0;i<=9;++i)
{
int t=((now/radix/10)*10+i)*radix+(now%radix);
if (t>=1000&&t<=9999&&!vis[t]&&prime[t]) q[++tail]=t,vis[t]=1,step[tail]=step[head]+1;
}
}
return -1;
}
int main()
{
//freopen("CODE.in","r",stdin); freopen("CODE.out","w",stdout);
memset(prime,true,sizeof(prime));
get_prime();
read(n);
while (n--)
{
read(a); read(b);
memset(vis,0,sizeof(vis));
int k=BFS(a,b);
if (k!=-1) write(k),putchar('\n'); else puts("Impossible");
}
return 0;
}
3087
这是模拟题吧
给你两个长度为c的字符串s1,s2,你每次可以进行洗牌,问几次操作后能使洗过的牌达到目标状态
要注意到,如果已经出现重复了,但是还没有到目标状态,那么就impossible了
map即可
CODE
#include<iostream>
#include<string>
#include<map>
using namespace std;
map <string,bool> hash;
string s1,s2,s,temp;
int n,c;
int main()
{
register int i,j;
std::ios::sync_with_stdio(false);
for (cin>>n,i=1;i<=n;++i)
{
hash.clear();
cin>>c; cin>>s1>>s2>>s;
for (int cnt=1;;++cnt)
{
temp="";
for (j=0;j<c;++j)
temp+=s2[j],temp+=s1[j];
if (temp==s) { cout<<i<<' '<<cnt<<endl; break; }
if (hash[temp]) { cout<<i<<' '<<-1<<endl; break; }
hash[temp]=1; s1.assign(temp,0,c); s2.assign(temp,c,c);
}
}
return 0;
}
3414
给出两个水壶,最大容量为a,b。有以下操作:
- FILL(i) 加满i
- DROP(i) 把i里的水道光
- POUR(i,j) 把i水壶的水全部倒入j水壶中
因为就两个水壶,BFS即可
CODE
#include<iostream>
#include<string>
using namespace std;
const int N=105;
const string F1="FILL(1)",F2="FILL(2)",D1="DROP(1)",D2="DROP(2)",P1="POUR(1,2)",P2="POUR(2,1)";
bool vis[N*N+N];
int a,b,c,q[N*N][2],step[N*N],pre[N*N],rear;
string work[N*N];
inline int hash(int x,int y)
{
return x*N+y;
}
inline int BFS(void)
{
q[1][0]=q[1][1]=0; vis[0]=1; step[1]=0;
int head=0,tail=1,h,x,y,xx,yy;
while (head<tail)
{
x=q[++head][0],y=q[head][1];
if (x==c||y==c) { rear=head; return step[head]; }
h=hash(a,y);
if (!vis[h]) q[++tail][0]=a,q[tail][1]=y,step[tail]=step[head]+1,vis[h]=1,work[tail]=F1,pre[tail]=head;
h=hash(x,b);
if (!vis[h]) q[++tail][0]=x,q[tail][1]=b,step[tail]=step[head]+1,vis[h]=1,work[tail]=F2,pre[tail]=head;
h=hash(0,y);
if (!vis[h]) q[++tail][0]=0,q[tail][1]=y,step[tail]=step[head]+1,vis[h]=1,work[tail]=D1,pre[tail]=head;
h=hash(x,0);
if (!vis[h]) q[++tail][0]=x,q[tail][1]=0,step[tail]=step[head]+1,vis[h]=1,work[tail]=D2,pre[tail]=head;
yy=x+y>b?b:x+y,xx=x+y>b?x+y-b:0;
h=hash(xx,yy);
if (!vis[h]) q[++tail][0]=xx,q[tail][1]=yy,step[tail]=step[head]+1,vis[h]=1,work[tail]=P1,pre[tail]=head;
xx=x+y>a?a:x+y,yy=x+y>a?x+y-a:0;
h=hash(xx,yy);
if (!vis[h]) q[++tail][0]=xx,q[tail][1]=yy,step[tail]=step[head]+1,vis[h]=1,work[tail]=P2,pre[tail]=head;
}
return -1;
}
inline void print(int now)
{
if (pre[now]!=1) print(pre[now]);
cout<<work[now]<<endl;
}
int main()
{
std::ios::sync_with_stdio(false);
cin>>a>>b>>c;
int k=BFS();
if (k!=-1) cout<<k<<endl,print(rear); else cout<<"impossible";
return 0;
}
POJ3278&&1426&&3126&&3087&&3414的更多相关文章
- [ACM训练] 算法初级 之 搜索算法 之 广度优先算法BFS (POJ 3278+1426+3126+3087+3414)
BFS算法与树的层次遍历很像,具有明显的层次性,一般都是使用队列来实现的!!! 常用步骤: 1.设置访问标记int visited[N],要覆盖所有的可能访问数据个数,这里设置成int而不是bool, ...
- 【算法入门】广度/宽度优先搜索(BFS)
广度/宽度优先搜索(BFS) [算法入门] 1.前言 广度优先搜索(也称宽度优先搜索,缩写BFS,以下采用广度来描述)是连通图的一种遍历策略.因为它的思想是从一个顶点V0开始,辐射状地优先遍历其周围较 ...
- 【转】POJ题目分类
初级:基本算法:枚举:1753 2965贪心:1328 2109 2586构造:3295模拟:1068 2632 1573 2993 2996 图:最短路径:1860 3259 1062 2253 1 ...
- 【模拟】POJ 3087
直达–>POJ 3087 Shuffle'm Up 题意:一开始没怎么看明白,注意现是从S2里拿牌放在最底下,再放S1,这样交叉放(我一开始以为是S1和S2随意哪个先放,分别模拟取最小),然后在 ...
- 【BFS】POJ 3414
直达 -> POJ 3414 Pots 相似题联动–>HDU 1495 非常可乐 题意:两个壶倒水,三种操作,两个桶其中一个满足等于C的最少操作,输出路径.注意a,b互倒的时候能不能倒满, ...
- Sublime Text 3 3126 注册码 + 下载地址
Sublime Text 3 3126 下载地址 Windows版本 64位:https://download.sublimetext.com/Sublime%20Text%20Build%2031 ...
- hihoCoder 1426 : What a Ridiculous Election(总统夶选)
hihoCoder #1426 : What a Ridiculous Election(总统夶选) 时间限制:1000ms 单点时限:1000ms 内存限制:256MB Description - ...
- POJ 3414 Pots
Pots Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status ...
- POJ 3087 Shuffle'm Up
Shuffle'm Up Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit ...
随机推荐
- IE浏览器“自定义安全级别”不能设置的原因
“自定义安全级别”和滑块都是灰色,不允许设置.可能的原因: 1.组策略里面设置了: 2.启用了IE增强的安全设置: 程序卸载->选择windows组件->取消增强的安全设置
- [Android] Linux下JNI简单实现过程
大概梳理了一下JNI的过程: start->先写好A.java文件,里面添加native方法B,调用库C.so->编译成.class文件->用javac生成.h文件,文件包含Java ...
- 机器学习实战(Machine Learning in Action)学习笔记————06.k-均值聚类算法(kMeans)学习笔记
机器学习实战(Machine Learning in Action)学习笔记————06.k-均值聚类算法(kMeans)学习笔记 关键字:k-均值.kMeans.聚类.非监督学习作者:米仓山下时间: ...
- 将 HPC 作业从本地计算机提交到部署在 Azure 中的 HPC Pack 群集
Note Azure 具有用于创建和处理资源的两个不同的部署模型:Resource Manager 和经典. 这篇文章介绍了如何使用这两种模型,但 Azure 建议大多数最新部署使用 Resource ...
- Oracle EBS AR 收款调整取值
SELECT ct.trx_number ,adj.adjustment_number ,ad.amount_dr ,ad.amount_cr ,ad.source_table ,ad.source_ ...
- JBoss EAP应用服务器部署方法和JBoss 开发JMS消息服务小例子
一.download JBoss-EAP-6.2.0GA: http://jbossas.jboss.org/downloads JBoss Enterprise Application Platfo ...
- Linux parted命令详解
parted常见命令参数 Usage: parted [OPTION]... [DEVICE [COMMAND [PARAMETERS]...]...] Apply COMMANDs with PAR ...
- NOIP2018考前抱佛脚——图论基础复习
目录 存图方式 邻接矩阵存图 邻接表存图 链式前向星 最小生成树 例1 P1536 村村通 题目描述 输入输出格式 输入输出样例 标程 例2 P1546 最短网络 Agri-Net 题目背景 题目描述 ...
- Oracle 11g AWR 系列五:如何生成 AWR 报告?
1.生成单实例 AWR 报告: @$ORACLE_HOME/rdbms/admin/awrrpt.sql 2.生成 Oracle RAC AWR 报告: @$ORACLE_HOME/rdbms/adm ...
- BZOJ2893:征服王(费用流)
Description 虽然春希将信息传递给了雪菜,但是雪菜却好像完全不认得春希了.心急如焚的春希打开了第二世代机能,对雪菜的脑内芯片进行了直连-hack. 进入到雪菜内部的春希发现(这什么玩意..) ...