HDU 1495 - 非常可乐 / SWUN 1307 - 一桶汽油引发的血案

 

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;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值