• bfs


    拯救大兵瑞恩 http://acm.hdu.edu.cn/showproblem.php?pid=4845

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<queue>
     4 #define mt(a,b) memset(a,b,sizeof(a))
     5 using namespace std;
     6 const int M=16;
     7 int mp[M][M][M][M];
     8 int key[M][M];
     9 bool vis[M][M][1<<11];
    10 int n,m;
    11 struct Q{
    12     int x,y,step,mykey;
    13 }now,pre;
    14 queue<Q> q;
    15 int dx[]={0,0,1,-1};
    16 int dy[]={1,-1,0,0};
    17 int bfs(){
    18     now.x=1;
    19     now.y=1;
    20     now.step=0;
    21     now.mykey=key[1][1];
    22     mt(vis,0);
    23     vis[1][1][now.mykey]=true;
    24     while(!q.empty()) q.pop();
    25     q.push(now);
    26     while(!q.empty()){
    27         pre=q.front();
    28         q.pop();
    29         if(pre.x==n&&pre.y==m) return pre.step;
    30         for(int i=0;i<4;i++){
    31             int tx=pre.x+dx[i];
    32             int ty=pre.y+dy[i];
    33             if(tx>=1&&tx<=n&&ty>=1&&ty<=m){
    34                 int need=mp[pre.x][pre.y][tx][ty];
    35                 if(need==-1||((pre.mykey>>need)&1)){
    36                     now.x=tx;
    37                     now.y=ty;
    38                     now.step=pre.step+1;
    39                     now.mykey=pre.mykey|key[tx][ty];
    40                     if(!vis[tx][ty][now.mykey]){
    41                         vis[tx][ty][now.mykey]=true;
    42                         q.push(now);
    43                     }
    44                 }
    45             }
    46         }
    47     }
    48     return -1;
    49 }
    50 int main(){
    51     int p,x1,y1,x2,y2,op;;
    52     while(~scanf("%d%d%d",&n,&m,&p)){
    53         scanf("%d",&p);
    54         mt(mp,-1);
    55         while(p--){
    56             scanf("%d%d%d%d%d",&x1,&y1,&x2,&y2,&op);
    57             mp[x1][y1][x2][y2]=op;
    58             mp[x2][y2][x1][y1]=op;
    59         }
    60         scanf("%d",&p);
    61         mt(key,0);
    62         while(p--){
    63             scanf("%d%d%d",&x1,&y1,&op);
    64             key[x1][y1]|=(1<<op);
    65         }
    66         printf("%d
    ",bfs());
    67     }
    68     return 0;
    69 }
    View Code

     Catch That Cow http://poj.org/problem?id=3278

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<queue>
     4 #define mt(a,b) memset(a,b,sizeof(a))
     5 using namespace std;
     6 const int M=100010;
     7 int n,k;
     8 bool vis[M];
     9 struct G{
    10     int val,step;
    11 }now,pre;
    12 queue<G> q;
    13 void add(){
    14     if(now.val>=0&&now.val<M){
    15         if(!vis[now.val]){
    16             vis[now.val]=true;
    17             q.push(now);
    18         }
    19     }
    20 }
    21 int bfs(){
    22     now.val=n;
    23     now.step=0;
    24     mt(vis,0);
    25     vis[n]=true;
    26     while(!q.empty()) q.pop();
    27     q.push(now);
    28     while(!q.empty()){
    29         pre=q.front();
    30         q.pop();
    31         if(pre.val==k) return pre.step;
    32         now.step=pre.step+1;
    33         now.val=pre.val+1;
    34         add();
    35         now.val=pre.val-1;
    36         add();
    37         now.val=pre.val<<1;
    38         add();
    39     }
    40 }
    41 int main(){
    42     while(~scanf("%d%d",&n,&k)){
    43         printf("%d
    ",bfs());
    44     }
    45     return 0;
    46 }
    View Code

     迷宫问题 http://poj.org/problem?id=3984

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<queue>
     4 #define mt(a,b) memset(a,b,sizeof(a))
     5 using namespace std;
     6 const int M=8;
     7 int a[M][M],fa[M][M];
     8 bool vis[M][M];
     9 struct G{
    10     int x,y;
    11 }now,pre;
    12 queue<G> q;
    13 int dx[]={0,0,1,-1};
    14 int dy[]={1,-1,0,0};
    15 bool judge(int x){
    16     if(x>=0&&x<5) return true;return false;
    17 }
    18 void bfs(){
    19     mt(fa,-1);
    20     mt(vis,0);
    21     vis[0][0]=true;
    22     now.x=0;
    23     now.y=0;
    24     while(!q.empty()) q.pop();
    25     q.push(now);
    26     while(!q.empty()){
    27         pre=q.front();
    28         q.pop();
    29         if(pre.x==4&&pre.y==4) return ;
    30         for(int i=0;i<4;i++){
    31             int tx=pre.x+dx[i];
    32             int ty=pre.y+dy[i];
    33             if(judge(tx)&&judge(ty)&&!vis[tx][ty]&&!a[tx][ty]){
    34                 vis[tx][ty]=true;
    35                 fa[tx][ty]=i;
    36                 now.x=tx;
    37                 now.y=ty;
    38                 q.push(now);
    39             }
    40         }
    41     }
    42 }
    43 struct OUT{
    44     int x,y;
    45 }out[M*M];
    46 int main(){
    47     for(int i=0;i<5;i++){
    48         for(int j=0;j<5;j++){
    49             scanf("%d",&a[i][j]);
    50         }
    51     }
    52     bfs();
    53     int tx=4,ty=4,lo=0;
    54     while(~fa[tx][ty]){
    55         out[lo].x=tx;
    56         out[lo].y=ty;
    57         lo++;
    58         int i=fa[tx][ty];
    59         tx-=dx[i];
    60         ty-=dy[i];
    61     }
    62     out[lo].x=0;
    63     out[lo].y=0;
    64     lo++;
    65     for(int i=lo-1;i>=0;i--){
    66         printf("(%d, %d)
    ",out[i].x,out[i].y);
    67     }
    68     return 0;
    69 }
    View Code

    Red and Black http://acm.hdu.edu.cn/showproblem.php?pid=1312

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<queue>
     4 #define mt(a,b) memset(a,b,sizeof(a))
     5 using namespace std;
     6 const int M=32;
     7 int n,m,sx,sy;
     8 char a[M][M];
     9 bool vis[M][M];
    10 struct Q{
    11     int x,y;
    12 }now,pre;
    13 queue<Q> q;
    14 int dx[]={0,0,1,-1};
    15 int dy[]={1,-1,0,0};
    16 int bfs(){
    17     mt(vis,0);
    18     vis[sx][sy]=true;
    19     now.x=sx;
    20     now.y=sy;
    21     while(!q.empty()) q.pop();
    22     q.push(now);
    23     while(!q.empty()){
    24         pre=q.front();
    25         q.pop();
    26         for(int i=0;i<4;i++){
    27             int tx=pre.x+dx[i];
    28             int ty=pre.y+dy[i];
    29             if(tx>=0&&tx<n&&ty>=0&&ty<m&&!vis[tx][ty]&&a[tx][ty]!='#'){
    30                 vis[tx][ty]=true;
    31                 now.x=tx;
    32                 now.y=ty;
    33                 q.push(now);
    34             }
    35         }
    36     }
    37     int res=0;
    38     for(int i=0;i<n;i++){
    39         for(int j=0;j<m;j++){
    40             if(vis[i][j]) res++;
    41         }
    42     }
    43     return res;
    44 }
    45 int main(){
    46     while(~scanf("%d%d",&m,&n),m|n){
    47         for(int i=0;i<n;i++){
    48             scanf("%s",a[i]);
    49             for(int j=0;j<m;j++){
    50                 if(a[i][j]=='@'){
    51                     sx=i;
    52                     sy=j;
    53                 }
    54             }
    55         }
    56         printf("%d
    ",bfs());
    57     }
    58     return 0;
    59 }
    View Code

    Pet http://acm.hdu.edu.cn/showproblem.php?pid=4707

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<queue>
     4 #define mt(a,b) memset(a,b,sizeof(a))
     5 using namespace std;
     6 const int M=100010;
     7 struct G{
     8     struct E{
     9         int v,next;
    10     }e[M<<1];
    11     int le,head[M];
    12     void init(){
    13         le=0;
    14         mt(head,-1);
    15     }
    16     void add(int u,int v){
    17         e[le].v=v;
    18         e[le].next=head[u];
    19         head[u]=le++;
    20     }
    21 }g;
    22 struct Q{
    23     int id,step;
    24 }now,pre;
    25 queue<Q> q;
    26 int D,n,t;
    27 bool vis[M];
    28 int bfs(){
    29     int res=0;
    30     mt(vis,0);
    31     vis[0]=true;
    32     now.id=0;
    33     now.step=0;
    34     while(!q.empty()) q.pop();
    35     q.push(now);
    36     while(!q.empty()){
    37         pre=q.front();
    38         q.pop();
    39         if(pre.step<=D) res++;
    40         if(pre.step==D) continue;
    41         for(int i=g.head[pre.id];~i;i=g.e[i].next){
    42             int v=g.e[i].v;
    43             if(!vis[v]){
    44                 vis[v]=true;
    45                 now.id=v;
    46                 now.step=pre.step+1;
    47                 q.push(now);
    48             }
    49         }
    50     }
    51     return n-res;
    52 }
    53 int main(){
    54     while(~scanf("%d",&t)){
    55         while(t--){
    56             scanf("%d%d",&n,&D);
    57             g.init();
    58             for(int i=0;i<n-1;i++){
    59                 int u,v;
    60                 scanf("%d%d",&u,&v);
    61                 g.add(u,v);
    62                 g.add(v,u);
    63             }
    64             printf("%d
    ",bfs());
    65         }
    66     }
    67     return 0;
    68 }
    View Code

    end

  • 相关阅读:
    商人的诀窍
    商人小鑫
    懒虫小鑫
    悼念512汶川大地震遇难同胞
    最少拦截系统
    活动选择问题
    删数问题
    常用排序算法的时间和空间复杂度总结
    一些博主
    C中自己的memcpy
  • 原文地址:https://www.cnblogs.com/gaolzzxin/p/3884674.html
Copyright © 2020-2023  润新知