Hdu1195
两个四位密码 问你最少多少步能到达
/*Huyyt*/ #include<bits/stdc++.h> #define mem(a,b) memset(a,b,sizeof(a)) #define pb push_back using namespace std; typedef long long ll; typedef unsigned long long ull; const int mod = 1e9 + 7; const int gakki = 5 + 2 + 1 + 19880611 + 1e9; const int MAXN = 2e5 + 5, MAXM = 2e5 + 5; /*int to[MAXM << 1], nxt[MAXM << 1], Head[MAXN], ed = 1; inline void addedge(int u, int v) { to[++ed] = v; nxt[ed] = Head[u]; Head[u] = ed; }*/ struct node { int now[5]; int step; } startpoint, endpoint, cnt, to; void print(node x) { for (int i = 1; i <= 4; i++) { cout << x.now[i]; } } int finalans; bool flag = false; int vis[10][10][10][10]; char s[10], e[10]; queue<node> que[2]; int stepsum[2]; node get_change(node x, int aim) { node cur = x; swap(cur.now[aim], cur.now[aim + 1]); return cur; } node get_next(node x, int aim, int y) { x.now[aim] += y; if (x.now[aim] > 9) { x.now[aim] = 1; } else if (x.now[aim] < 1) { x.now[aim] = 9; } return x; } void get_vis(node x, int y) { vis[x.now[1]][x.now[2]][x.now[3]][x.now[4]] = y; } void check(node x, int y) { if (vis[x.now[1]][x.now[2]][x.now[3]][x.now[4]] == (y ^ 1)) { finalans = min(finalans, stepsum[0] + stepsum[1] + 1); flag = true; } else if (vis[x.now[1]][x.now[2]][x.now[3]][x.now[4]] == -1) { que[y].push(x); get_vis(x, y); } } void doit(int x) { while (que[x].front().step == stepsum[x] && que[x].size()) { cnt = que[x].front(); que[x].pop(); for (int i = 1; i <= 4; i++) { if (i != 4) { to = get_change(cnt, i); to.step = cnt.step + 1; check(to, x); } to = get_next(cnt, i, 1); to.step = cnt.step + 1; //cout << x << " ", print(cnt), cout << " ", print(to), cout << endl; check(to, x); to = get_next(cnt, i, -1); to.step = cnt.step + 1; //cout << x << " ", print(cnt), cout << " ", print(to), cout << endl; check(to, x); } } stepsum[x]++; } int main() { int T; scanf("%d", &T); while (T--) { flag = false; scanf("%s", s + 1), scanf("%s", e + 1); stepsum[0] = stepsum[1] = 0; for (int i = 1; i <= 9; i++) { for (int j = 1; j <= 9; j++) { for (int k = 1; k <= 9; k++) { for (int w = 1; w <= 9; w++) { vis[i][j][k][w] = -1; } } } } finalans = INT_MAX; for (int i = 0; i <= 1; i++) { while (que[i].size()) { que[i].pop(); } } startpoint.step = 0, endpoint.step = 0; for (int i = 1; i <= 4; i++) { startpoint.now[i] = s[i] - '0'; endpoint.now[i] = e[i] - '0'; } //print(startpoint), print(endpoint); que[0].push(startpoint), que[1].push(endpoint); get_vis(startpoint, 0), get_vis(endpoint, 1); while (!flag) { if (que[0].size() > que[1].size()) { doit(1); } else { doit(0); } } printf("%d ", finalans); } return 0; }
Hdu1401
8X8的棋盘上有四个棋子 问你能不能在八步之内把一个状态转移到另一一个状态