poj 1465 (BFS+剪枝)
来源:互联网 发布:centos 安装apache2 编辑:程序博客网 时间:2024/04/28 12:13
解题思路:
剪枝用到了数论中的知识:
若a % n = r, 则( 10 * a + b ) % n = ( 10 * r + b ) % n,
又如果现在存在一个c, c > a且c % n = r, 则由于扩展的时候是相同的扩展, c就是个重复的状态, 且没有a更优。
人工模拟队列,起始为:head=0; tail=0; 队列还有数据的条件: head<tail (尾进头出)
#include <iostream>#include <cstring>#include <algorithm>using namespace std;const int SIZE=10000;typedef struct node{ int v; int r; int pre;}node;node q[SIZE];int vis[SIZE];int n, m;int a[15];int bfs(){ int head=0, tail=0; memset(vis,0 ,sizeof(vis)); //start for(int i=0;i<m;i++) { if(a[i]<=0) continue; q[tail].v=a[i]; q[tail].r=a[i]%n; q[tail++].pre=-1; vis[a[i]%n]=1; } //then while(head<tail) { node t=q[head]; for(int i=0;i<m;i++) { if(vis[(10*t.r+a[i])%n]) continue; q[tail].v=a[i]; q[tail].r=(10*t.r+a[i])%n; q[tail].pre=head; vis[q[tail].r]=1; if(q[tail].r==0) return tail; tail++; } head++; } return 0;}void print(int t){ if(q[t].pre!=-1) print(q[t].pre); cout<<q[t].v;}int main(){ while(cin>>n) { cin>>m; for(int i=0;i<m;i++) cin>>a[i]; if(n==0) {cout<<0<<endl; continue;} sort(a,a+m); int temp=bfs(); if(temp) {print(temp); cout<<endl;} else cout<<0<<endl; } return 0;}
- poj 1465 (BFS+剪枝)
- POJ:1465 Multiple(bfs+剪枝)
- POJ 1465 Multiple(BFS+同余剪枝)
- POJ-1465-bfs,同余剪枝
- POJ 1465 Multiple(BFS+同余剪枝)
- POJ 3985 Knight's Problem bfs + 剪枝
- POJ 1324 Holedox Moving (BFS+剪枝)
- poj 3278 Catch That Cow (BFS剪枝)
- POJ 3126 Prime Path (BFS+剪枝)
- POJ 1184 聪明的打字员 (BFS + 剪枝)
- POJ - 1724 ROADS(bfs+剪枝)
- hdu 1401/poj 1198 Solitaire(BFS,剪枝)
- POJ 3278 Catch That Cow(bfs+剪枝)
- poj 3278 Catch That Cow(BFS,剪枝)
- POJ 3278 Catch That Cow(BFS 剪枝)
- poj 3278 Catch That Cow (BFS+剪枝)
- POJ 3985 Knight's Problem(bfs+hash+剪枝)
- HDU1226-BFS剪枝
- UIImageview 点击事件 UITapGestureRecognizer传值
- asp.net解决高并发的方案(转)
- Android------水平滚动
- socket通信解析
- Linux基础命令实践二
- poj 1465 (BFS+剪枝)
- Parameter index out of range (1 > number of parameters, which is 0)
- oracle的split函数
- Hadoop集群安装配置
- UIImage转换成nsdata
- C# 中的委托和事件
- POJ 1753 Flip Game DFS枚举
- java反射机制
- Ruby学习笔记之标准库