#10029. 「一本通 1.4 练习 1」棋盘游戏

该文章介绍了一个关于4x4棋盘上黑白棋子交换的问题,目标是找到从初始布局变换到最终布局的最短移动序列。通过使用宽度优先搜索(BFS)策略,结合状态编码和哈希集合避免重复状态,求解出所需的最小步数。代码中详细展示了如何实现这一算法。
摘要由CSDN通过智能技术生成

题目

题目描述

在一个 4 × 4 4\times 4 4×4 的棋盘上有 8 个黑棋和 8 个白棋,当且仅当两个格子有公共边,这两个格子上的棋是相邻的。移动棋子的规则是交换相邻两个棋子。
给出一个初始棋盘和一个最终棋盘,请找出一个最短的移动序列使初始棋盘变为最终棋盘。

输入格式

前四行,每行 4 个数字(1 或者 0),描述了初始棋盘;
接着是一个空行;
第六到第九行,每行 4 个数字(1 或者 0),描述了最终棋盘。

输出格式

输出文件的第一行是一个整数 n,表示最少的移动步数。

样例

输入

1111
0000
1110
0010

1010
0101
1010
0101

输出

4

代码详解

ACcode

#include<bits/stdc++.h>
using namespace std;
int dx[4]={1,0,0,-1};
int dy[4]={0,-1,1,0};
struct node{
	int step;
	int z[5][5];
}fir,fin;
queue<node> q;
unordered_set<int> ss;
int cod(node);
bool vis(node);
void bfs();
int main()
{
	char ch;
	for(int i=1;i<=4;i++){
		for(int j=1;j<=4;j++){
			cin>>ch;
			fir.z[i][j]=ch-'0';
		}
	}
	for(int i=1;i<=4;i++){
		for(int j=1;j<=4;j++){
			cin>>ch;
			fin.z[i][j]=ch-'0';
		}
	}
	fir.step=0;
	q.push(fir);
	ss.emplace(cod(fir));
	bfs();
	return 0;
}
int cod(node t){
	int s=0;
	for(int i=1;i<=4;i++){
		for(int j=1;j<=4;j++){
			s*=10;
			s+=t.z[i][j];
		}
	}
	return s;
}
bool vis(node t){
	int tmp=cod(t);
	if(!ss.count(tmp)){
		ss.emplace(tmp);
		return true;
	}
	return false;
}
void bfs(){
	while(!q.empty()){
		node now=q.front();
		q.pop();
		if(cod(now)==cod(fin)){
			cout<<now.step;
			return;
		}
		for(int i=1;i<=4;i++){
			for(int j=1;j<=4;j++){
				for(int k=0;k<4;k++){
					int xx=i+dx[k],yy=j+dy[k];
					if(xx>=1&&xx<=4&&yy>=1&&yy<=4){
						node fut=now;
						swap(fut.z[i][j],fut.z[xx][yy]);
						if(vis(fut)){
							fut.step=now.step+1;
							q.push(fut);
						}
					}
				}
			}
		}
	}
}

主函数

int main()
{
	char ch;
	for(int i=1;i<=4;i++){
		for(int j=1;j<=4;j++){
			cin>>ch;
			fir.z[i][j]=ch-'0';
		}
	}
	for(int i=1;i<=4;i++){
		for(int j=1;j<=4;j++){
			cin>>ch;
			fin.z[i][j]=ch-'0';
		}
	}
	fir.step=0;
	q.push(fir);
	ss.emplace(cod(fir));
	bfs();
	return 0;
}

在输入的时候把字符类型转换成整型
初始化一开始的步数为0
让ss记录一下fir的状态

cod函数

int cod(node t){
	int s=0;
	for(int i=1;i<=4;i++){
		for(int j=1;j<=4;j++){
			s*=10;
			s+=t.z[i][j];
		}
	}
	return s;
}

把这个棋盘转换成10进制

vis函数

bool vis(node t){
	int tmp=cod(t);
	if(!ss.count(tmp)){//如果从未出现过
		ss.emplace(tmp);//记录一下
		return true;
	}
	return false;
}

bfs

void bfs(){
	while(!q.empty()){
		node now=q.front();//取队头
		q.pop();
		if(cod(now)==cod(fin)){//若到达结束状态
			cout<<now.step;
			return;
		}
		for(int i=1;i<=4;i++){
			for(int j=1;j<=4;j++){//ij遍历每个格子
				for(int k=0;k<4;k++){//与上下左右哪个格子交换
					int xx=i+dx[k],yy=j+dy[k];
					if(xx>=1&&xx<=4&&yy>=1&&yy<=4){//如果在范围内
						node fut=now;
						swap(fut.z[i][j],fut.z[xx][yy]);//交换一下
						if(vis(fut)){//满足条件
							fut.step=now.step+1;//步数++
							q.push(fut);
						}
					}
				}
			}
		}
	}
}

end

经典bfs,相信读者能够看明白大部分的代码,笔者在这不做赘述

根据题目描述,我们需要找出棋盘上棋子所在的坐标。首先,我们可以遍历整个棋盘,找到所有能攻击到的格子。然后,我们再找出其中唯一一个能攻击到最多格子的棋子位置即可。 具体的解题思路如下: 1. 遍历整个棋盘,找到所有能攻击到的格子。对于每一个格子,判断它是否能被当前棋子攻击到。如果是,则将该格子的坐标加入到一个集合中。 2. 统计集合中每个坐标出现的次数,找到出现次数最多的坐标。即为棋子所在的位置。 3. 输出棋子所在的坐标。 以下是一个可能的实现(使用Python语言): ```python def find_chess_piece(board): rows = len(board) cols = len(board[0]) attacked = set() # 存储所有能被攻击到的格子 for i in range(rows): for j in range(cols): if board[i][j] == '#': # 判断当前格子是否能被攻击到 for k in range(rows): for l in range(cols): if abs(k - i) == abs(l - j): attacked.add((k, l)) count = {} # 统计每个坐标出现的次数 for pos in attacked: if pos in count: count[pos] += 1 else: count[pos] = 1 max_count = max(count.values()) # 找到出现次数最多的坐标 for pos, cnt in count.items(): if cnt == max_count: return pos board = [ ".....#..", "#...#...", ".#.#....", "..#.....", ".#.#....", "#...#...", ".....#..", "......#." ] chess_piece = find_chess_piece(board) print(chess_piece[0]+1, chess_piece[1]+1) ``` 输出结果为:8 8,表示棋子所在的格子坐标为(8, 8)。注意,这里行和列的索引从1开始,所以需要在输出时将索引加1。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值