zoukankan      html  css  js  c++  java
  • 『一本通』广搜的优化技巧

    打开灯泡

     1 #include<bits/stdc++.h>
     2 #define N 500+5
     3 using namespace std;
     4 int n,m,dis[N][N];
     5 char a[N][N];
     6 struct node{int X,Y;};
     7 deque<node>q;
     8 
     9 void Do(int x,int y,int s,int k) {
    10     if(dis[x][y]>s+k) {
    11         dis[x][y]=s+k;
    12         if(!k) q.push_front((node){x,y});
    13         else q.push_back((node){x,y});
    14     }
    15 }
    16 
    17 void BFS() {
    18     memset(dis,0x7f,sizeof(dis));
    19     q.push_front((node){1,1}); dis[1][1]=0;
    20     while(!q.empty()) {
    21         int x=q.front().X,y=q.front().Y;
    22         q.pop_front();
    23         if(x>1&&y>1) Do(x-1,y-1,dis[x][y],a[x-1][y-1]!='\');
    24         if(x>1&&y<=m) Do(x-1,y+1,dis[x][y],a[x-1][y]!='/');
    25         if(x<=n&&y<=m) Do(x+1,y+1,dis[x][y],a[x][y]!='\');
    26         if(x<=n&&y>1) Do(x+1,y-1,dis[x][y],a[x][y-1]!='/');
    27     }
    28     printf("%d",dis[n+1][m+1]);
    29 }
    30 
    31 int main() {
    32     scanf("%d%d",&n,&m);
    33     if((n+m)%2) return printf("NO SOLUTION"),0;
    34     for(int i=1;i<=n;i++) scanf("%s",a[i]+1);
    35     BFS(); //Breadth-First Search(广度优先搜索)
    36 }

    魔板

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 const int N=5e4+5,M=165e5;
     4 const int g[3][9]={{0,8,7,6,5,4,3,2,1},{0,4,1,2,3,6,7,8,5},{0,1,7,2,4,5,3,6,8}};
     5 int h,t=1,e,now,a[9],b[N][9],c[N][2];
     6 char s[N];
     7 bool vis[M];
     8 
     9 int main() {
    10     int x,top;    
    11     for(int i=1;i<=8;i++) scanf("%d",&x),e=(e<<3)+x-1; //Hash
    12     for(int i=1;i<=8;i++) now=(now<<3)+(b[1][i]=i-1);
    13     if(now==e) return puts("0"),0;
    14     vis[now]=1;     
    15     while((h++)<t) 
    16      for(int i=0;i<3;i++) {
    17         now=0; 
    18         for(int j=1;j<=8;j++) now=(now<<3)+(a[j]=b[h][g[i][j]]);
    19         if(vis[now]) continue;
    20         vis[now]=1;
    21         b[++t][0]=b[h][0]+1;
    22         for(int j=1;j<=8;j++) b[t][j]=a[j];
    23         c[t][0]=h,c[t][1]=i+'A';
    24         if(now==e) {
    25            printf("%d
    ",b[t][0]); 
    26            x=t,top=0;
    27            while(c[x][0]) s[++top]=c[x][1],x=c[x][0];
    28            for(int j=top;j>0;j--) putchar(s[j]);
    29            return 0;    
    30         }
    31      }
    32 }

    Knight Moves

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 const int dx[9]={2,2,1,1,-1,-1,-2,-2},dy[9]={-1,1,-2,2,-2,2,-1,1};
     4 int n,L,step[305][305];
     5 struct node{int x,y;}b,e; 
     6 queue<node>q;
     7 bool check(int x,int y) {return x>=0&&x<L&&y>=0&&y<L;}
     8 
     9 void BFS() {
    10     while(!q.empty()) q.pop();
    11     memset(step,0x7f,sizeof(step));
    12     q.push(b),step[b.x][b.y]=0;
    13     while(step[e.x][e.y]>1e3) {
    14         int X=q.front().x,Y=q.front().y; q.pop();
    15         for(int i=0;i<8;i++) {
    16             int nx=X+dx[i],ny=Y+dy[i];
    17             if(!check(nx,ny)||step[nx][ny]<1e3) continue;
    18             step[nx][ny]=step[X][Y]+1;
    19             q.push((node){nx,ny});
    20         }
    21     }
    22     printf("%d
    ",step[e.x][e.y]);
    23 }
    24 
    25 int main() {
    26     scanf("%d",&n);
    27     while(n--) {
    28         scanf("%d%d%d%d%d",&L,&b.x,&b.y,&e.x,&e.y);
    29         if(b.x==e.x&&b.y==e.y) puts("0"); 
    30         else BFS(); //Breadth-First Search(广度优先搜索)
    31     }
    32 }
  • 相关阅读:
    sqlserver之高灵活的业务单据流水号生成
    Putty的ppk格式密钥在linux与mac上无法支持
    矩形圆角调整
    新的旅途
    AndroidRichText 让Textview轻松的支持富文本(图像ImageSpan、点击效果等等类似QQ微信聊天)
    轻量级的惰性控件——ViewStub
    Android的WebView通过JS调用java代码
    对TextVIew中特定字符串设定onTouchEvent方法
    同步任务 AsyncTask 介绍
    自定义可点击的ImageSpan并在TextView中内置“View“
  • 原文地址:https://www.cnblogs.com/qq8260573/p/10340152.html
Copyright © 2011-2022 走看看