D - Open the Lock开锁问题

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.
InputThe 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.
OutputFor each test case, print the minimal steps in one line.
Sample Input2
1234
2144

1111
9999Sample Output2
题目大意,給定两个由4个数构成的密码,第一个密码每位数字都能加1,减1,或者和左右的数字互换,同时,9+1变为1,1-1变为9,问最少需多少步骤才能变为第二个密码;

思路:bfs水题,主要一点就是在对已经变换过的密码进行标记(什么神仙才能想到用四维数组进行标记…),然后bfs跑这三个步骤,水题~
代码如下;

#include <iostream>
#include <cstdio>
#include <string>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#pragma warning(disable:4996)
using namespace std;
int vis[10][10][10][10];
int num,t,n;
struct nood
{
	int a[4],sum;
}s,s1,s3;
int check(nood s2)
{
	if (s2.a[0] * 1000 + s2.a[1] * 100 + s2.a[2] * 10 + s2.a[3] == n)
		return s2.sum;
	else
		return 0;
}
int bfs(string S)
{
	queue<nood>p;
	for (int i = 0; i < 4; i++)
		s.a[i] = S[i] - '0';
	s.sum = 0;
	vis[s.a[0]][s.a[1]][s.a[2]][s.a[3]] = 1;
	p.push(s);
	while (p.empty() == false)
	{
		s1 = p.front();
		//s1.sum++;
		p.pop();
		if (check(s1))
			return  s1.sum;
		for (int i = 0; i < 4; i++)//加1变化
		{
			s3 = s1;
			if (s3.a[i] == 9)
				s3.a[i] = 1;
			else s3.a[i] += 1;
			if (vis[s3.a[0]][s3.a[1]][s3.a[2]][s3.a[3]] == 0)
			{
				vis[s3.a[0]][s3.a[1]][s3.a[2]][s3.a[3]] = 1;
				s3.sum++;
				p.push(s3);
			}
		}
		for (int i = 0; i < 4; i++)//减1变化
		{
			s3 = s1;
			if (s3.a[i] == 1)
				s3.a[i] = 9;
			else s3.a[i] -= 1;
			if (vis[s3.a[0]][s3.a[1]][s3.a[2]][s3.a[3]] == 0)
			{
				vis[s3.a[0]][s3.a[1]][s3.a[2]][s3.a[3]] = 1;
				s3.sum++;
				p.push(s3);
			}
		}
		for (int i = 0; i < 3; i++)//左右互换
		{
			s3 = s1;
			int t;
			t = s3.a[i];
			s3.a[i] = s3.a[i + 1];
			s3.a[i + 1] = t;

			if (vis[s3.a[0]][s3.a[1]][s3.a[2]][s3.a[3]] == 0)
			{
				vis[s3.a[0]][s3.a[1]][s3.a[2]][s3.a[3]] = 1;
				s3.sum++;
				p.push(s3);
			}
		}
	}
}
int main()
{  
	int N;
	while (cin >> N)
	{
		string S;
		while (N--)
		{
			cin >> S;
			cin >> n;
			memset(vis, 0, sizeof(vis));
			memset(s.a, 0, sizeof(s.a));
			cout << bfs(S) << endl;
		}
	}
	return 0;
}

不知道有没有其他的更好的标记方法能够推荐…

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值