问题 1426: [蓝桥杯][历届试题]九宫重排

解法一:

#include<iostream>
#include<algorithm>
#include<vector>
#include<set>
#include<cstring>
#include<cstdio>
#define M 1000000
using namespace std;
 
typedef int type[9];
type qs[M];
type mb;
int front,rear;
int dir[4][2]={-1,0,0,-1,0,1,1,0};
int dis[M]={0};
set<int> vis;
int panchong(int x)
{
	int i,sum=0;
	for (i=0; i<9; i++)
	{
		sum = sum*10+qs[x][i];
	}
	if (vis.count(sum))
	{
		return 0;
	}
	vis.insert(sum);
	return 1;
}
int bfs()
{
	front = 1;
	rear = 2;
	int i,j,k=0,c,x,y,xx,yy,z,zz;
	while (front < rear)
	{
		type &s = qs[front];
		if (memcmp(s,mb,sizeof(mb)) == 0)
		{
			return front;
		}
		for (k=0; k<9; k++)
		{
			if(s[k]==0)
            {
                z=k;
            }
		}
		x = z/3;
		y = z%3;
		for (i=0; i<4; i++)
		{
			xx = x+dir[i][0];
			yy = y+dir[i][1];
			if(xx>=0 && xx<3 && yy>=0 && yy<3)
			{
				type &t = qs[rear];
				memcpy(t,s,sizeof(s));
				zz=xx*3+yy;
				t[z] = s[zz];
				t[zz] = s[z];
				if(panchong(rear))
				{
					dis[rear] = dis[front]+1;
					rear++;
				}
			}
		}
		front++;
	}
	return -1;
}
int main()
{
	int i,j,cnt;
	char ch[10],ch2[10];
	scanf("%s %s",ch,ch2);
	for (i=0; i<9; i++)
	{
		if(ch[i]<='8'&&ch[i]>='0')
            qs[1][i]=ch[i]-'0';
        else
            qs[1][i]=0;
	}
	for (i=0; i<9; i++)
	{
		if(ch2[i]<='8'&&ch2[i]>='0')
            mb[i]=ch2[i]-'0';
        else
            mb[i]=0;
	}
	cnt = bfs();
	if(cnt>0)
	{
		cout<<dis[cnt]<<endl;
	 }
	 else
	 {
	 	cout<<-1<<endl;
	 }
	return 0;
}

解法二

总的来说题意就是看两个字符串经过最少多少步能成为相同的,我是用广搜做的,有上下左右四个方向,把把两个位置的数字和’.'调换,看和想要的字符串是否相同,用map记录字符串是否出现过,这样只要搜到就一定是最短的。结果很不幸的超时了。最后用了双向BFS,把尾部的字符串也加入队列,同时搜索,用maps标记字符串是否出现过,如果是从初态转化而来,标记为1,如果是从终态转化而来,标记为2,用temp来记录步数,当双方有交替的时候就证明搜索成功。

#include <bits/stdc++.h>
using namespace std;
const int N = 10;
char a[N], b[N];
int nex[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};
map<string, int>maps;
map<string, int>temp;
int BFS()
{
	if(strcmp(a, b)==0)
		return 0;
	queue<string>q;
	q.push(a);
	q.push(b);
	while(!q.empty()) {
		string head = q.front();
		q.pop();
		int x, y, t;
		for(int i=0; i<3; i++)
			for(int j=0; j<3; j++) {
				t = i*3+j;
				if(head[t]=='.') {
					x=i;
					y=j;
					break;
				}
			}
		t=x*3+y;
		for(int i=0; i<4; i++) {
			int tx = x+nex[i][0];
			int ty = y+nex[i][1];
			
			if(tx>=0 && tx<3 && ty>=0 && ty<3) {
				int r=tx*3+ty;
				string tail = head;
				tail[t] = tail[r];
				tail[r] = '.';
				if(!maps[tail]) {
					maps[tail] = maps[head];
					temp[tail] = temp[head]+1;
					q.push(tail);
				}
				else if(maps[head]+maps[tail]==3){
					return temp[head]+temp[tail]+1;
				}
			}
		}	
		
	}
	return -1;
}
int main()
{
	scanf("%s%s", a, b);
	maps[a]=1;
	maps[b]=2;
	printf("%d\n", BFS());
	return 0;
} 

全部评论

相关推荐

会不会进去就面临裁员啊....
MellowWW:我朋友签的美区销售岗,这才是天崩开局
点赞 评论 收藏
分享
03-15 00:45
已编辑
高德地图_go开发(实习员工)
问的很简单都秒了,但是面试官没开摄像头,疑似kpi,无后续。--------------------3/14更新,3/12通知给了口头offer,3/13发了意向书,已拒。一面(35min)(25/3/6)(无后续)&nbsp;&nbsp;&nbsp;&nbsp;1、自我介绍&nbsp;&nbsp;&nbsp;&nbsp;2、介绍一下你的那个Python相关项目(本科毕设,web系统+算法模型提供部分接口)&nbsp;&nbsp;&nbsp;&nbsp;3、Java面向对象有哪些特点呢?详细说一下。&nbsp;&nbsp;&nbsp;&nbsp;4、介绍一下hashmap;为什么要把链表转换为红黑树呢?红黑树查找的时间复杂度?1.7和1.8的区别。&nbsp;&nbsp;&nbsp;&nbsp;5、介绍一下concurrentHashmap。&nbsp;&nbsp;&nbsp;&nbsp;6、synchronized锁和Lock锁有什么区别?&nbsp;&nbsp;&nbsp;&nbsp;7、公平锁的一个底层是怎么实现的呢?&nbsp;&nbsp;&nbsp;&nbsp;8、线程池的核心参数、拒绝策略、提交一个任务执行流程?&nbsp;&nbsp;&nbsp;&nbsp;9、spring有哪些特点?(ioc/aop)&nbsp;&nbsp;&nbsp;&nbsp;10、spring中对于循环依赖是怎么解决的?&nbsp;&nbsp;&nbsp;&nbsp;11、MySQL和redis的区别?&nbsp;&nbsp;&nbsp;&nbsp;12、MySQL的索引结构是什么?&nbsp;&nbsp;&nbsp;&nbsp;13、MySQL的事务有哪些特性?怎么保证?&nbsp;&nbsp;&nbsp;&nbsp;14、MySQL的默认隔离级别?可重复读是怎么做到的呢?&nbsp;&nbsp;&nbsp;&nbsp;15、介绍一下MVCC和快照读readview。&nbsp;&nbsp;&nbsp;&nbsp;16、一般在什么场景下会使用redis?&nbsp;&nbsp;&nbsp;&nbsp;17、对于大量的请求,如果此时缓存中还没有写入数据怎么办?&nbsp;&nbsp;&nbsp;&nbsp;18、介绍一下redis实现的分布式锁。&nbsp;&nbsp;&nbsp;&nbsp;19、有用过es和mongo&nbsp;DB吗?(知道,没用过)&nbsp;&nbsp;&nbsp;&nbsp;20、消息中间件用过吗?说一下你的使用场景?&nbsp;&nbsp;&nbsp;&nbsp;21、一个场景,如果说有一个接口响应的比较慢,如果说让你排查,你会怎么去排查?(上下游接口、大key问题,只答了两,后面试官补充)&nbsp;&nbsp;&nbsp;&nbsp;无手撕,反问业务。
胖墩墩的查理在学c语言:哥们我是五号面的 流程差不多
查看21道真题和解析
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务