Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- int S,N,M;
- int vis[102][102];
- struct pos{
- int Vol;
- int volN;
- int volM;
- int steps;
- };
- void bfs();
- int main(){
- while(scanf("%d %d %d",&S,&N,&M)==3){
- if(S==0) return 0;
- if(S%2!=0) {
- printf("NO\n");
- continue;
- }
- for(int i=0;i<=101;i++)
- for(int j=1;j<=101;j++){
- vis[i][j]=0;
- }
- bfs();
- }
- return 0;
- }
- void bfs()
- {
- pos cur,nex;
- cur.Vol=S;
- cur.volN=0;
- cur.volM=0;
- cur.steps=0;
- queue<pos>qu;
- qu.push(cur);
- vis[cur.volN][cur.volM]=1;
- while(!qu.empty()){
- cur=qu.front();
- qu.pop();
- if(cur.Vol==S/2&&cur.volN==S/2||cur.Vol==S/2&&cur.volM==S/2||cur.volM==S/2&&cur.volN==S/2){
- printf("%d\n",cur.steps);
- return;
- }
- int temp;
- temp=N-cur.volN;
- if(cur.Vol<=temp){
- nex.volN=cur.Vol+cur.volN;
- nex.Vol=0;
- nex.volM=cur.volM;
- }
- else{
- nex.volN=N;
- nex.Vol=cur.Vol-temp;
- nex.volM=cur.volM;
- }
- nex.steps=cur.steps+1;
- if(nex.Vol<=S&&nex.Vol>=0&&nex.volN<=N&&nex.volN>=0){
- if(vis[nex.volN][nex.volM]==0){
- vis[nex.volN][nex.volM]=1;
- qu.push(nex);
- }
- }
- temp=M-cur.volM;
- if(cur.Vol<=temp){
- nex.volM=cur.Vol+cur.volM;
- nex.volN=cur.volN;
- nex.Vol=0;
- }
- else{
- nex.volM=M;
- nex.Vol=cur.Vol-temp;
- nex.volN=cur.volN;
- }
- nex.steps=cur.steps+1;
- if(nex.Vol<=S&&nex.Vol>=0&&nex.volM<=M&&nex.volM>=0){
- if(vis[nex.volN][nex.volM]==0){
- vis[nex.volN][nex.volM]=1;
- qu.push(nex);
- }
- }
- temp=S-cur.Vol;
- if(cur.volN<=temp){
- nex.Vol=cur.Vol+cur.volN;
- nex.volM=cur.volM;
- nex.volN=0;
- }
- else{
- nex.Vol=S;
- nex.volN=cur.volN-temp;
- nex.volM=cur.volM;
- }
- nex.steps=cur.steps+1;
- if(nex.Vol<=S&&nex.Vol>=0&&nex.volN<=N&&nex.volN>=0){
- if(vis[nex.volN][nex.volM]==0){
- vis[nex.volN][nex.volM]=1;
- qu.push(nex);
- }
- }
- temp=S-cur.Vol;
- if(cur.volM<=temp){
- nex.Vol=cur.Vol+cur.volM;
- nex.volN=cur.volN;
- nex.volM=0;
- }
- else{
- nex.Vol=S;
- nex.volM=cur.volM-temp;
- nex.volN=cur.volN;
- }
- nex.steps=cur.steps+1;
- if(nex.Vol<=S&&nex.Vol>=0&&nex.volM<=M&&nex.volM>=0){
- if(vis[nex.volN][nex.volM]==0){
- vis[nex.volN][nex.volM]=1;
- qu.push(nex);
- }
- }
- temp=M-cur.volM;
- if(cur.volN<=temp){
- nex.volM=cur.volN+cur.volM;
- nex.Vol=cur.Vol;
- nex.volN=0;
- }
- else{
- nex.volM=M;
- nex.volN=cur.volN-temp;
- nex.Vol=cur.Vol;
- }
- nex.steps=cur.steps+1;
- if(nex.volN<=N&&nex.volN>=0&&nex.volM<=M&&nex.volM>=0){
- if(vis[nex.volN][nex.volM]==0){
- vis[nex.volN][nex.volM]=1;
- qu.push(nex);
- }
- }
- temp=N-cur.volN;
- if(cur.volM<=temp){
- nex.volN=cur.volM+cur.volN;
- nex.volM=0;
- nex.Vol=cur.Vol;
- }
- else{
- nex.volN=N;
- nex.volM=cur.volM-temp;
- nex.Vol=cur.Vol;
- }
- nex.steps=cur.steps+1;
- if(nex.volN<=N&&nex.volN>=0&&nex.volM<=M&&nex.volM>=0){
- if(vis[nex.volN][nex.volM]==0){
- vis[nex.volN][nex.volM]=1;
- qu.push(nex);
- }
- }
- }
- printf("NO\n");
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment