HDU查找(4)

1067 Gap

bfs,注意输入的数据是short可以转化为char再转化为string来存储
打表不可行,因为情况实在是太多了

#pragma warning(disable:4996)
#include<cstdio>
#include<map>
#include<cmath>
#include<vector>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cstdlib>
#include<stack>
#include<queue>
#include<set>
using namespace std;

char temp[35];
const char mp[35] = {11,12,13,14,15,16,17,1,
			   21,22,23,24,25,26,27,1,
			   31,32,33,34,35,36,37,1,
			   41,42,43,44,45,46,47,1};

void showmap()
{
	int k;
	for (int i = 0; i < 4; i++)
	{
		for (int j = 0; j < 8; j++)
		{
			printf("%hhd ", temp[8 * i + j]);
		}
		printf("\n");
	}
	scanf("%d", &k);
	printf("\n");
	
}

int bfs()
{
	if (!strcmp(temp, mp))return 0;
	pair<string, int> p, tp;
	queue< pair<string, int>> q; q.push({ temp,0 });
	set<string> s; s.insert(temp);
	while (!q.empty())
	{
		tp = q.front(); q.pop();
		strcpy(temp, tp.first.c_str());
		//showmap(); 
		for (int i = 0; i < 32; i++)
		{
			if (temp[i] == 1 && temp[i - 1] != 1 && temp[i - 1] % 10 != 7)
			{
				char ttemp[35]; strcpy(ttemp, temp);
				for (int j = 0; j < 32; j++) if (ttemp[j] == ttemp[i - 1] + 1) ttemp[j] = 1;
				ttemp[i] = ttemp[i - 1] + 1;
				if (!strcmp(ttemp, mp))return tp.second + 1;
				if (s.find(ttemp) == s.end())
				{
					q.push({ ttemp,tp.second + 1 });
					s.insert(ttemp);
				}
			}
		}
	}
	return -1;
}

int main()
{
	int n; scanf("%d", &n);
	while (n--)
	{
		for (int i = 0; i < 4; i++)
		{
			temp[8 * i] = 10 * i + 11;
			for (int j = 0; j < 7; j++)
			{
				scanf("%hhd", temp + 8 * i + j + 1);
				if (temp[8 * i + j + 1] % 10 == 1)temp[8 * i + j + 1] = 1;
			}
		}
		printf("%d\n", bfs());
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值