P1379 八数码难题
用string或array存储棋子,set做vis。
#include<iostream>
#include<queue>
#include<set>
#define s first
#define cnt second
using namespace std;
const string atend="123804765";
string s;
set<string>st;
class bfs_class{
private:
queue<pair<string,int>>q;
public:
void run(){
q.push(make_pair(s,0));
while(q.size()){
auto tp=q.front();
q.pop();
if(tp.s==atend){
cout<<tp.cnt;
return;
}
int x=tp.s.find('0');
if(x%3>0){//A
swap(tp.s[x],tp.s[x-1]);
if(st.find(tp.s)==st.end()){
q.push(make_pair(tp.s,tp.cnt+1));
st.insert(tp.s);
}
swap(tp.s[x],tp.s[x-1]);
}
if(x%3<2){//D
swap(tp.s[x],tp.s[x+1]);
if(st.find(tp.s)==st.end()){
q.push(make_pair(tp.s,tp.cnt+1));
st.insert(tp.s);
}
swap(tp.s[x],tp.s[x+1]);
}
if(x>2){//W
swap(tp.s[x],tp.s[x-3]);
if(st.find(tp.s)==st.end()){
q.push(make_pair(tp.s,tp.cnt+1));
st.insert(tp.s);
}
swap(tp.s[x],tp.s[x-3]);
}
if(x<6){//S
swap(tp.s[x],tp.s[x+3]);
if(st.find(tp.s)==st.end()){
q.push(make_pair(tp.s,tp.cnt+1));
st.insert(tp.s);
}
swap(tp.s[x],tp.s[x+3]);
}
}
}
}bfs;
int main(){
ios::sync_with_stdio(0);
cin.tie(nullptr);
cin>>s;
bfs.run();
return 0;
}P2730 [USACO3.2] 魔板 Magic Squares
同上。
#include<iostream>
#include<array>
#include<queue>
#include<set>
#include<string>
using namespace std;
set<array<int,8>>vis;
array<int,8>a;
class BFS {
private:
auto A(const array<int,8>&a){
array<int,8>res=a;
swap(res[0],res[7]);
swap(res[1],res[6]);
swap(res[2],res[5]);
swap(res[3],res[4]);
return res;
}
auto B(const array<int,8>&a){
return array<int,8>{a[3],a[0],a[1],a[2],a[5],a[6],a[7],a[4]};
}
auto C(const array<int,8>&a){
return array<int,8>{a[0],a[6],a[1],a[3],a[4],a[2],a[5],a[7]};
}
public:
void run(){
queue<pair<array<int,8>,string>>q;
array<int,8>starter={1,2,3,4,5,6,7,8};
q.push({starter,""});
vis.insert(starter);
while (!q.empty()) {
auto [x,y]=q.front();
q.pop();
if(x==a){
cout<<y.size()<<'\n';
for(int i=0;i<y.size();i++){
cout<<y[i];
if((i+1)%60==0){
cout<<'\n';
}
}
if(y.size()%60!=0){
cout<<'\n';
}
return;
}
array<array<int,8>,3>next;
next[0]=A(x);
next[1]=B(x);
next[2]=C(x);
for(int i=0;i<3;i++){
if(vis.find(next[i])==vis.end()){
vis.insert(next[i]);
q.push({next[i],y+char('A'+i)});
}
}
}
}
}bfs;
int main() {
ios::sync_with_stdio(0);
cin.tie(nullptr);
for (auto &&i:a){
cin>>i;
}
bfs.run();
return 0;
}D3443 【提高】走出迷宫的最短路径
#include<iostream>
#include<vector>
#include<array>
#include<queue>
#include<stack>
#define x first
#define y second
using namespace std;
int n,m,s1,s2,e1,e2;
vector<vector<int>>v,vis;
vector<vector<pair<int,int>>>last;
constexpr array<int,4>ox={-1,0,1,0},oy={0,-1,0,1};
class bfs_class{
queue<pair<int,int>>q;
public:
void run(){
q.push(make_pair(s1,s2));
vis[s1][s2]=1;
last[s1][s2]=make_pair(-1,-1);
while(q.size()){
auto tp=q.front();
q.pop();
if(tp==make_pair(e1,e2)){
stack<pair<int,int>>s;
{
int tx=e1,ty=e2;
while(last[tx][ty]!=make_pair(-1,-1)){
s.push(make_pair(tx,ty));
tx=last[tx][ty].x;
ty=last[tx][ty].y;
}
}
cout<<'('<<s.top().x<<','<<s.top().y<<')';
while(s.size()){
s.pop();
cout<<"->("<<s.top().x+1<<','<<s.top().y+1<<')';
}
return;
}
for(int i=0;i<4;i++){
int nx=tp.x+ox[i],ny=tp.y+oy[i];
if(nx>=0&&nx<n&&ny>=0&&ny<m&&!v[nx][ny]&&!vis[nx][ny]){
q.push(make_pair(nx,ny));
vis[nx][ny]=1;
last[nx][ny]=make_pair(tp.x,tp.y);
}
}
}
cout<<"no way";
}
}bfs;
int main(){
ios::sync_with_stdio(0);
cin.tie(nullptr);
cin>>n>>m;
v.resize(n);
for(auto &&i:v){
i.resize(m);
for(auto &&t:i){
cin>>t;
}
}
vis.resize(n);
for(auto &&i:vis){
i.resize(m);
}
last.resize(n);
for(auto &&i:last){
i.resize(m);
}
cin>>s1>>s2>>e1>>e2;
s1--;
s2--;
e1--;
e2--;
bfs.run();
return 0;
}P1699 [USACO19OPEN] Bucket Brigade B
二维bfs板子。作业和复习一样
#include<iostream>
#include<array>
#include<queue>
#define x first
#define y second
using namespace std;
pair<int,int>b,r,l;
array<array<char,10>,10>a;
array<array<bool,10>,10>vis;
constexpr array<int,4>ox={-1,0,1,0},oy={0,-1,0,1};
class bfs_class{
private:
queue<pair<int,int>>q;
queue<int>cnt;
public:
void run(){
q.push(l);
cnt.push(0);
vis[l.x][l.y]=1;
while(q.size()){
auto tp=q.front();
auto tpp=cnt.front();
q.pop();
cnt.pop();
if(tp==b){
cout<<tpp-1;
return;
}
for(int i=0;i<4;i++){
int nx=tp.x+ox[i],ny=tp.y+oy[i];
if(nx>=0&&nx<10&&ny>=0&&ny<10&&make_pair(nx,ny)!=r&&!vis[nx][ny]){
q.push(make_pair(nx,ny));
cnt.push(tpp+1);
vis[nx][ny]=1;
}
}
}
}
}bfs;
int main(){
ios::sync_with_stdio(0);
cin.tie(nullptr);
for(auto &&i:a){
for(auto &&t:i){
cin>>t;
}
}
for(size_t i=0;i<a.size();i++){
for(size_t t=0;t<a[i].size();t++){
if(a[i][t]=='B'){
b=make_pair(i,t);
}else if(a[i][t]=='R'){
r=make_pair(i,t);
}else if(a[i][t]=='L'){
l=make_pair(i,t);
}
}
}
bfs.run();
return 0;
}本页面的全部内容在 CC BY-SA 4.0 和 SATA 协议之条款下提供,附加条款亦可能应用
