HDU-1195-Open the Lock(BFS)
2014-08-13 14:24
387 查看
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
For each test case, print the minimal steps in one line.
[align=left]Sample Input[/align]
2
1234
2144
1111
9999
[align=left]Sample Output[/align]
2
4
[align=left]Author[/align]
YE, Kai
[align=left]Source[/align]
Zhejiang University Local Contest 2005
思路:又是一道水题。
#include <stdio.h>
#include <string.h>
#include <cstdlib>
struct{
char num[5];
int step;
}que[1000000],t;
int p[4]={1,10,100,1000};
bool vis[10000];
int main()
{
int T,i,j,len,val,temp;
char des[5];
scanf("%d",&T);
while(T--)
{
scanf("%s%s",que[0].num,des);
int top=0,bottom=1;
que[0].step=0;
memset(vis,0,sizeof vis);
while(top<bottom)
{
t=que[top];
if(strcmp(t.num,des)==0)
{
printf("%d\n",t.step);
break;
}
t.step++;
for(i=0;i<4;i++)
{
if(t.num[i]=='9') t.num[i]='1';
else t.num[i]++;
val=0;
for(j=0;j<4;j++) val=val*10+t.num[j]-'0';
if(!vis[val])
{
vis[val]=1;
que[bottom++]=t;
}
strcpy(t.num,que[top].num);
if(t.num[i]=='1') t.num[i]='9';
else t.num[i]--;
val=0;
for(j=0;j<4;j++) val=val*10+t.num[j]-'0';
if(!vis[val])
{
vis[val]=1;
que[bottom++]=t;
}
strcpy(t.num,que[top].num);
}
for(i=0;i<3;i++)
{
temp=t.num[i];
t.num[i]=t.num[i+1];
t.num[i+1]=temp;
val=0;
for(j=0;j<4;j++) val=val*10+t.num[j]-'0';
if(!vis[val])
{
vis[val]=1;
que[bottom++]=t;
}
temp=t.num[i];
t.num[i]=t.num[i+1];
t.num[i+1]=temp;
}
top++;
}
}
}
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
For each test case, print the minimal steps in one line.
[align=left]Sample Input[/align]
2
1234
2144
1111
9999
[align=left]Sample Output[/align]
2
4
[align=left]Author[/align]
YE, Kai
[align=left]Source[/align]
Zhejiang University Local Contest 2005
思路:又是一道水题。
#include <stdio.h>
#include <string.h>
#include <cstdlib>
struct{
char num[5];
int step;
}que[1000000],t;
int p[4]={1,10,100,1000};
bool vis[10000];
int main()
{
int T,i,j,len,val,temp;
char des[5];
scanf("%d",&T);
while(T--)
{
scanf("%s%s",que[0].num,des);
int top=0,bottom=1;
que[0].step=0;
memset(vis,0,sizeof vis);
while(top<bottom)
{
t=que[top];
if(strcmp(t.num,des)==0)
{
printf("%d\n",t.step);
break;
}
t.step++;
for(i=0;i<4;i++)
{
if(t.num[i]=='9') t.num[i]='1';
else t.num[i]++;
val=0;
for(j=0;j<4;j++) val=val*10+t.num[j]-'0';
if(!vis[val])
{
vis[val]=1;
que[bottom++]=t;
}
strcpy(t.num,que[top].num);
if(t.num[i]=='1') t.num[i]='9';
else t.num[i]--;
val=0;
for(j=0;j<4;j++) val=val*10+t.num[j]-'0';
if(!vis[val])
{
vis[val]=1;
que[bottom++]=t;
}
strcpy(t.num,que[top].num);
}
for(i=0;i<3;i++)
{
temp=t.num[i];
t.num[i]=t.num[i+1];
t.num[i+1]=temp;
val=0;
for(j=0;j<4;j++) val=val*10+t.num[j]-'0';
if(!vis[val])
{
vis[val]=1;
que[bottom++]=t;
}
temp=t.num[i];
t.num[i]=t.num[i+1];
t.num[i+1]=temp;
}
top++;
}
}
}
相关文章推荐
- hdu 1195 Open the Lock(BFS)
- HDU 1195 Open the Lock BFS
- hdu 1195 Open the Lock(bfs)
- hdu 1195 Open the Lock (BFS)
- 【HDU 1195】Open the Lock(BFS)
- hdu 1195 Open the Lock(基础bfs)
- HDU 1195 Open the Lock BFS
- hdu 1195 Open the Lock(双向bfs)
- HDU 1195 Open the Lock 简单搜索-bfs
- HDU 1195 Open the Lock (不一样的BFS)
- HDU--1195:Open the Lock (一道简单的BFS)
- hdu 1195 Open the Lock bfs
- Hdu 1195 Open the Lock [Bfs]
- hdu 1195 Open the Lock(bfs)
- HDU 1195 Open the Lock (双向BFS与单向BFS)
- hdu 1195 Open the Lock (BFS)
- hdu - 1195 Open the Lock (bfs) && hdu 1973 Prime Path (bfs)
- hdu-1195--Open the Lock(BFS)
- hdu 1195 Open the Lock (bfs)
- HDU 1195 Open the Lock (BFS 剪枝)