这个递归也太慢了
写了一个用的是广度搜索
#include
#include
using namespace std;
const int Limit_Size = 1000;
struct Con
{
int x, y;
int times;
};
queue
int target, x, y, Min_time;
bool use[ Limit_Size ][ Limit_Size ];
void init( )
{
Con tmp;
int i, j, t;
cin >> x >> y >> target;
if ( x > y )
t = x;
else
t = y;
tmp.x = tmp.y = tmp.times = 0;
q.push( tmp );
for ( i = 0; i <= t; i++ )
for ( j = 0; j <= t; j++ )
use[ i ][ j ] = false;
use[ 0 ][ 0 ] = true;
}
void fillwater( Con t )
{
Con tmp;
int c;
if ( !use[ t.x ][ 0 ] ) //倒出y
{
tmp = t;
tmp.y = 0;
tmp.times = t.times + 1;
q.push( tmp );
use[ t.x ][ 0 ] = true;
}
if ( !use[ 0 ][ t.y ] ) //倒出x
{
tmp = t;
tmp.x = 0;
tmp.times = t.times + 1;
q.push( tmp );
use[ 0 ][ t.y ] = true;
}
if ( !use[ x ][ t.y ] ) //灌满x
{
tmp = t;
tmp.x = x;
tmp.times = t.times + 1;
q.push( tmp );
use[ x ][ t.y ] = true;
}
if ( !use[ t.x ][ y ] ) //灌满y
{
tmp = t;
tmp.y = y;
tmp.times = t.times + 1;
q.push( tmp );
use[ t.x ][ y ] = true;
}
c = t.x + t.y;
if ( c > x )
c = x;
if ( !use[ c ][ t.y - c + t.x ] ) //y向x倒
{
tmp = t;
tmp.x = c;
tmp.y = t.y - c + t.x;
tmp.times = t.times + 1;
q.push( tmp );
use[ c ][ t.y - c + t.x ] = true;
}
c = t.x + t.y;
if ( c > y )
c = y;
if ( !use[ t.x - c + t.y ][ c ] ) //x向y倒
{
tmp = t;
tmp.x = t.x - c + t.y;
tmp.y = c;
tmp.times = t.times + 1;
q.push( tmp );
use[ t.x - c + t.y ][ c ] = true;
}
}
void work( )
{
Con t;
while ( !q.empty( ) )
{
t = q.front( );
q.pop( );
// cout << t.x << ' ' << t.y << ' ' << t.times << endl;
if ( t.x == target || t.y == target )
{
Min_time = t.times;
break;
}
fillwater( t );
}
}
void print( )
{
if ( Min_time == 0 )
cout << "No" << endl;
else
cout << Min_time << endl;
}
int main( )
{
init( );
work( );
print( );
return 0;
}
这个递归也太慢了
写了一个用的是广度搜索
#include
#include
using
namespace
std;
const
int
Limit_Size
=
1000;
struct
Con
{
int
x,
y;
int
times;
};
queue
q;
int
target,
x,
y,
Min_time;
bool
use[
Limit_Size
][
Limit_Size
];
void
init(
)
{
Con
tmp;
int
i,
j,
t;
cin
>>
x
>>
y
>>
target;
if
(
x
>
y
)
t
=
x;
else
t
=
y;
tmp.x
=
tmp.y
=
tmp.times
=
0;
q.push(
tmp
);
for
(
i
=
0;
i
<=
t;
i++
)
for
(
j
=
0;
j
<=
t;
j++
)
use[
i
][
j
]
=
false;
use[
0
][
0
]
=
true;
}
void
fillwater(
Con
t
)
{
Con
tmp;
int
c;
if
(
!use[
t.x
][
0
]
)
//倒出y
{
tmp
=
t;
tmp.y
=
0;
tmp.times
=
t.times
+
1;
q.push(
tmp
);
use[
t.x
][
0
]
=
true;
}
if
(
!use[
0
][
t.y
]
)
//倒出x
{
tmp
=
t;
tmp.x
=
0;
tmp.times
=
t.times
+
1;
q.push(
tmp
);
use[
0
][
t.y
]
=
true;
}
if
(
!use[
x
][
t.y
]
)
//灌满x
{
tmp
=
t;
tmp.x
=
x;
tmp.times
=
t.times
+
1;
q.push(
tmp
);
use[
x
][
t.y
]
=
true;
}
if
(
!use[
t.x
][
y
]
)
//灌满y
{
tmp
=
t;
tmp.y
=
y;
tmp.times
=
t.times
+
1;
q.push(
tmp
);
use[
t.x
][
y
]
=
true;
}
c
=
t.x
+
t.y;
if
(
c
>
x
)
c
=
x;
if
(
!use[
c
][
t.y
-
c
+
t.x
]
)
//y向x倒
{
tmp
=
t;
tmp.x
=
c;
tmp.y
=
t.y
-
c
+
t.x;
tmp.times
=
t.times
+
1;
q.push(
tmp
);
use[
c
][
t.y
-
c
+
t.x
]
=
true;
}
c
=
t.x
+
t.y;
if
(
c
>
y
)
c
=
y;
if
(
!use[
t.x
-
c
+
t.y
][
c
]
)
//x向y倒
{
tmp
=
t;
tmp.x
=
t.x
-
c
+
t.y;
tmp.y
=
c;
tmp.times
=
t.times
+
1;
q.push(
tmp
);
use[
t.x
-
c
+
t.y
][
c
]
=
true;
}
}
void
work(
)
{
Con
t;
while
(
!q.empty(
)
)
{
t
=
q.front(
);
q.pop(
);
//
cout
<<
t.x
<<
'
'
<<
t.y
<<
'
'
<<
t.times
<<
endl;
if
(
t.x
==
target
||
t.y
==
target
)
{
Min_time
=
t.times;
break;
}
fillwater(
t
);
}
}
void
print(
)
{
if
(
Min_time
==
0
)
cout
<<
"No"
<<
endl;
else
cout
<<
Min_time
<<
endl;
}
int
main(
)
{
init(
);
work(
);
print(
);
return
0;
}
假设x > y 用 ax % y == z, 就行了,一般算法书上都有这类的解,抄上就行了!
题目都看不懂什么意思~~