码迷,mamicode.com
首页 > 其他好文 > 详细

HDU1401(双向BFS)

时间:2019-12-05 22:12:08      阅读:113      评论:0      收藏:0      [点我收藏+]

标签:eof   deque   empty   users   window   name   define   str   element   

题意:http://acm.hdu.edu.cn/showproblem.php?pid=1401

给你8*8的棋盘和4个棋子初始位置、最终位置,问你能否在8次操作后达到该状态。

思路:

双向BFS,起点开始正搜4步,终点倒搜4步,map标记。

  1 #define IOS ios_base::sync_with_stdio(0); cin.tie(0);
  2 #include <cstdio>//sprintf islower isupper
  3 #include <cstdlib>//malloc  exit strcat itoa system("cls")
  4 #include <iostream>//pair
  5 #include <fstream>//freopen("C:\\Users\\13606\\Desktop\\Input.txt","r",stdin);
  6 #include <bitset>
  7 //#include <map>
  8 #include<unordered_map>
  9 #include <vector>
 10 #include <stack>
 11 #include <set>
 12 #include <string.h>//strstr substr strcat
 13 #include <string>
 14 #include <time.h>// srand(((unsigned)time(NULL))); Seed n=rand()%10 - 0~9;
 15 #include <cmath>
 16 #include <deque>
 17 #include <queue>//priority_queue<int, vector<int>, greater<int> > q;//less
 18 #include <vector>//emplace_back
 19 //#include <math.h>
 20 #include <cassert>
 21 #include <iomanip>
 22 //#include <windows.h>//reverse(a,a+len);// ~ ! ~ ! floor
 23 #include <algorithm>//sort + unique : sz=unique(b+1,b+n+1)-(b+1);+nth_element(first, nth, last, compare)
 24 using namespace std;//next_permutation(a+1,a+1+n);//prev_permutation
 25 //******************
 26 clock_t __START,__END;
 27 double __TOTALTIME;
 28 void _MS(){__START=clock();}
 29 void _ME(){__END=clock();__TOTALTIME=(double)(__END-__START)/CLOCKS_PER_SEC;cout<<"Time: "<<__TOTALTIME<<" s"<<endl;}
 30 //***********************
 31 #define rint register int
 32 #define fo(a,b,c) for(rint a=b;a<=c;++a)
 33 #define fr(a,b,c) for(rint a=b;a>=c;--a)
 34 #define mem(a,b) memset(a,b,sizeof(a))
 35 #define pr printf
 36 #define sc scanf
 37 #define ls rt<<1
 38 #define rs rt<<1|1
 39 typedef pair<int,int> PII;
 40 typedef vector<int> VI;
 41 typedef unsigned long long ull;
 42 typedef long long ll;
 43 typedef double db;
 44 const double E=2.718281828;
 45 const double PI=acos(-1.0);
 46 const ll INF=(1LL<<60);
 47 const int inf=(1<<30);
 48 const double ESP=1e-9;
 49 const int mod=(int)1e9+7;
 50 const int N=(int)1e6+10;
 51 
 52 int mp[10][10];
 53 unordered_map<ull,bool>mark;
 54 struct node
 55 {
 56     ull id;
 57     int step;
 58 };
 59 ull get()
 60 {
 61     ull now=0,temp=1;
 62     int cnt=-1;
 63     for(int i=1;i<=8;++i)
 64     {
 65         for(int j=1;j<=8;++j)
 66         {
 67             ++cnt;
 68             if(mp[i][j])
 69                 now|=temp<<cnt;
 70         }
 71     }
 72     return now;
 73 }
 74 bool ok(int x,int y)
 75 {
 76     return x>=1&&x<=8&&y>=1&&y<=8;
 77 }
 78 bool ans;
 79 void bfs(ull start,bool f)
 80 {
 81     queue<node>q;
 82     q.push({start,0});
 83     while(!q.empty())
 84     {
 85         node now=q.front();q.pop();
 86         if(!f)
 87         {
 88             if(mark[now.id])continue;
 89             mark[now.id]=1;
 90         }
 91         else
 92         {
 93             if(mark[now.id])
 94             {
 95                 ans=1;
 96                 return;
 97             }
 98         }
 99         if(now.step==4)continue;
100         int cnt=-1;
101         for(int i=1;i<=8;++i)
102         {
103             for(int j=1;j<=8;++j)
104             {
105                 ++cnt;
106                 mp[i][j]=(int)(now.id>>cnt)&1;
107             }
108         }
109         for(int i=1;i<=8;++i)
110         {
111             for(int j=1;j<=8;++j)
112             {
113                 if(mp[i][j]&&ok(i-1,j)&&mp[i-1][j]==1&&ok(i-2,j)&&mp[i-2][j]==0)
114                 {
115                     mp[i-2][j]=1,mp[i][j]=0;
116                     q.push({get(),now.step+1});
117                     mp[i-2][j]=0,mp[i][j]=1;
118                 }
119                 if(mp[i][j]&&ok(i+1,j)&&mp[i+1][j]==1&&ok(i+2,j)&&mp[i+2][j]==0)
120                 {
121                     mp[i+2][j]=1,mp[i][j]=0;
122                     q.push({get(),now.step+1});
123                     mp[i+2][j]=0,mp[i][j]=1;
124                 }
125                 if(mp[i][j]&&ok(i,j-1)&&mp[i][j-1]==1&&ok(i,j-2)&&mp[i][j-2]==0)
126                 {
127                     mp[i][j-2]=1,mp[i][j]=0;
128                     q.push({get(),now.step+1});
129                     mp[i][j-2]=0,mp[i][j]=1;
130                 }
131                 if(mp[i][j]&&ok(i,j+1)&&mp[i][j+1]==1&&ok(i,j+2)&&mp[i][j+2]==0)
132                 {
133                     mp[i][j+2]=1,mp[i][j]=0;
134                     q.push({get(),now.step+1});
135                     mp[i][j+2]=0,mp[i][j]=1;
136                 }
137                 //--------------------------------------------------------------
138                 if(mp[i][j]==1&&ok(i-1,j)&&mp[i-1][j]==0)
139                 {
140                     mp[i-1][j]=1,mp[i][j]=0;
141                     q.push({get(),now.step+1});
142                     mp[i-1][j]=0;mp[i][j]=1;
143                 }
144                 if(mp[i][j]==1&&ok(i+1,j)&&mp[i+1][j]==0)
145                 {
146                     mp[i+1][j]=1,mp[i][j]=0;
147                     q.push({get(),now.step+1});
148                     mp[i+1][j]=0;mp[i][j]=1;
149                 }
150                 if(mp[i][j]==1&&ok(i,j-1)&&mp[i][j-1]==0)
151                 {
152                     mp[i][j-1]=1,mp[i][j]=0;
153                     q.push({get(),now.step+1});
154                     mp[i][j-1]=0;mp[i][j]=1;
155                 }
156                 if(mp[i][j]==1&&ok(i,j+1)&&mp[i][j+1]==0)
157                 {
158                     mp[i][j+1]=1,mp[i][j]=0;
159                     q.push({get(),now.step+1});
160                     mp[i][j+1]=0;mp[i][j]=1;
161                 }
162             }
163         }
164     }
165 }
166 
167 int main()
168 {
169     int x,y;
170     while(~sc("%d%d",&x,&y))
171     {
172         ans=0;
173         mark.clear();
174         mem(mp,0);
175         mp[x][y]=1;
176         for(int i=2;i<=4;++i)
177         {
178             sc("%d%d",&x,&y);
179             mp[x][y]=1;
180         }
181         bfs(get(),0);
182         mem(mp,0);
183         for(int i=1;i<=4;++i)
184         {
185             sc("%d%d",&x,&y);
186             mp[x][y]=1;
187         }
188         bfs(get(),1);
189         if(ans)
190             pr("YES\n");
191         else
192             pr("NO\n");
193     }
194     return 0;
195 }
196 
197 /**************************************************************************************/

 

HDU1401(双向BFS)

标签:eof   deque   empty   users   window   name   define   str   element   

原文地址:https://www.cnblogs.com/--HPY-7m/p/11991974.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!