1 题目编号:1017
2 题目内容:
7 4 3<br>4 1 3<br>0 0 0
NO<br>3
3 解题思路形成过程:很明显,这个题目用BFS最好,由于数据比较小(100内),所以可以直接用visit数组来判重,同时由于数据类型应该都是整型,所以在s为奇数的时候直接输出NO,每次只有6种情况,分别是s->m/n,m->s/n,n->s/m,然后每种又分能倒完和不能倒完两种情况,分别判断入队即可。代码虽长,但基本都是复制粘贴
4 代码:#include <iostream>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
int s,m,n;
bool visit[105][105][105];
unsigned long long int sum, aim; typedef struct dt
{
int s, m, n;
unsigned long long int times;
}Data; typedef struct queue
{
unsigned long long int head, tail;
Data data[1000000];
}Queue; Queue q; void bfs()
{
q.head = 0;
q.tail = 0;
dt t, k;
t.s = s;
t.m = 0;
t.n = 0;
t.times = 0;
q.data[q.tail++] = t;
while (q.head != q.tail) {
t = q.data[q.head++];
if ((t.s == aim && t.m == aim) || (t.s == aim && t.n == aim) || (t.m == aim && t.n == aim)) {
sum = t.times;
return;
}
if (t.s != 0 && t.m<m) {
k = t;
if (m - t.m >= t.s) {
k.m += k.s;
k.s = 0;
k.times++;
if (visit[k.s][k.m][k.n] == 0) {
visit[k.s][k.m][k.n] = 1;
q.data[q.tail++] = k;
}
}
else
{
k.s = k.s - (m - k.m);
k.m = m;
k.times++;
if (visit[k.s][k.m][k.n] == 0) {
visit[k.s][k.m][k.n] = 1;
q.data[q.tail++] = k;
}
}
}
if (t.s != 0 && t.n<n) {
k = t;
if (n - t.n >= t.s) {
k.n += k.s;
k.s = 0;
k.times++;
if (visit[k.s][k.m][k.n] == 0) {
visit[k.s][k.m][k.n] = 1;
q.data[q.tail++] = k;
}
}
else
{
k.s = k.s - (n - k.n);
k.n = n;
k.times++;
if (visit[k.s][k.m][k.n] == 0) {
visit[k.s][k.m][k.n] = 1;
q.data[q.tail++] = k;
}
}
}
if (t.m != 0 && t.n<n) {
k = t;
if (n - t.n >= t.m) {
k.n += k.m;
k.m = 0;
k.times++;
if (visit[k.s][k.m][k.n] == 0) {
visit[k.s][k.m][k.n] = 1;
q.data[q.tail++] = k;
}
}
else
{
k.m = k.m - (n - k.n);
k.n = n;
k.times++;
if (visit[k.s][k.m][k.n] == 0) {
visit[k.s][k.m][k.n] = 1;
q.data[q.tail++] = k;
}
}
}
if (t.m != 0 && t.s<s) {
k = t;
if (s - t.s >= t.m) {
k.s += k.m;
k.m = 0;
k.times++;
if (visit[k.s][k.m][k.n] == 0) {
visit[k.s][k.m][k.n] = 1;
q.data[q.tail++] = k;
}
}
else
{
k.m = k.m - (s - k.s);
k.s = s;
k.times++;
if (visit[k.s][k.m][k.n] == 0) {
visit[k.s][k.m][k.n] = 1;
q.data[q.tail++] = k;
}
}
}
if (t.n != 0 && t.s<s) {
k = t;
if (s - t.s >= t.n) {
k.s += k.n;
k.n = 0;
k.times++;
if (visit[k.s][k.m][k.n] == 0) {
visit[k.s][k.m][k.n] = 1;
q.data[q.tail++] = k;
}
}
else
{
k.n = k.n - (s - k.s);
k.s = s;
k.times++;
if (visit[k.s][k.m][k.n] == 0) {
visit[k.s][k.m][k.n] = 1;
q.data[q.tail++] = k;
}
}
}
if (t.n != 0 && t.m<m) {
k = t;
if (m - t.m >= t.n) {
k.m += k.n;
k.n = 0;
k.times++;
if (visit[k.s][k.m][k.n] == 0) {
visit[k.s][k.m][k.n] = 1;
q.data[q.tail++] = k;
}
}
else
{
k.n = k.n - (m - k.m);
k.m = m;
k.times++;
if (visit[k.s][k.m][k.n] == 0) {
visit[k.s][k.m][k.n] = 1;
q.data[q.tail++] = k;
}
}
}
}
}
int main(int argc, const char * argv[]) {
while (1) {
cin >> s >> m >> n;
if (s == 0) {
return 0;
}
memset(visit, 0, sizeof(visit));
sum = 0;
if (s % 2) {
cout << "NO" << endl;
continue;
}
else
aim = s / 2;
bfs();
if (sum) {
cout << sum << endl;
}
else
{
cout << "NO" << endl;
}
}
return 0;
}