HDU 1195 Open the Lock(BFS)

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

一道广搜的题目。题意:一个4位字符串通过若干次操作可以变换成给定的字符串,要你求操作的最少次数。注:字符串的每一位字符 + 1或 - 1就算作一次操作,将字符串的相邻两位字符交换位置也算作一次操作。

这题对一个密码串我们可以将它定义成一个结构体,code

struct code{
	int a[4];			//存四个位上的数 
	int step;
};

step表示变到这个字符串所用的步数,用一个数组a存上四个位的数
广搜思路就是,对于每种情况下的字符串s,把s通过一次变换能得到的所有字符串全部按顺序放进广搜队列q中,再按顺序检查队首元素,如果发现了一个和目标code相同的字符串,那么就可以结束了,返回步数即可

广搜其实就和围棋中阿尔法狗搜索棋局一样,每次把下一步的可能情况都列出来,再和目标进行比对

AC代码:
1.用四维tag数组标记这个四位数是否被枚举过,如果枚举过,就没有必要再加入到队列中了,这点之前没注意,后来想起来了
2.memset每次初始化tag数组别忘了,这次又写掉了,大意了。。。

/*
两种思路:bfs, dfs 
*/
#include<iostream>
#include<queue>
#include<memory.h>
using namespace std;

int tag[10][10][10][10];

struct code{
	int a[4];			//存四个位上的数 
	int step;
};

int bfs(code n, code m)
{
	bool flag = true;
	queue<code> q;
	q.push(n);
	while(!q.empty())
	{
		flag = true;
		code f = q.front();
		for(int i = 0;i < 4;i++)
		{
			if(f.a[i] != m.a[i])
			{
				flag = false;
				break;
			}
		}
		if(flag)
			return f.step;
			
		// + 1
		for(int i = 0; i < 4;i++)
		{
			code current = f;
			if(f.a[i] == 9)
				current.a[i] = 1;
			else
				current.a[i]++;
			if(tag[current.a[0]][current.a[1]][current.a[2]][current.a[3]] == 0)
			{
				tag[current.a[0]][current.a[1]][current.a[2]][current.a[3]] = 1;
				current.step++;
				q.push(current);
			}
		}
		
		// - 1
		for(int i = 0; i < 4;i++)
		{
			code current = f;
			if(f.a[i] == 1)
				current.a[i] = 9;
			else
				current.a[i]--;
			if(tag[current.a[0]][current.a[1]][current.a[2]][current.a[3]] == 0)
			{
				tag[current.a[0]][current.a[1]][current.a[2]][current.a[3]] = 1;
				current.step++;
				q.push(current);
			}
		}
		
		//交换
		for(int i = 0;i < 3;i++)
		{
			code current = f; 
			current.a[i + 1] = f.a[i];
			current.a[i] = f.a[i + 1];
			if(tag[current.a[0]][current.a[1]][current.a[2]][current.a[3]] == 0)
			{
				tag[current.a[0]][current.a[1]][current.a[2]][current.a[3]] = 1;
				current.step++;
				q.push(current);
			}
		} 
		//弹出队首元素 
		q.pop();
	}
	return 0;
}

int main()
{
	int T;
	cin >> T;
	while(T-- > 0)
	{
		memset(tag, 0, sizeof(tag));			//初始化一定注意!又忘记了,WA一次 
		string str1, str2;
		cin >> str1 >> str2;
		code n;
		n.step = 0;
		for(int i = 0;i < 4;i++)
		{
			n.a[i] = str1[i] - '0';
		}
		code m;
		m.step = 0;
		for(int i = 0;i < 4;i++)
		{
			m.a[i] = str2[i] - '0';
		}
		cout << bfs(n, m) << endl;
	}
	return 0;
}

总体来说,这道题思路好想,但写法和一些细节值得注意和学习!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值