HDU 1401 solitaire bidirectional BFS

Source: Internet
Author: User

Reprint please indicate the source, thank you http://blog.csdn.net/ACM_cxlove? Viewmode = contents by --- cxlove

The first write bidirectional BFs.

Bidirectional BFS searches from the start and end points. If the intersection points appear, the target point is reachable. This requirement is to arrive within eight steps. Each step has 16 states, and the complexity of 16 ^ 8 is still very high. Because the start and end points are determined, it is very suitable for Bidirectional BFs, that is, you can search for four steps from the start point and the end point respectively. If an intersection occurs, the search is reachable.

Hash is compressed in hexadecimal notation. Each coordinate range is 0-7 and can be expressed in three binary notation. A total of 24 binary notation is used. In hash notation, sort the coordinates. At the beginning, the result of flag [1 <24] is still MLE. Later, I changed it to map.

Although it is written as a BFs, the middle part is written very well.

#include<iostream>#include<cstdio>#include<cstring>#include<queue>#include<cmath>#include<string>#include<vector>#include<algorithm>#include<map>#include<set>#define inf 1<<30#define LL long long#define maxn 1<<24using namespace std;struct Point{int x,y;bool check(){if(x<8&&x>=0&&y<8&&y>=0)return true;return false;}};struct Node{Point pos[4];int step;}s,e;map<int,int>m;map<int,int>::iterator it;int way[4][2]={{0,1},{0,-1},{1,0},{-1,0}};bool cmp(Point n1,Point n2){return n1.x!=n2.x?n1.x<n2.x:n1.y<n2.y;}int get_hash(Point *tmp){int res=0;sort(tmp,tmp+4,cmp);for(int i=0;i<4;i++){res|=(tmp[i].x<<(6*i));res|=(tmp[i].y<<(6*i+3));}return res;}bool bfs(int kind,Node u){Node v;queue<Node>que;u.step=0;if(kind==2){it=m.find(get_hash(u.pos));if(it!=m.end())return true;}m[get_hash(u.pos)]=kind;que.push(u);while(!que.empty()){u=que.front();que.pop();if(u.step>=4)continue;for(int i=0;i<4;i++){for(int j=0;j<4;j++){v=u;v.step++;v.pos[j].x+=way[i][0];v.pos[j].y+=way[i][1];int k;if(!v.pos[j].check())continue;for(k=0;k<4;k++)if(k!=j&&v.pos[k].x==v.pos[j].x&&v.pos[k].y==v.pos[j].y)break;if(k==4){int HASH=get_hash(v.pos);it=m.find(HASH);if(kind==1){if(it==m.end()){m[HASH]=1;que.push(v);}}else{if(it==m.end()){m[HASH]=2;que.push(v);}else if((*it).second==1)return true;}}else{v.pos[j].x+=way[i][0];v.pos[j].y+=way[i][1];if(!v.pos[j].check())continue;for(k=0;k<4;k++)if(k!=j&&v.pos[k].x==v.pos[j].x&&v.pos[k].y==v.pos[j].y)break;if(k==4){int HASH=get_hash(v.pos);it=m.find(HASH);if(kind==1){if(it==m.end()){m[HASH]=1;que.push(v);}}else{if(it==m.end()){m[HASH]=2;que.push(v);}else if((*it).second==1)return true;}}}}}}return false;}int main(){while(scanf("%d%d",&s.pos[0].x,&s.pos[0].y)!=EOF){m.clear();for(int i=1;i<4;i++)scanf("%d%d",&s.pos[i].x,&s.pos[i].y);for(int i=0;i<4;i++)scanf("%d%d",&e.pos[i].x,&e.pos[i].y);for(int i=0;i<4;i++){s.pos[i].x--;s.pos[i].y--;e.pos[i].x--;e.pos[i].y--;}bfs(1,s);printf("%s\n",bfs(2,e)?"YES":"NO");}return 0;}/*2 1 8 8 7 8 1 11 5 8 7 7 7 2 46 5 5 7 5 1 7 17 7 6 5 5 7 6 34 6 5 6 5 5 5 22 2 7 6 5 3 5 24 4 5 5 3 4 7 22 7 7 2 6 5 4 84 2 3 4 5 5 6 62 7 4 4 3 5 4 53 3 5 3 6 2 6 66 3 8 3 6 6 6 83 2 5 3 6 2 6 66 3 8 3 6 6 6 83 4 4 5 5 5 7 53 3 4 3 5 3 6 3*/

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.