zoukankan      html  css  js  c++  java
  • 称不上算法的算法-3.bfs

    1.HRBUST1613

    待优化

    注意换行对于char读取的影响!!!

    bfs大体步骤:建立记录地图,确定起点,起点入队,以队列不空为条件循化,在循环内不断把队列的首元素当成操作元素,并把此操作元素的所有下层可能结果入队。

    这样循环往复就可以把每层搜尽。

     1 #include<stdio.h>
     2 #include<string.h>
     3 #include<queue>
     4 using namespace std;
     5 char map[110][110];
     6 struct point{
     7     int x;
     8     int y;
     9 };
    10 int xmove[4]={0,0,-1,1};
    11 int ymove[4]={1,-1,0,0};
    12 int times[110][110];
    13 point P[10000];
    14 int main(void){
    15     int t;
    16     scanf("%d",&t);
    17     for(int tt=0;tt<t;tt++){
    18         point start;
    19         int sumofp=0;
    20         int judg=0;
    21         memset(times,0,sizeof(times));
    22         int R,C;
    23         scanf("%d%d",&R,&C);
    24         getchar();
    25         for(int i=0;i<R;i++){
    26             for(int j=0;j<C;j++){
    27                 scanf("%c",&map[i][j]);
    28                 if(map[i][j]=='Z'){
    29                     start.x=i;
    30                     start.y=j;
    31                 }
    32                 else if(map[i][j]=='P'){
    33                     point tmp;
    34                     tmp.x=i;
    35                     tmp.y=j;
    36                     P[sumofp]=tmp;
    37                     sumofp++;
    38                 }
    39             }
    40             getchar();
    41         }
    42         queue<point> que;
    43         que.push(start);
    44         while(!que.empty()){
    45             point atmp=que.front();
    46             que.pop();
    47             for(int g=0;g<4;g++){
    48                 point nowoption;
    49                 nowoption.x=atmp.x+xmove[g];
    50                 nowoption.y=atmp.y+ymove[g];
    51                 if(nowoption.x>=0&& nowoption.x<R&& nowoption.y>=0&& nowoption.y<C&& map[nowoption.x][nowoption.y]!='#'&&times[nowoption.x][nowoption.y]==0){
    52                     if(map[nowoption.x][nowoption.y]=='W'){
    53                         judg=1;
    54                         printf("%d
    ",times[atmp.x][atmp.y]+1);
    55                         break;
    56                     }
    57                     else if(map[nowoption.x][nowoption.y]=='.'){
    58                         que.push(nowoption);
    59                         times[nowoption.x][nowoption.y]=times[atmp.x][atmp.y]+1;
    60                     }
    61                     else if(map[nowoption.x][nowoption.y]=='P'){
    62                         for(int j=0;j<sumofp;j++){
    63                             que.push(P[j]);
    64                             times[P[j].x][P[j].y]=times[atmp.x][atmp.y]+1;
    65                         }
    66                     }
    67                 }
    68             }
    69             if(judg==1)
    70                 break;
    71         }
    72         if(judg==0){
    73             printf("IMPOSSIBLE
    ");
    74         }
    75     }
    76 }

     2.HRBUST1621(时间最短)

    使用优先队列,时间短优先

     1 #include<stdio.h>
     2 #include<string.h>
     3 #include<queue>
     4 using namespace std;
     5 char map[220][220];
     6 int times[220][220];
     7 int xmove[4]={0,0,1,-1};
     8 int ymove[4]={1,-1,0,0};
     9 struct point{
    10     int x,y;
    11     int per_group;
    12     bool operator< (const point&a)const{
    13         return times[a.x][a.y]<times[x][y];
    14     }
    15 };
    16 int main(void){
    17     int T;
    18     scanf("%d",&T);
    19     for(int tt=0;tt<T;tt++){
    20         memset(times,0,sizeof(times));
    21         point start;
    22         point endp;
    23         int R,C;
    24         scanf("%d%d",&R,&C);
    25         getchar();
    26         for(int i=0;i<R;i++){
    27             for(int j=0;j<C;j++){
    28                 scanf("%c",&map[i][j]);
    29                 if(map[i][j]=='Z'){
    30                     start.x=i;
    31                     start.y=j;
    32                 }
    33                 if(map[i][j]=='W'){
    34                     endp.x=i;
    35                     endp.y=j;
    36                 }
    37             }
    38             getchar();
    39         }
    40         priority_queue<point> que;
    41         que.push(start);
    42         while(!que.empty()){
    43             point nowpoint=que.top();
    44             que.pop();
    45             for(int i=0;i<4;i++){
    46                 point tmp;
    47                 tmp.x=nowpoint.x+xmove[i];
    48                 tmp.y=nowpoint.y+ymove[i];
    49                 if(tmp.x>=0&&tmp.x<R&&tmp.y>=0&&tmp.y<C&&map[tmp.x][tmp.y]!='#'&&times[tmp.x][tmp.y]==0){
    50                     if(map[tmp.x][tmp.y]=='W'){
    51                         times[tmp.x][tmp.y]=times[nowpoint.x][nowpoint.y]+1;
    52                         break;
    53                     }
    54                     if(map[tmp.x][tmp.y]=='.'){
    55                         times[tmp.x][tmp.y]=times[nowpoint.x][nowpoint.y]+1;
    56                     }
    57                     else{
    58                         times[tmp.x][tmp.y]=times[nowpoint.x][nowpoint.y]+1+map[tmp.x][tmp.y]-'0';
    59                     }
    60                     que.push(tmp);
    61                 }
    62             }
    63         }
    64         if(times[endp.x][endp.y]==0)
    65             printf("IMPOSSIBLE
    ");
    66         else
    67             printf("%d
    ",times[endp.x][endp.y]);
    68     }
    69 }

     3.HDU1026(时间最短+打印路径)

    打印路径用递归

     1 #include<stdio.h>
     2 #include<queue>
     3 #include<string.h>
     4 char themap[150][150];
     5 int xmove[4]={0,0,1,-1};
     6 int ymove[4]={1,-1,0,0};
     7 int dir[150][150];
     8 int ti[150][150];
     9 int n,m,sum;
    10 struct node{
    11     int x,y;
    12     int times;
    13     friend bool operator <(node a,node b){
    14         return a.times>b.times;
    15     }
    16 };
    17 using namespace std;
    18 void show(int x,int y){
    19     if(x==0&&y==0)
    20         return;
    21     show(x-xmove[dir[x][y]],y-ymove[dir[x][y]]);
    22     printf("%ds:(%d,%d)->(%d,%d)
    ",sum,x-xmove[dir[x][y]],y-ymove[dir[x][y]],x,y);
    23     sum++;
    24     while(ti[x][y]!=0){
    25         printf("%ds:FIGHT AT (%d,%d)
    ",sum,x,y);
    26         sum++;
    27         ti[x][y]--;
    28     }
    29 }
    30 int main(void){
    31     while(scanf("%d%d",&n,&m)==2){
    32         int judg=0;
    33         sum=1;
    34         getchar();
    35         memset(ti,0,sizeof(ti));
    36         memset(themap,-1,sizeof(themap));
    37         memset(dir,-1,sizeof(dir));
    38         for(int i=0;i<n;i++){
    39             for(int j=0;j<m;j++){
    40                 scanf("%c",&themap[i][j]);
    41             }
    42             getchar();
    43         }
    44         node start;
    45         start.x=0;
    46         start.y=0;
    47         start.times=0;
    48         themap[0][0]='X';
    49         priority_queue<node> que;
    50         que.push(start);
    51         while(!que.empty()){
    52             node nownode;
    53             nownode=que.top();
    54             que.pop();
    55             if(nownode.x==n-1&&nownode.y==m-1){
    56                 judg=1;
    57                 printf("It takes %d seconds to reach the target position, let me show you the way.
    ",nownode.times);
    58                 break;
    59             }
    60             for(int i=0;i<4;i++){
    61                 node tmp;
    62                 tmp.x=nownode.x+xmove[i];
    63                 tmp.y=nownode.y+ymove[i];
    64                 tmp.times=nownode.times+1;
    65                 if(tmp.x>=0&&tmp.x<n&&tmp.y>=0&&tmp.y<m&&themap[tmp.x][tmp.y]!='X'){
    66                     if(themap[tmp.x][tmp.y]=='.'){
    67                         themap[tmp.x][tmp.y]='X';
    68                     }
    69                     else{
    70                         tmp.times+=themap[tmp.x][tmp.y]-'0';
    71                         ti[tmp.x][tmp.y]=themap[tmp.x][tmp.y]-'0';
    72                         themap[tmp.x][tmp.y]='X';
    73                     }
    74                     dir[tmp.x][tmp.y]=i;
    75                     que.push(tmp);
    76                 }
    77             }
    78         }
    79         if(judg==0){
    80             printf("God please help our poor hero.
    ");
    81         }
    82         else{
    83             show(n-1,m-1);
    84         }
    85         printf("FINISH
    ");
    86     }
    87 }
  • 相关阅读:
    jQuery1.3.2 源码学习 2 两个重要的正则表达式
    学习 jQuery 4 使用方法选择
    学习 jQuery 6 在 TreeView 中实现全选
    jQuery1.3.2 源码学习4 init 函数分析 2
    学习 jQuery 3 选择器
    学习 jQuery 5 筛选和过滤器
    条款9:在删除选项中仔细选择
    优化3D图形流水线
    指针相减
    浅谈水体的实现
  • 原文地址:https://www.cnblogs.com/liuzey/p/8965206.html
Copyright © 2011-2022 走看看