20200202 POJ - 3126 Prime Path POJ - 1426 Find The Multiple
Posted tabshh
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了20200202 POJ - 3126 Prime Path POJ - 1426 Find The Multiple相关的知识,希望对你有一定的参考价值。
>>>>>>>>>POJ 1426直达??
>>>>>>>>>POJ 3126直达??
做了这么几道搜索题,感觉差不多也摸出了门路,模板差不多记下来了,只是面对不同的题目算法不同罢了
简单写一下想法,搞明白搜索的主题到底是什么(之前的mutiple那题,一开始就没想明白到底搜谁,就没想到算法
TBC(晚上再写
===================================分割线===================================
#include <iostream> #include <cstring> #include <queue> #include <math.h> #include <map> #include<cstring> #include<cstdio> #include<algorithm> #define RG register int #define rep(i,a,b) for(RG i=a;i<=b;++i) #define per(i,a,b) for(RG i=a;i>=b;--i) #define ll long long #define inf (1<<29) using namespace std; const int maxn=20; int a[maxn][maxn],o[maxn][maxn],out[maxn][maxn],path[maxn][maxn],mins=1e9; //int line[10][10],row[10][10],block[10][10]; int n,m,flag,coun; struct point{int l,x,y;}; typedef pair<point,int>p; int dx[]={0,0,-1,1}; int dy[]={1,-1,0,0}; point now,newo,ende; queue<p> qu; inline int read(){ int x=0,f=1;char c=getchar(); while(c<‘0‘||c>‘9‘){if(c==‘-‘)f=-1;c=getchar();} while(c>=‘0‘&&c<=‘9‘){x=x*10+c-‘0‘;c=getchar();} return x*f; } inline void clean(){ memset(a,0,sizeof(a)); memset(out,0,sizeof(out)); } void print(){ return; } void dfs(ll p,int q){ if(flag){ if(q<=19 && p%n==0){flag=0;cout<<p<<endl;} if(q>19)return; dfs(p*10,q+1); dfs(p*10+1,q+1); } } int main() { while(cin>>n && n){ flag=1; dfs(1,1); } return 0; }
===================================分割线===================================
#include <iostream> #include <cstring> #include <queue> #include <math.h> #include <map> #include<cstring> #include<cstdio> #include<algorithm> #define RG register int #define rep(i,a,b) for(RG i=a;i<=b;++i) #define per(i,a,b) for(RG i=a;i>=b;--i) #define ll long long #define inf (1<<29) using namespace std; const int maxn=1e5+5; int a[maxn],vis[maxn],mins=1e9; int n,flag,coun; struct point{int x,y;}; typedef pair<point,int>p; int dx[]={0,0,-1,1}; int dy[]={1,-1,0,0}; point num,now; queue<p> qu; inline int read(){ int x=0,f=1;char c=getchar(); while(c<‘0‘||c>‘9‘){if(c==‘-‘)f=-1;c=getchar();} while(c>=‘0‘&&c<=‘9‘){x=x*10+c-‘0‘;c=getchar();} return x*f; } inline void clean(){ queue<p> empty; swap(empty,qu); memset(vis,0,sizeof(vis)); } int judge(int n){ double p=sqrt(n); rep(i,2,p)if(n%i==0){return 0;} return 1; } void find_prime(){ rep(i,1000,9999) if(judge(i)){a[i]=1;} else a[i]=0; } void print(){ rep(i,1000,9999)if(a[i])cout<<i<<endl; } int judge_num(int n,int m){ if(n==m)return 0; if(n<m){int t=m;m=n;n=t;} if(n-m>999){if((n-m)%1000==0)return 1;} else if(n-m>99){if(n/1000==m/1000 && (n-m)%100==0)return 1;} else if(n-m>9){if((n-m)%10==0 && n/100==m/100)return 1;} else if(n/10==m/10)return 1; return 0; } void bfs(){ while(!qu.empty()) { int flag=0; num=qu.front().first; int dis=qu.front().second; //cout<<"now: "<<num.x<<"dis: "<<dis<<endl; if(num.x==num.y){cout<<dis<<endl;break;} now.y=num.y; qu.pop(); rep(i,1000,9999) { if(a[i] && !vis[i] && judge_num(num.x,i)) { now.x=i; if(i==num.y){flag=1;break;} vis[i]=1; //cout<<i<<endl; qu.push(p(now,dis+1)); } } if(flag){cout<<dis+1<<endl;break;} } } int main() { n=read(); find_prime(); while(n--) { num.x=read(),num.y=read(); qu.push(p(num,0)); bfs(); clean(); } return 0; }
以上是关于20200202 POJ - 3126 Prime Path POJ - 1426 Find The Multiple的主要内容,如果未能解决你的问题,请参考以下文章