USACO 3.2 msquare 裸BFS

又是个裸BFS...

和西安网赛那道1006一样的,只不过加上了要记录方案。顺便复习map

记录方案直接在bfs队列的结点里加一个vector<int> opt,把从开头一直到当前结点的操作序列记下来

  1 /*
  2 PROB:msquare
  3 LANG:C++
  4 */
  5
  6 #include <iostream>
  7 #include <vector>
  8 #include <algorithm>
  9 #include <map>
 10 #include <queue>
 11 #include <cstdio>
 12 using namespace std;
 13
 14 struct node
 15 {
 16     vector<int> seq;
 17     int step;
 18     vector<int> opt;
 19     node(vector<int> x,int m,vector<int> o,int op):seq(x),step(m),opt(o)
 20     {
 21         opt.push_back(op);
 22     }
 23 };
 24
 25 map<vector<int>,int> M;
 26 queue<node> Q;
 27 vector<int> a;      //tmp2
 28 vector<int> A;      //init
 29 vector<int> y;      //tmp1
 30 vector<int> B;      //goal
 31 vector<int> ans;
 32 vector<int> yy;
 33 int b[10];
 34
 35 bool same()
 36 {
 37     for (int i=0;i<8;i++)
 38         if (y[i]!=b[i])
 39             return false;
 40     return true;
 41 }
 42
 43 int main()
 44 {
 45     freopen("msquare.in","r",stdin);
 46     freopen("msquare.out","w",stdout);
 47
 48     B.clear();
 49     a.clear();
 50     A.clear();
 51     for (int i=0;i<=3;i++)
 52     {
 53         cin>>b[i];
 54         A.push_back(i+1);
 55     }
 56     for (int i=7;i>=4;i--)
 57     {
 58         cin>>b[i];
 59         A.push_back(i+1);
 60     }
 61     for (int i=0;i<8;i++)
 62         B.push_back(b[i]);
 63
 64     //for (int i=0;i<8;i++)
 65     //    cout<<B[i]<<" "<<A[i]<<endl;
 66
 67     //A[0]=1;     A[1]=2;     A[2]=3;     A[3]=4;
 68     //A[4]=8;     A[5]=7;     A[6]=6;     A[7]=5;
 69     Q.push(node(A,0,a,0));
 70     M.insert(pair<vector<int>,int>(A,1));
 71     while (!Q.empty())
 72     {
 73         node tmp=Q.front();
 74         Q.pop();
 75         int st=tmp.step;
 76         //cout<<"step  "<<st<<endl;
 77         y=tmp.seq;
 78         yy=tmp.opt;
 79         if (same())
 80         {
 81             ans.clear();
 82             ans=tmp.opt;
 83             cout<<st<<endl;
 84             for (int i=1;i<=st;i++)
 85             {
 86                 char ch=ans[i]+‘A‘-1;
 87                 cout<<ch;
 88             }
 89             cout<<endl;
 90             break;
 91         }
 92         else
 93         {
 94             //opr1
 95             a=y;
 96             swap(a[0],a[4]);
 97             swap(a[1],a[5]);
 98             swap(a[2],a[6]);
 99             swap(a[3],a[7]);
100             if (!M.count(a))
101             {
102                 M.insert(pair<vector<int>,int>(a,1));
103                 Q.push(node(a,st+1,yy,1));
104             }
105             //opr2
106             a=y;
107             int t1=a[3],t2=a[7];
108             a[3]=a[2];  a[7]=a[6];
109             a[2]=a[1];  a[6]=a[5];
110             a[1]=a[0];  a[5]=a[4];
111             a[0]=t1;    a[4]=t2;
112             if (!M.count(a))
113             {
114                 M.insert(pair<vector<int>,int>(a,1));
115                 Q.push(node(a,st+1,yy,2));
116             }
117             //opr3
118             a=y;
119             int tm=a[1];
120             a[1]=a[5];  a[5]=a[6];  a[6]=a[2];  a[2]=tm;
121             if (!M.count(a))
122             {
123                 M.insert(pair<vector<int>,int>(a,1));
124                 Q.push(node(a,st+1,yy,3));
125             }
126         }
127     }
128
129     return 0;
130 }

时间: 2024-11-10 00:08:26

USACO 3.2 msquare 裸BFS的相关文章

USACO抓牛catchcow (bfs)

这题是黄巨大出的比赛题. http://poj.org/problem?id=3278 Description Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and the cow is at a point K (0 ≤ K ≤

USACO Mother&#39;s Milk(bfs)

题目请点我 题解: 水杯倒水的问题很经典,套路也是一样的,bfs找出所有状态.这道题的关键在于每次都应该进行六次的倒水尝试,细心一点.PS:三维数组表示状态真的很方便. 代码实现: /* ID: eashion LANG: C++ TASK: milk3 */ #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <queue> #

hdu 4707 仓鼠 记录深度 裸bfs

题意:linji的仓鼠丢了,他要找回仓鼠,他在房间0放了一块奶酪,按照抓鼠手册所说,这块奶酪可以吸引距离它D的仓鼠,但是仓鼠还是没有出现,现在给出一张关系图,表示各个房间的关系,相邻房间距离为1,而且图中没有回路,每个房间都是联通的,求仓鼠可能出现的房间的数量. Sample Input110 20 10 20 31 41 52 63 74 86 9 Sample Output2 1 #include <cstdio> 2 #include <algorithm> 3 #inclu

裸BFS题若干

1poj 3278 http://poj.org/problem?id=3278 #include<math.h> #include<algorithm> #include<string> #include<string.h> #include<stdio.h> #include<iostream> using namespace std; #define N 100005 #define M 155 #define INF 0x3f

USACO Mother&amp;#39;s Milk(bfs)

a=9MvljJDNdls&S=milk3">题目请点我 题解: 水杯倒水的问题非常经典,套路也是一样的,bfs找出全部状态. 这道题的关键在于每次都应该进行六次的倒水尝试,细心一点.PS:三维数组表示状态真的非常方便. 代码实现: /* ID: eashion LANG: C++ TASK: milk3 */ #include <iostream> #include <cstdio> #include <cstdlib> #include &l

USACO(含training section)水题合集[5/未完待续]

(1) USACO2.1 Ordered Fractions 枚举 排序即可,注意1/1 #include<iostream> #include<cstdio> #include<algorithm> #include<cstring> using namespace std; const int N=165,L=1e5; struct fr{ int a,b; fr(int q=0,int w=1):a(q),b(w){} }f[L]; int n,cnt

hdoj 5094 Maze 【BFS + 状态压缩】 【好多坑】

Maze Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 100000/100000 K (Java/Others) Total Submission(s): 901    Accepted Submission(s): 314 Problem Description This story happened on the background of Star Trek. Spock, the deputy captain of St

[BZOJ 1054][HAOI 2008]移动玩具(BFS+判重)

题目链接:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1054 真是水题,感谢HAOI送的福利样例23333 裸BFS,用string做判重,会八数码就会这题. 注意由于队列中状态数很多,一定要把队列的数组开大点!!! #include <iostream> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <

【POJ 3669】Meteor Shower

Meteor Shower Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11661   Accepted: 3182 Description Bessie hears that an extraordinary meteor shower is coming; reports say that these meteors will crash into earth and destroy anything they h