<pre name="code" class="cpp">#include <iostream>
#include <queue>
#include <math.h>
using namespace std;
#define N 1000000
#define M 10
int array[N][M];
int bfs(int *a, int cur, int x, int d , int n)
{
if (cur == d)
return 0;
for (int i = 0; i < n; i++)
{
if (cur == 0 || array[cur-1][i] < a[i])
{
if (cur)
for (int k = 0; k < n; k++)
{
array[cur][k] = array[cur - 1][k];
}
else
for (int k = 0; k < n; k++)
{
array[cur][k] = 0;
}
array[cur][i] = a[i];
if (array[cur][0] == x)
{
for (int m = 0; m <= cur; m++)
{
for (int k = 0; k <= n; k++)
cout << array[m][k] << ' ';
cout << endl;
}
return 1;
}
else
{
if (bfs(a, cur + 1, x, d, n)) return 1;
}
}
if (cur && array[cur-1][i] > 0)
{
for (int k = 0; k < n; k++)
{
array[cur][k] = array[cur - 1][k];
}
array[cur][i] = 0;
if (array[cur][0] == x)
{
for (int m = 0; m <= cur; m++)
{
for (int k = 0; k < n; k++)
cout << array[m][k] << ' ';
cout << endl;
}
return 1;
}
else
{
if (bfs(a, cur + 1, x, d, n)) return 1;
}
}
if (cur)
{
for (int j = 0; j < n; j++)
{
for (int k = 0; k < n; k++)
{
array[cur][k] = array[cur - 1][k];
}
if (i != j)
{
if (array[cur - 1][j] < a[j] && array[cur - 1][i] > 0)//此时可以倒水i给j倒水
{
array[cur][i] = array[cur - 1][i] + array[cur - 1][j] <= a[j] ? 0 : array[cur - 1][i] + array[cur - 1][j] - a[j];
array[cur][j] = array[cur - 1][i] + array[cur - 1][j] <= a[j] ? array[cur - 1][i] + array[cur - 1][j] : a[j];
if (array[cur][0] == x)// || array[cur][j] == x)
{
for (int m = 0; m <= cur; m++)
{
for (int k = 0; k < n; k++)
cout << array[m][k] << ' ';
cout << endl;
}
return 1;
}
else
{
if (bfs(a, cur + 1, x, d, n)) return 1;
}
}
}
}
}
}
return 0;
}
int main()
{
int a[M], n,x;
while (1)
{
cin >> n;
for (int i = 0; i < n; i++)
cin >> a[i];
cin >> x;
for (int i = 1;; i++)
{
for (int j = 0; j < n; j++)
array[0][j] = 0;
if (bfs(a, 0, x, i, n))
break;
}
}
return 0;
}
空瓶子装水解谜游戏
最新推荐文章于 2021-04-14 22:20:18 发布