FAFU-1410 九数矩阵 康拓展开

来源:互联网 发布:领航员监控软件 编辑:程序博客网 时间:2024/06/05 20:47

http://acm.fafu.edu.cn/problem.php?id=1410

类似八数码问题 用整形存状态4秒 康拓展开式1秒

#include<stdio.h>#include<string.h>#include<queue>#include<vector>#include<map>using namespace std;const int maxn = 1005;const int inf = 1<<30;int n;int f[10];int mod[] = {1,10,100,1000,10000,100000,1000000,10000000,100000000};struct Node{char s[10];int step;}node[400000];char input(){char ch;while(ch = getchar(), ch == '\n' || ch == ' ');    if( ch == EOF ) return EOF;return ch;}void getf(){f[0] = 0; f[1] = 1;for( int i = 2; i <= 9; i ++ )f[i] = f[i-1] * i;}int kt(char s[],int n)//n表示该排列有n个数{    int sum = 0;    for(int i = 1; i <= n; i++)    {        int temp = 0;        for(int j = i + 1; j <= n; j++)          if( s[j] < s[i] )             temp ++;        sum += f[n - i] * temp;//f[n]表示n的阶乘    }    return sum;}Node change( Node x,int i ){char a,b,c,d;if( i == 0 ){a = x.s[1],b = x.s[2], c = x.s[4],d = x.s[5];x.s[1] = b,x.s[2] = d, x.s[4] = a,x.s[5] = c;}else if( i == 1 ){a = x.s[2],b = x.s[3], c = x.s[5],d = x.s[6];x.s[2] = b,x.s[3] = d, x.s[5] = a,x.s[6] = c;}else if( i == 2 ){a = x.s[4],b = x.s[5], c = x.s[7],d = x.s[8];x.s[4] = b,x.s[5] = d, x.s[7] = a,x.s[8] = c;}else if( i == 3 ){a = x.s[5],b = x.s[6], c = x.s[8],d = x.s[9];x.s[5] = b,x.s[6] = d, x.s[8] = a,x.s[9] = c;}return x;}void bfs(){queue<Node>que;Node cur,cnt;int k;for( int i = 1; i <= 9; i ++ )node[0].s[i] = '0' + i;node[0].step = 1;que.push(node[0]);while( !que.empty() ){cur = que.front(); que.pop();for( int i = 0; i < 4; i ++ ){cnt = change( cur,i );k = kt( cnt.s,9 );if( node[k].step == 0 ){node[k] = cnt;node[k].step = cur.step + 1;que.push( node[k] );}}}}int main(){//freopen("data.txt","r",stdin);//freopen("matrix.in","r",stdin);//freopen("matrix.out","w",stdout);getf();bfs();char s[10],st,temp;while( (s[1] = input()),s[1] != EOF ){for( int i = 2; i <= 9; i ++ ){temp = input();s[i] = temp;}int ans = node[kt(s,9)].step;printf("%d\n",ans-1);}return 0;}


#include<stdio.h>#include<string.h>#include<queue>#include<vector>#include<map>using namespace std;const int maxn = 1005;const int inf = 1<<30;int n;int mod[] = {1,10,100,1000,10000,100000,1000000,10000000,100000000};map<int,int>mark;struct node{int x,step;};int input(){char ch;while( (ch = getchar()),ch < '0' || ch > '9' );return ch - '0';}int change( int x,int i ){int u,v,a,b,c,d;if( i == 0 ){a = 8,b = 7, c = 5,d = 4;}else if( i == 1 ){a = 7,b = 6, c = 4,d = 3;}else if( i == 2 ){a = 5,b = 4, c = 2,d = 1;}else if( i == 3 ){a = 4,b = 3, c = 1,d = 0;}u = x/mod[b]%100;     v = x/mod[d]%100;x -= u * mod[b] + v * mod[d];x +=  u%10 * mod[a] + v%10*mod[b] + u/10*mod[c] + v/10*mod[d];return x;}void bfs(){int ans = 123456789;queue<node>que;node cur,cnt;cur.x = ans; cur.step = 1;mark[ans] = 1;que.push(cur);while( !que.empty() ){cur = que.front(); que.pop();for( int i = 0; i < 4; i ++ ){cnt.x = change( cur.x,i );cnt.step = cur.step + 1;if( mark[cnt.x] == 0 ){mark[cnt.x] = cnt.step;que.push( cnt );}}}}int main(){//freopen("matrix.in","r",stdin);//freopen("matrix.out","w",stdout);int st,temp;bfs();while( scanf("%d",&st) != EOF ){for( int i = 1; i < 9; i ++ ){temp = input();st = st * 10 + temp;}printf("%d\n",mark[st]-1);}return 0;}



0 0
原创粉丝点击