HDU - 1401 Solitaire

从起点和终点分别搜,找是否存在8步内共同经过的点

这样就可以把暴力的时间复杂度开根号

  1 #include<map>
  2 #include<queue>
  3 #include<cstdio>
  4 #include<cstring>
  5 #include<algorithm>
  6 using namespace std;
  7 int n,m,cnt,ans,tim,d1,d2,d3;
  8 int dx[4]={1,0,-1,0};
  9 int dy[4]={0,1,0,-1};
 10 int mp[11][11];
 11 struct node{
 12     int x[4];
 13     int y[4];
 14 }st,ed,fla;
 15 long long make_order(node a){
 16     int tmp[4]={0,0,0,0};
 17     long long ret=0;long long mul=100;
 18     for(int i=0;i<4;i++){
 19         tmp[i]=(a.x[i]-1)*8+a.y[i];
 20     }
 21     sort(tmp,tmp+4);
 22     for(int i=3;i>=0;i--){
 23         ret=1ll*ret*mul+tmp[i];
 24     }
 25     return ret;
 26 }
 27 bool bfs(){
 28     queue<node>que1;
 29     queue<node>que2;
 30     map<long long,int>dis1;
 31     map<long long,int>dis2;
 32     que1.push(st);
 33     que2.push(ed);
 34     dis1[make_order(st)]=0;dis2[make_order(ed)]=0;
 35     while(1){
 36         node s=que1.front();
 37         que1.pop();
 38         if(dis1[make_order(s)]>4)break;
 39         for(int i=0;i<4;i++){
 40             mp[s.x[i]][s.y[i]]=1;
 41         }
 42         for(int i=0;i<4;i++){
 43             for(int j=0;j<4;j++){
 44                 int fl=0;
 45                 int xx=s.x[i]+dx[j];
 46                 int yy=s.y[i]+dy[j];
 47                 while(mp[xx][yy]){
 48                     xx+=dx[j];
 49                     yy+=dy[j];
 50                     fl++;
 51                 }
 52                 if(xx>8||xx<1||yy>8||yy<1)continue;
 53                 node to=s;to.x[i]=xx,to.y[i]=yy;
 54                 if(dis1.find(make_order(to))==dis1.end() && fl<=1){
 55                     dis1[make_order(to)]=dis1[make_order(s)]+1;
 56                     que1.push(to);
 57                     if(dis2.find(make_order(to))!=dis2.end()){
 58                         tim=dis1[make_order(to)]+dis2[make_order(to)];
 59                         if(tim<=8)return true;
 60                     }
 61                 }
 62             }
 63         }
 64         for(int i=0;i<4;i++){
 65             mp[s.x[i]][s.y[i]]=0;
 66         }
 67         s=que2.front();
 68         que2.pop();
 69         if(dis2[make_order(s)]>4)break;
 70         for(int i=0;i<4;i++){
 71             mp[s.x[i]][s.y[i]]=1;
 72         }
 73         for(int i=0;i<4;i++){
 74             for(int j=0;j<4;j++){
 75                 int fl=0;
 76                 int xx=s.x[i]+dx[j];
 77                 int yy=s.y[i]+dy[j];
 78                 while(mp[xx][yy]){
 79                     xx+=dx[j];
 80                     yy+=dy[j];
 81                     fl++;
 82                 }
 83                 if(xx>8||xx<1||yy>8||yy<1)continue;
 84                 node to=s;to.x[i]=xx,to.y[i]=yy;
 85                 if(dis2.find(make_order(to))==dis2.end() && fl<=1){
 86                      dis2[make_order(to)]=dis2[make_order(s)]+1;
 87                     que2.push(to);
 88                     if(dis1.find(make_order(to))!=dis1.end()){
 89                         tim=dis1[make_order(to)]+dis2[make_order(to)];
 90                         if(tim<=8)return true;
 91                     }
 92                 }
 93             }
 94         }
 95         for(int i=0;i<4;i++){
 96             mp[s.x[i]][s.y[i]]=0;
 97         }
 98     }
 99     return false;
100 }
101 int main(){
102     while(scanf("%d%d",&st.x[0],&st.y[0])>0){
103         for(int i=1;i<4;i++){
104             scanf("%d%d",&st.x[i],&st.y[i]);
105         }
106         for(int i=0;i<4;i++){
107             scanf("%d%d",&ed.x[i],&ed.y[i]);
108         }
109         if(make_order(st)==make_order(ed)){
110             printf("YES\n");
111             continue;
112         }
113         if(bfs()){
114             printf("YES\n");
115         }
116         else printf("NO\n");
117     }
118     return 0;
119 }

 

posted @ 2018-11-22 19:19  Mr_Handsome  阅读(197)  评论(0编辑  收藏  举报