标签:oar main icpc gre == temp using att return
12345678.
123.46758
3
bfs+康托展开判断是否重复操作
#include<bits/stdc++.h> #include<queue> #include<bits/stdc++.h> using namespace std; int p[15], q[15],t[1000005][15],cnt[1000005],vis[1000005]; int fac[15] = { 1 }; int d[5][5] = { {1,0},{-1,0},{0,1},{0,-1} }; int kt(int *a) { int sum = 0,u=0; for (int i = 0; i < 8; i++) { u = 0; for (int j = i + 1; j < 9; j++) { if (a[i] > a[j])u++; } sum += u * fac[8 - i]; } if (vis[sum])return 0; else { vis[sum] = 1; return 1; } } int bfs() { int head = 0, tail = 1; memcpy(t[0], p, sizeof(p)); while (head < tail) { int tmp[15]; memcpy(tmp, t[head], sizeof(t[head])); if (memcmp(tmp, q,sizeof(q)) == 0)return head; int k; for (k = 0; k < 9; k++)if (!tmp[k])break; int x = k / 3, y = k % 3; for (int i = 0; i < 4; i++) { int cx = x + d[i][0], cy = y+d[i][1]; if (cx < 0 || cy < 0 || cx>2 || cy>2)continue; int now = 3 * cx + cy; int temp[15]; memcpy(temp, tmp, sizeof(tmp)); temp[k] = tmp[now]; temp[now] = 0; if (kt(temp))memcpy(t[tail],temp,sizeof(temp)),cnt[tail++] = cnt[head]+1; } ++head; } return -1; } int main() { char ch; //for (int i = 1; i <= 10; i++)cout << fac[i] << endl; for (int i = 1; i <= 9; i++)fac[i] = i * fac[i - 1]; //for (int i = 1; i <= 10; i++)cout << fac[i] << endl; for (int i = 0; i < 9; i++) { cin >> ch; if (ch == ‘.‘)p[i] = 0; else p[i] = ch - ‘0‘; } for (int i = 0; i < 9; i++) { cin >> ch; if (ch == ‘.‘)q[i] = 0; else q[i] = ch - ‘0‘; } int ans = bfs(); if (ans == -1)cout << -1 << endl; else cout << cnt[ans] << endl; return 0; }
标签:oar main icpc gre == temp using att return
原文地址:https://www.cnblogs.com/czy-power/p/10356930.html