| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
poj 100题留念 弱弱贴下0ms的模拟队列的代码记录路径的bfs 微水 wa了几发在输出的地方 gcd判断impossible 最少次时可能有多种方案 输出一种就好 string记录路径是坠吼的
#include<cstdio>
#include<cstring>
#include<string>
#include <algorithm>
using namespace std;
int a, b, c, t, f, w, m[105][105];
char s[6][11] = { "FILL(1)","FILL(2)","DROP(1)","DROP(2)","POUR(1,2)","POUR(2,1)" };
struct { int x, y; string v; }q[105];
int gcd(int a, int b)
{
return b ? gcd(b, a%b) : a;
}
void in(int x, int y)
{ if ((x == c || y == c)&&f==0) {
printf("%d\n", q[t].v.size());
for (int i = 0; i < q[t].v.size(); i++)
printf("%s\n", s[q[t].v[i] - '1']);
f = 1;
}
q[t].x = x, q[t].y = y;
t = (t + 1) % 105;
}
void out()
{
int i, j;
if (q[w].x != a&&m[a][q[w].y] == 0) m[a][q[w].y] = 1, q[t].v = q[w].v + '1', in(a, q[w].y);
if (q[w].y != b&&m[q[w].x][b] == 0) m[q[w].x][b] = 1, q[t].v = q[w].v + '2', in(q[w].x, b);
if (q[w].x != 0 && m[0][q[w].y] == 0) m[0][q[w].y] = 1, q[t].v = q[w].v + '3', in(0, q[w].y);
if (q[w].y != 0 && m[q[w].x][0] == 0) m[q[w].x][0] = 1, q[t].v = q[w].v + '4', in(q[w].x, 0);
i = min(q[w].x, b - q[w].y);
if (m[q[w].x - i][q[w].y + i] == 0)
m[q[w].x - i][q[w].y + i] = 1, q[t].v = q[w].v + '5', in(q[w].x - i, q[w].y + i);
i = min(q[w].y, a - q[w].x);
if (m[q[w].x + i][q[w].y - i] == 0)
m[q[w].x + i][q[w].y - i] = 1, q[t].v = q[w].v + '6', in(q[w].x + i, q[w].y - i);
w=(w+1)%105;
}
int main()
{
memset(q, 0, sizeof(q)), memset(m, 0, sizeof(m)), t = w = f = 0;
scanf("%d%d%d", &a, &b, &c);
in(0, 0);
if (c % gcd(a, b) != 0) puts("impossible");
else while (f == 0 && t != w) out();
}
Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator