BFS标志:

初始状态经过演变到达目标状态,问最少经过几步

如九宫重排问题

蓝桥杯:BFS解决问题总结(九宫重排,跳蚱蜢,卡片交换)_职场和发展

蓝桥杯:BFS解决问题总结(九宫重排,跳蚱蜢,卡片交换)_职场和发展_02

 

跳蚱蜢问题:

蓝桥杯:BFS解决问题总结(九宫重排,跳蚱蜢,卡片交换)_#include_03

蓝桥杯:BFS解决问题总结(九宫重排,跳蚱蜢,卡片交换)_宽度优先_04

 

 卡片交换问题:

蓝桥杯:BFS解决问题总结(九宫重排,跳蚱蜢,卡片交换)_#include_05

 

蓝桥杯:BFS解决问题总结(九宫重排,跳蚱蜢,卡片交换)_宽度优先_06

 

 解题模板:

不同题目的move函数和move函数的个数不同,其他都是相同的

struct state{
 string s;
 int level;
}
set<string>allstate;
queue<state>q;
string move(string s,int flag)
{
string res;
 //res为状态移动对应的字符串
  return res;
}
int main()
{
   string start;
   q.push(state(start,0));
   while(!q.empty())
   {
     state cur=q.front();
     q.pop();
     string cur_s=q.s;
     int level=q.level;
     if(cur==end)
     {
       cout<<level;
     }
    //状态的移动1
    string new_s=move(1);
    state newstate(new_s,level+1);
    if(allstate.find(new_s)==allstate.end())
    {
       allstate.insert(new_s);
       q.push(newstate);
    }
   //状态移动2
    .....


   }

}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.

 跳蚱蜢:(填空不需要考虑时间复杂度)

#include<iostream>
#include<string>
#include<set>
#include<queue>
using namespace std;
string start = "012345678";
string ends = "087654321";
struct state
{
	string s;
	int level;
	state(string _s, int _level) :s(_s), level(_level) {};
};

string move(string s, int x)
{
	string res = s;
	int pos0 = s.find("0", 0);
	if (x == -1)
	{
		char c = res[pos0];
		res[pos0] = res[(pos0 - 1 + 9) % 9];
		res[(pos0 - 1 + 9) % 9] = c;
	}
	else if (x == -2)
	{
		char c = res[pos0];
		res[pos0] = res[(pos0 - 2 + 9) % 9];
		res[(pos0 - 2 + 9) % 9] = c;
	}
	else if (x == 1)
	{
		char c = res[pos0];
		res[pos0] = res[(pos0 + 1 + 9) % 9];
		res[(pos0 +1 + 9) % 9] = c;
	}
	else
	{
		char c = res[pos0];
		res[pos0] = res[(pos0 + 2 + 9) % 9];
		res[(pos0 +2 + 9) % 9] = c;
	}
	return res;
}
set<string>allState;
int main()
{
	queue<state>q;
	q.push(state(start, 0));
	while (!q.empty())
	{
		state cur = q.front();
		q.pop();
		string cur_s = cur.s;
	    int cur_level = cur.level;
		if (cur_s == "087654321")
		{
			cout<<cur_level;
			return 0;
		}
		allState.insert(cur_s);

		//左移1
		string new_s = move(cur_s, -1);
		state new_state(new_s, cur_level + 1);
		if (allState.find(new_s) == allState.end())
		{
			allState.insert(new_s);
			q.push(new_state);
		}
		//左移2
		new_s = move(cur_s, -2);
		state new_state1(new_s, cur_level + 1);
		if (allState.find(new_s) == allState.end())
		{
			allState.insert(new_s);
			q.push(new_state1);
		}

		//右移1
		new_s = move(cur_s, 1);
		state new_state2(new_s, cur_level + 1);
		if (allState.find(new_s) == allState.end())
		{
			allState.insert(new_s);
			q.push(new_state2);
		}

		//右移2
		new_s = move(cur_s, 2);
		state new_state3(new_s, cur_level + 1);
		if (allState.find(new_s) == allState.end())
		{
			allState.insert(new_s);
			q.push(new_state3);
		}

	}
	return 0;
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.

九宫重排:

#include<bits/stdc++.h>
using namespace std;
struct state
{
	string s;
	int level;
	state(string _s,int _level):s(_s),level(_level){
	};
}; 
string move(string s,int flag)
{
	string res=s;
	int pos=s.find('.',0);
	int x=pos/3,y=pos%3;
	int x_down=pos/3+1;
	int x_up=pos/3-1;
	int y_left=(pos)%3-1;
	int y_right=(pos)%3+1;
	if(flag==1)
	{
		if(y_left<0)
		{
			return "-1";
		}
		else
		{
			char c=res[pos];
			res[pos]=res[x*3+y_left];
			res[x*3+y_left]=c;
		}
		return res;
	}
	else if(flag==2)
	{
		if(y_right>=3)
		{
			return "-1";
		}
		else
		{
			char c=res[pos];
			res[pos]=res[x*3+y_right];
			res[x*3+y_right]=c;
		}
		return res;
		
	}
	else if(flag==3)
	{
		if(x_up<0)
		{
			return "-1";
		}
		else
		{
			char c=res[pos];
			res[pos]=res[x_up*3+y];
			res[x_up*3+y]=c;
		}
		return res;
	}
	else
	{
		if(x_down>=3)
		{
			return "-1";
		}
		else
		{
			char c=res[pos];
			res[pos]=res[x_down*3+y];
			res[x_down*3+y]=c;
		}
		return res;
	}
}
set<string>allState;
queue<state>q;
int main()
{
	string s1,s2;
	cin>>s1>>s2;
    state start(s1,0);
	q.push(start);
	while(!q.empty())
	{
	  state cur=q.front();
	  q.pop();
	  string cur_s=cur.s;
	  int level=cur.level;
	  if(cur_s==s2)
	  {
	  	cout<<level;
	  	return 0;
	  }
	  //可以和上下左右交换位置,如果越界返回
	  string new_s=move(cur_s,1);
	  if(new_s!="-1"&&allState.find(new_s)==allState.end())
	  {
           allState.insert(new_s);
		   q.push(state(new_s,level+1));	  	
	  } 
	  
	  new_s=move(cur_s,2);
	  if(new_s!="-1"&&allState.find(new_s)==allState.end())
	  {
           allState.insert(new_s);
		   q.push(state(new_s,level+1));	  	
	  } 
	  
	  new_s=move(cur_s,3);
	  if(new_s!="-1"&&allState.find(new_s)==allState.end())
	  {
           allState.insert(new_s);
		   q.push(state(new_s,level+1));	  	
	  } 
	  
	  new_s=move(cur_s,4);
	  if(new_s!="-1"&&allState.find(new_s)==allState.end())
	  {
           allState.insert(new_s);
		   q.push(state(new_s,level+1));	  	
	  } 
		
	}
	return 0; 
	
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.