SWUN题目地址: http://218.194.91.48/acmhome/problemdetail.do?&method=showdetail&id=1307
HDU题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=1495
初看挺难,其实就是基础的BFS,练练手。。。 竟然还找到个规律,写了个240B长度的代码~~
做法就不说了,代码应该很好理解~~
#include <iostream>
#include <cstring>
using namespace std;
struct Node{
int s,n,m,t;
Node(){}
Node(int a,int b,int c,int d){s=a,n=b,m=c,t=d;}
}que[400000];
bool vis[104][104];
inline int add(int a,int r,int R){return a+r<=R?a:R-r;}
int bfs(int S,int N,int M){
if(S&1) return -1;
int p,q,n,m,s,t;
memset(vis,0,sizeof(vis));
que[p=q=0]=Node(S,0,0,0);
while(p<=q){
n=que[p].n,m=que[p].m,s=que[p].s,t=que[p].t;
p++,t++;
if(vis[s][n]) continue;
vis[s][n]=true;
if(n==0 && m==s || m==0 && n==s) return t-1;
que[++q]=Node(s-add(s,n,N),n+add(s,n,N),m,t);
que[++q]=Node(s,n+add(m,n,N),m-add(m,n,N),t);
que[++q]=Node(s,n-add(n,m,M),m+add(n,m,M),t);
que[++q]=Node(s-add(s,m,M),n,m+add(s,m,M),t);
que[++q]=Node(s+n,0,m,t);
que[++q]=Node(s+m,n,0,t);
}
return -1;
}
int main(){
int S,N,M,res;
while(cin>>S>>N>>M,S){
res=bfs(S,N,M);
if(res==-1) cout<<"NO"<<endl;
else cout<<res<<endl;
}
return 0;
}
这是根据打表得到的规律写的~~
#include <iostream>
using namespace std;
int gcd(int a,int b){
if(b==0) return a;
return gcd(b,a%b);
}
int main(){
int S,N,M;
while(cin>>S>>N>>M,S){
S/=gcd(gcd(S,N),M);
if(S&1) cout<<"NO"<<endl;
else cout<<S-1<<endl;
}
return 0;
}