简单题意:倒可乐,分成相同的容量,先判断能不能到相同,再求最少到多少次
解题思路:广度搜索,有目标,,一直到倒可乐, 达到目标为止
代码如下:
#include<iostream>
#include<algorithm>
#include<queue>
#include<cstring>
using namespace std;
bool is[101][101];
int m, n, s, si, sj;
struct node
{
int x,y,all,t;
};
void bfs()
{
queue<node> que;
memset(is,false,sizeof(is));
node p,q;
p.x = 0,p.y = 0,p.t = 0,p.all = s;
que.push(p);
is[p.x][p.y] = true;
while(!que.empty())
{
p = que.front();
que.pop();
if(p.y == p.all && p.y == s/2)
{
cout << p.t << endl;
return;
}
if(p.all+p.x > m)
{
q.x = m,q.y = p.y,q.all = p.all+p.x-m,q.t = p.t+1;
if(!is[q.x][q.y])
que.push(q),is[q.x][q.y] = true;
}
else
{
q.x = p.all+p.x,q.y = p.y,q.all = 0,q.t = p.t+1;
if(!is[q.x][q.y])
que.push(q),is[q.x][q.y] = true;
}
if(p.all+p.y > n)
{
q.x = p.x, q.y = n, q.all = p.all+p.y-n,q.t = p.t+1;
if(!is[q.x][q.y])
que.push(q),is[q.x][q.y] = true;
}
else
{
q.x = p.x,q.y = p.all+p.y,q.all = 0,q.t = p.t+1;
if(!is[q.x][q.y])
que.push(q),is[q.x][q.y] = true;
}
if(p.x+p.y > n)
{
q.x = p.x+p.y-n,q.y = n,q.all = p.all,q.t = p.t+1;
if(!is[q.x][q.y])
que.push(q),is[q.x][q.y] = true;
}
else
{
q.x = 0,q.y = p.x+p.y,q.all = p.all,q.t = p.t+1;
if(!is[q.x][q.y])
que.push(q),is[q.x][q.y] = true;
}
q.all = p.all+p.x,q.x = 0,q.y = p.y,q.t = p.t+1;
if(!is[q.x][q.y])
que.push(q),is[q.x][q.y] = true;
if(p.x+p.y > m)
{
q.y = p.y+p.x-m,q.x = m,q.all = p.all,q.t = p.t+1;
if(!is[q.x][q.y])
que.push(q),is[q.x][q.y] = true;
}
else
{
q.x = p.x+p.y,q.y = 0,q.all = p.all,q.t = p.t+1;
if(!is[q.x][q.y])
que.push(q),is[q.x][q.y] = true;
}
q.all = p.all+p.y,q.x = p.x,q.y = 0,q.t = p.t+1;
if(!is[q.x][q.y])
que.push(q),is[q.x][q.y] = true;
}
cout << "NO" << endl;
}
int main()
{
while(cin >> s >> m >> n)
{
if(s == 0 && m == 0 && n == 0)
break;
if(s % 2)
{
cout << "NO" << endl;
continue;
}
if(m > n) swap(m,n);
bfs();
}
return 0;
}
#include<algorithm>
#include<queue>
#include<cstring>
using namespace std;
bool is[101][101];
int m, n, s, si, sj;
struct node
{
int x,y,all,t;
};
void bfs()
{
queue<node> que;
memset(is,false,sizeof(is));
node p,q;
p.x = 0,p.y = 0,p.t = 0,p.all = s;
que.push(p);
is[p.x][p.y] = true;
while(!que.empty())
{
p = que.front();
que.pop();
if(p.y == p.all && p.y == s/2)
{
cout << p.t << endl;
return;
}
if(p.all+p.x > m)
{
q.x = m,q.y = p.y,q.all = p.all+p.x-m,q.t = p.t+1;
if(!is[q.x][q.y])
que.push(q),is[q.x][q.y] = true;
}
else
{
q.x = p.all+p.x,q.y = p.y,q.all = 0,q.t = p.t+1;
if(!is[q.x][q.y])
que.push(q),is[q.x][q.y] = true;
}
if(p.all+p.y > n)
{
q.x = p.x, q.y = n, q.all = p.all+p.y-n,q.t = p.t+1;
if(!is[q.x][q.y])
que.push(q),is[q.x][q.y] = true;
}
else
{
q.x = p.x,q.y = p.all+p.y,q.all = 0,q.t = p.t+1;
if(!is[q.x][q.y])
que.push(q),is[q.x][q.y] = true;
}
if(p.x+p.y > n)
{
q.x = p.x+p.y-n,q.y = n,q.all = p.all,q.t = p.t+1;
if(!is[q.x][q.y])
que.push(q),is[q.x][q.y] = true;
}
else
{
q.x = 0,q.y = p.x+p.y,q.all = p.all,q.t = p.t+1;
if(!is[q.x][q.y])
que.push(q),is[q.x][q.y] = true;
}
q.all = p.all+p.x,q.x = 0,q.y = p.y,q.t = p.t+1;
if(!is[q.x][q.y])
que.push(q),is[q.x][q.y] = true;
if(p.x+p.y > m)
{
q.y = p.y+p.x-m,q.x = m,q.all = p.all,q.t = p.t+1;
if(!is[q.x][q.y])
que.push(q),is[q.x][q.y] = true;
}
else
{
q.x = p.x+p.y,q.y = 0,q.all = p.all,q.t = p.t+1;
if(!is[q.x][q.y])
que.push(q),is[q.x][q.y] = true;
}
q.all = p.all+p.y,q.x = p.x,q.y = 0,q.t = p.t+1;
if(!is[q.x][q.y])
que.push(q),is[q.x][q.y] = true;
}
cout << "NO" << endl;
}
int main()
{
while(cin >> s >> m >> n)
{
if(s == 0 && m == 0 && n == 0)
break;
if(s % 2)
{
cout << "NO" << endl;
continue;
}
if(m > n) swap(m,n);
bfs();
}
return 0;
}