zoj2416题解
来源:互联网 发布:卢本伟衣服淘宝店地址 编辑:程序博客网 时间:2024/06/01 10:52
Now an emergent task for you is to open a password lock. The password is consisted of four digits. Each digit is numbered from 1 to 9.
Each time, you can add or minus 1 to any digit. When add 1 to '9', the digit will change to be '1' and when minus 1 to '1', the digit will change to be '9'. You can also exchange the digit with its neighbor. Each action will take one step.
Now your task is to use minimal steps to open the lock.
Note: The leftmost digit is not the neighbor of the rightmost digit.
Input
The input file begins with an integer T, indicating the number of test cases.
Each test case begins with a four digit N, indicating the initial state of the password lock. Then followed a line with anotther four dight M, indicating the password which can open the lock. There is one blank line after each test case.
Output
For each test case, print the minimal steps in one line.
Sample Input
2
1234
2144
1111
9999
Sample Output
2
4
#include<stdio.h>#include<stdlib.h>#include<math.h>typedef struct node{ int data; int floor; }node;typedef struct QNode{ node data; struct QNode *next; }QNode,*QueueP;typedef struct { QueueP font; QueueP rear; }LinkQueue;LinkQueue Q;void InitQueue(LinkQueue &Q){ Q.font=Q.rear=(QueueP )malloc(sizeof(QNode)); Q.font->next=NULL; }void EnQueue(LinkQueue &Q,node e){ QueueP p; p=(QueueP )malloc(sizeof(QNode)); p->data=e; p->next=NULL; Q.rear->next=p; Q.rear=p; }void DeQueue(LinkQueue &Q,node &head){ QueueP p; p=Q.font->next; head=p->data; Q.font->next=p->next; if(Q.rear==p)Q.rear=Q.font;//如果队列中的最后一个元素被删除的时候,队尾指针也会被删除所以要将队头指针赋给队尾,然后再释放p free(p); }int QueueEmpty(LinkQueue Q){ if(Q.font==Q.rear)return 1; else return 0; } #define SIZE 10000void add(node head,int *key){ node rear; int temp[4],org[4]; for(int i=0;i<4;i++){ org[3-i]=head.data/(int)pow(10.0,i)%10; } for(int i=0;i<4;i++){ if(org[3-i]==9){ temp[3-i]=1; rear.data=0; for(int j=0;j<4;j++){ if(j!=3-i)temp[j]=org[j]; rear.data+=temp[j]*(int)pow(10.0,3-j); } } else rear.data=head.data+(int)pow(10.0,i); rear.floor=head.floor+1; if(!key[rear.data]){ key[rear.data]=1; EnQueue(Q,rear); } } }void minus(node head,int *key){ node rear; int org[4],temp[4]; for(int j=0;j<4;j++) org[3-j]=head.data/(int)pow(10.0,j)%10; for(int i=0;i<4;i++){ if(org[3-i]==1){ temp[3-i]=9; rear.data=0; for(int j=0;j<4;j++){ if(j!=3-i)temp[j]=org[j]; rear.data+=temp[j]*(int)pow(10.0,3-j); } } else rear.data=head.data-(int)pow(10.0,i); rear.floor=head.floor+1; if(!key[rear.data]){ key[rear.data]=1; EnQueue(Q,rear); } } }void exchange(node head,int *key){ node rear; int temp[4],temp1,org[4]; for(int i=0;i<4;i++){ org[3-i]=(head.data/(int)pow(10.0,i))%10; } for(int i=0;i<=2;i++){ rear.data=0; for(int k=0;k<4;k++)temp[k]=org[k]; if(temp[i]!=temp[i+1]){ temp1=temp[i]; temp[i]=temp[i+1]; temp[i+1]=temp1; } for(int j=0;j<4;j++) rear.data+=temp[j]*(int)pow(10.0,3-j); rear.floor=head.floor+1; if(!key[rear.data]){ key[rear.data]=1; EnQueue(Q,rear); } } }int main(){ int T,N,M,i; node head; int key[SIZE]; scanf("%d",&T); while(T--){ InitQueue(Q); for(i=0;i<SIZE;i++) key[i]=0; scanf("%d%d",&N,&M); head.data=N; head.floor=0; key[N]=1; EnQueue(Q,head); while(!QueueEmpty(Q)){ DeQueue(Q,head); if(head.data==M)break; else{ add(head,key); minus(head,key); exchange(head,key); } } printf("%d\n",head.floor); } }
- zoj2416题解
- HDU1195 ZOJ2416 Open the Lock
- 题解
- 题解
- 题解
- 题解
- 题解
- 题解
- 题解
- 题解
- 题解
- 题解
- 题解
- 题解~~~~
- 题解。。。。
- 题解
- 题解
- 1002 题解
- configure: error: cannot run /bin/sh ./config.sub解决办法
- 64 位系统遇到未在本地计算机上注册 Microsoft.Jet.OLEDB.4.0
- JDBC的封装类构建
- Android开发中Dialog对话框的使用
- 解决android模拟器中FileExplorer不显示问题
- zoj2416题解
- 【记忆化搜索】 HDU-4155-The Game of 31
- 如何为Android应用程序添加社会化分享
- .exe.config和.vshost.exe.config的差异
- PHP设计模式之装饰模式
- Android游戏开发框架libgdx的使用
- .NET Framework中存在的诸多问题。
- 开发者手册:Android开发平台概述
- maven