zoukankan      html  css  js  c++  java
  • hdu1026Ignatius and the Princess I(广搜记录路径的优先队列)

    题目需要对路径进行打表,记录路径很关键,从网上找的,基本都是通过记录当前点的来源方向来处理路径问题。

    自己写的不怎么滴,来自网上http://blog.csdn.net/ice_crazy/article/details/7763302博客

    这道题加深了对广搜优先队列的理解,很不错的一道题

      1 #include<stdio.h>
      2 #include<string.h>
      3 #include<queue>
      4 using namespace std;
      5 
      6 struct node 
      7 {
      8     int x,y;
      9     int step;
     10     friend bool operator<(node n1,node n2)
     11     {
     12         return n2.step<n1.step;
     13     }
     14 };
     15 int dir[4][2]={0,1,1,0,0,-1,-1,0};
     16 int map[111][111];
     17 int flag[111][111];
     18 int c[111][111];
     19 int n,m;
     20 
     21 
     22 int judge(int x,int y)
     23 {
     24     if(x<0||x>=n||y<0||y>=m)
     25         return 1;
     26     if(map[x][y]==-1)
     27         return 1;
     28     return 0;
     29 }
     30 
     31 int BFS()
     32 {
     33     priority_queue<node>q;
     34     node cur,next;
     35     int i;
     36     cur.x=0;
     37     cur.y=0;
     38     cur.step=0;
     39     map[0][0]=-1;
     40     q.push(cur);
     41     while(!q.empty())
     42     {
     43         cur=q.top();
     44         q.pop();
     45         if(cur.x==n-1&&cur.y==m-1)
     46             return cur.step;
     47         for(i=0;i<4;i++)
     48         {
     49             next.x=cur.x+dir[i][0];
     50             next.y=cur.y+dir[i][1];
     51             if(judge(next.x,next.y))
     52                 continue;
     53             next.step=cur.step+1+map[next.x][next.y];
     54             flag[next.x][next.y]=i+1;
     55             map[next.x][next.y]=-1;
     56             q.push(next);
     57         }
     58     }
     59     return -1;
     60 }
     61 
     62 int temp;
     63 void fun(int x,int y)
     64 {
     65     int a,b;
     66     if(flag[x][y]==0)
     67         return;
     68     a=x-dir[flag[x][y]-1][0];
     69     b=y-dir[flag[x][y]-1][1];
     70     fun(a,b);
     71     printf("%ds:(%d,%d)->(%d,%d)
    ",temp++,a,b,x,y); 
     72     while(c[x][y]--)    printf("%ds:FIGHT AT (%d,%d)
    ",temp++,x,y);
     73 }
     74 int main()
     75 {
     76     char str[111];
     77     int i,j;
     78     int ans;
     79     while(scanf("%d%d",&n,&m)!=-1)
     80     {
     81         memset(map,0,sizeof(map));
     82         memset(flag,0,sizeof(flag));
     83         memset(c,0,sizeof(c));
     84         for(i=0;i<n;i++)
     85         {
     86             scanf("%s",str);
     87             for(j=0;str[j];j++)
     88             {
     89                 if(str[j]=='.')
     90                     map[i][j]=0;
     91                 else if(str[j]=='X')
     92                     map[i][j]=-1;
     93                 else
     94                     map[i][j]=c[i][j]=str[j]-'0';
     95             }
     96         }
     97         ans=BFS();
     98         if(ans==-1)
     99             printf("God please help our poor hero.
    "); 
    100         else
    101         {
    102             printf("It takes %d seconds to reach the target position, let me show you the way.
    ",ans);
    103             temp=1;
    104             fun(n-1,m-1);
    105         }
    106         printf("FINISH
    ");  
    107     }
    108     return 0;
    109 }
    View Code
  • 相关阅读:
    HDU 3280 Equal Sum Partitions(二分查找)
    提高效率—编程中的技巧
    开发心得
    使用OpenCV读、操作、写图像并与bash合作对某个文件夹下全部图像进行相似处理
    观察者模式
    ORA-00932: inconsistent datatypes: expected
    Struts1应用、实现简单计算器、使用DispatchAction、显示友好的报错信息、使用动态Form简化开发
    Apple Swift编程语言新手教程
    I NEED A OFFER! (hdu 1203 01背包)
    端午遥想
  • 原文地址:https://www.cnblogs.com/zlyblog/p/3189100.html
Copyright © 2011-2022 走看看