HDU 1195 Open the Lock


Open the Lock

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6627    Accepted Submission(s): 2986


Problem Description
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
 

Author
YE, Kai
 

Source
 

Recommend
Ignatius.L   |   We have carefully selected several similar problems for you:   1175  1072  1026  1180  1240 
 

Statistic |  Submit |  Discuss |  Note


简单BFS,但是改了很久就是没找到错,最后发现是if(next.num[i]=0)这样只写了一个=

当时就想掐死自己啊!!!

三个方向 两两互换,+1,-1,注意只在横向改变。当数字9+1=10时记为1,     1-1=0时记为9



#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<queue>
#include<algorithm>
using namespace std;
struct point{
	int num[4],step;
};
int a[4];
int vis[11][11][11][11];
void bfs(point p){
	queue<point>que; 
	int i;
	que.push(p);
	point next;
	while(!que.empty()){
		p=que.front(); 
		que.pop();
		if(p.num[0]==a[0]&&p.num[1]==a[1]&&p.num[2]==a[2]&&p.num[3]==a[3]){
			printf("%d\n",p.step);
			return ;
		}
		for(i=0;i<3;i++){//交换 
			next=p;
			next.num[i]=p.num[i+1];  next.num[i+1]=p.num[i];
			if(!vis[next.num[0]][next.num[1]][next.num[2]][next.num[3]]){
				vis[next.num[0]][next.num[1]][next.num[2]][next.num[3]]=1;
				next.step++;
				que.push(next);
			}
		}
		for(i=0;i<4;i++){//+1 
			next=p;
			next.num[i]++;
			if(next.num[i]==10)
				next.num[i]=1;
			if(!vis[next.num[0]][next.num[1]][next.num[2]][next.num[3]]){
				vis[next.num[0]][next.num[1]][next.num[2]][next.num[3]]=1;
				next.step++;
				que.push(next);
			}
		}
		for(i=0;i<4;i++){//-1 
			next=p;
			next.num[i]--;
			if(next.num[i]==0)
				next.num[i]=9;
			if(!vis[next.num[0]][next.num[1]][next.num[2]][next.num[3]]){
				vis[next.num[0]][next.num[1]][next.num[2]][next.num[3]]=1;
				next.step++;
				que.push(next);
			}
		}
	}
}
int main(){
	int n;
	char a1[4],a2[4];
	scanf("%d",&n);
	while(n--){
		memset(vis,0,sizeof(vis));
		point p;
		scanf("%s%s",a1,a2);
		a[0]=a1[0]-'0';   a[1]=a1[1]-'0';
		a[2]=a1[2]-'0';   a[3]=a1[3]-'0';
		p.num[0]=a2[0]-'0';   p.num[1]=a2[1]-'0';
		p.num[2]=a2[2]-'0';   p.num[3]=a2[3]-'0';
		vis[p.num[0]][p.num[1]][p.num[2]][p.num[3]]=1;
		p.step=0;
		bfs(p);
	}
	return 0;
} 




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值