蓝桥杯-穿越雷区

题目要求

需求:从一个方格中A点,按要求移动到B点。
要求:每次只能走上下左右,每次只能走一次,每次是轮换穿越’+‘,’-'两个,否则就会能量失衡,发生爆炸。

使用的算法:这题典型的就是使用BFS算法,DFS也是可以的,不过BFS明显最为简单。

bfs算法,优先广度搜索

int bfs(std::pair<int,int>s,std::pair<int,int>t)
{
	std::map<  std::pair<int,int>, std::pair<int,int>>processor;//记录前驱与后继节点进行回溯的 
	int  visted[5][5];
	memset(visted,0,sizeof(visted));
	int orient[4][2]={{-1,0},{1,0},{0,-1},{0,1}};//定义上下左右移动的方向
	std::queue<std::pair<int,int>> que; //定义一个队列
	que.push(s);
	visted[s.first][s.second]=1; 
	while(!que.empty()) 
	{
		//取出第一个元素
		auto temp=que.front();
		que.pop();//弹出队列第一个位置元素 
		if( temp.first == t.first && temp.second == t.second )
			break ;
		for(int i=0;i<4;i++)
		{  
		
			std::pair<int,int> next =std::make_pair(temp.first+orient[i][0],temp.second+orient[i][1]);
			if(next.first >=0 && next.first <=4 && 
			next.second >=0 && next.second <=4 && 
			visted[ next.first][next.second] !=1 &&
			map_info[temp.first][temp.second] != map_info[next.first][next.second] )
			{
				visted[next.first][next.second] =1;//表示已经走过了 
				que.push(next);
				processor[next]=temp;
				
				cout<<temp.first<<"  "<<temp.second<<"       "<<next.first << "  "<<next.second<<endl;
			}
		}
		cout<<"--------------------------"<<endl;
	}
	
	
//反向回溯路径	
	std::vector<std::pair<int,int>> ops;
	for(std::pair<int,int> at =t;at!=s;at=processor[at])
		{
			if(processor.find(at) ==processor.end())
				return -1;
			ops.push_back(at);
		}
		
		
//只是为了显示结果,打印出来	
	for(auto it=processor.begin();it!=processor.end();++it)
	{
		std::cout << "Key: (" << it->first.first << ", " << it->first.second << "), Value: (" 
                  << it->second.first << ", " << it->second.second << ")" << std::endl;

	}
	return ops.size();
	
}

全部代码如下

#include<bits/stdc++.h>
using namespace std;

char map_info[5][5]={
{'A','+','-','+','-'},
{'-','+','-','-','+'},
{'-','+','+','+','-'},
{'+','-','+','-','+'},
{'B','+','-','+','-'}
};

int bfs(std::pair<int,int>s,std::pair<int,int>t)
{
	std::map<  std::pair<int,int>, std::pair<int,int>>processor;//记录前驱与后继节点进行回溯的 
	int  visted[5][5];
	memset(visted,0,sizeof(visted));
	int orient[4][2]={{-1,0},{1,0},{0,-1},{0,1}};//定义上下左右移动的方向
	std::queue<std::pair<int,int>> que; //定义一个队列
	que.push(s);
	visted[s.first][s.second]=1; 
	while(!que.empty()) 
	{
		//取出第一个元素
		auto temp=que.front();
		que.pop();//弹出队列第一个位置元素 
		if( temp.first == t.first && temp.second == t.second )
			break ;
		for(int i=0;i<4;i++)
		{  
		
			std::pair<int,int> next =std::make_pair(temp.first+orient[i][0],temp.second+orient[i][1]);
			if(next.first >=0 && next.first <=4 && 
			next.second >=0 && next.second <=4 && 
			visted[ next.first][next.second] !=1 &&
			map_info[temp.first][temp.second] != map_info[next.first][next.second] )
			{
				visted[next.first][next.second] =1;//表示已经走过了 
				que.push(next);
				processor[next]=temp;
				
				cout<<temp.first<<"  "<<temp.second<<"       "<<next.first << "  "<<next.second<<endl;
			}
		}
		cout<<"--------------------------"<<endl;
	}
	
	
	
	std::vector<std::pair<int,int>> ops;
	for(std::pair<int,int> at =t;at!=s;at=processor[at])
		{
			if(processor.find(at) ==processor.end())
				return -1;
			ops.push_back(at);
		}
		
		
	
	for(auto it=processor.begin();it!=processor.end();++it)
	{
		std::cout << "Key: (" << it->first.first << ", " << it->first.second << "), Value: (" 
                  << it->second.first << ", " << it->second.second << ")" << std::endl;

	}
	return ops.size();
	
}



signed main()
{
	
	
	std::pair<int,int> s0=std::make_pair(0,0);
	std::pair<int,int> s1=std::make_pair(4,0);
	
cout<<	bfs(s0,s1);

	return 0;
}   

输出结果

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,以下是一个简单的 Java 代码示例,用于生成一个包含随机雷的二维雷区,每个格子用数字表示周围雷的数量: ```java import java.util.Random; public class MineSweeper { private int[][] board; private int row; private int col; private int numMines; public MineSweeper(int row, int col, int numMines) { this.row = row; this.col = col; this.numMines = numMines; this.board = new int[row][col]; generateMines(); countMines(); } private void generateMines() { Random rand = new Random(); int mines = numMines; while (mines > 0) { int r = rand.nextInt(row); int c = rand.nextInt(col); if (board[r][c] != -1) { board[r][c] = -1; mines--; } } } private void countMines() { for (int r = 0; r < row; r++) { for (int c = 0; c < col; c++) { if (board[r][c] == -1) { continue; } int count = 0; if (r > 0 && c > 0 && board[r-1][c-1] == -1) { count++; } if (r > 0 && board[r-1][c] == -1) { count++; } if (r > 0 && c < col-1 && board[r-1][c+1] == -1) { count++; } if (c > 0 && board[r][c-1] == -1) { count++; } if (c < col-1 && board[r][c+1] == -1) { count++; } if (r < row-1 && c > 0 && board[r+1][c-1] == -1) { count++; } if (r < row-1 && board[r+1][c] == -1) { count++; } if (r < row-1 && c < col-1 && board[r+1][c+1] == -1) { count++; } board[r][c] = count; } } } public void printBoard() { for (int r = 0; r < row; r++) { for (int c = 0; c < col; c++) { if (board[r][c] == -1) { System.out.print("* "); } else { System.out.print(board[r][c] + " "); } } System.out.println(); } } public static void main(String[] args) { MineSweeper game = new MineSweeper(5, 5, 5); game.printBoard(); } } ``` 示例输出: ``` 0 1 1 * 2 2 * 3 3 * * 4 * 4 3 2 * 3 * 2 1 1 2 1 0 ``` 其中,-1 表示一个雷,其余数字表示周围雷的数量。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

书中藏着宇宙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值