觉得浮夸了四年,漠然发现原来是浮躁了四年!

hdu bfs problems

hdu 2425  Hiking Trip     Problem link adress:http://acm.hdu.edu.cn/showproblem.php?pid=2425

A simple bfs problem!

Notice: If " There is a blank line after each test case."  appeared in the INPUT format, we needn't output one blank more after each test case;

复制代码
View Code
 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 #include<queue>
 5 using namespace std;
 6 int n,m;
 7 char map[25][25];
 8 int p,s,t;
 9 int x1,y1,x2,y2;
10 struct node{
11     friend bool operator <(node a,node b)
12     {
13         return a.time>b.time;
14     };
15     int x,y,time;
16 };
17 int dir[4][2]={0,1,1,0,0,-1,-1,0};
18 int bfs()
19 {
20     node cur,next;
21     priority_queue<node>q;
22     int x,y,i;
23     cur.x=x1;cur.y=y1;cur.time=0;
24     q.push(cur);
25     map[x1][y1]='@';
26     while(!q.empty())
27     {
28         cur=q.top();
29         q.pop();
30         if(cur.x==x2&&cur.y==y2)
31             return  cur.time;
32         for(i=0;i<4;i++)
33         {
34             next.x=x=cur.x+dir[i][0];
35             next.y=y=cur.y+dir[i][1];
36             if(x>=0&&x<m&&y>=0&&y<n&&map[x][y]!='@')
37             {
38                 if(map[x][y]=='#')
39                     next.time=cur.time+p;
40                 if(map[x][y]=='.')
41                     next.time=cur.time+s;
42                 if(map[x][y]=='T')
43                     next.time=cur.time+t;
44                 q.push(next);
45                 map[x][y]='@';
46             }
47         }
48     }
49     return-1;
50 }
51 
52 
53                     
54 
55 int main()
56 {
57     int i;
58     int flag=1;
59     while(cin>>m>>n)
60     {
61         cin>>p>>s>>t;
62         getchar();
63         for(i=0;i<m;i++)
64             scanf("%s",map[i]);
65         cin>>x1>>y1>>x2>>y2;
66         if(x1==x2&&y1==y2)
67         {
68            printf("Case %d: 0\n",flag++);
69         }
70         else
71         {
72             int ans=bfs();
73             if(ans==-1)   printf("Case %d: -1\n",flag++);
74             else
75                 printf("Case %d: %d\n",flag++,ans);
76         }
77     
78     }
79     return 0;
80 }
复制代码

hdu 4308  Saving Princess claire_        Problem link adress http://acm.hdu.edu.cn/showproblem.php?pid=4308

When the problem is accepted, I want to cry!  I failed 20 more times before I succeed!

And I learn much from the problem!

no 1: must calm down in you mind when you are solving the problem

no 2: when you donn't konw how to solve the problem,you can learn the ways to solve the problem from other's blog,but never write the code follow other's;

no 3: when  failed in the problem again and again, set aside the problem, but don't forget it!

复制代码
View Code
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<queue>
 4 #include<cstring>
 5 using namespace std;
 6 int n,m,c;
 7 int k;
 8 int x1,y1;
 9 int a[505],b[505];
10 char map[5002][5002];
11 struct node{
12     int x,y,money;
13     bool friend operator<(node a,node b)
14     {
15         return a.money>b.money;
16     };
17 };
18 int dir[4][2]={0,1,1,0,0,-1,-1,0};
19 int bfs()
20 {
21     node cur,next;
22     priority_queue<node>q;
23     cur.x=x1;cur.y=y1;
24     cur.money=0;
25     q.push(cur);
26     map[x1][y1]='#';
27     int x,y,i;
28     while(!q.empty())
29     {
30         cur=q.top();
31         q.pop();
32         for(i=0;i<4;i++)
33         {
34             next.x=x=cur.x+dir[i][0];
35             next.y=y=cur.y+dir[i][1];
36             if(x>=0&&x<m&&y>=0&&y<n&&map[x][y]!='#')
37             {
38                 if(map[x][y]=='C')  return  cur.money;
39                 if(map[x][y]=='*')
40                 {
41                     next.money=cur.money+c;
42                     q.push(next);
43                     map[x][y]='#';
44                 }
45                 if(map[x][y]=='P')
46                 {
47                     int num;
48                     for(num=0;num<k;num++)
49                     {
50                         next.x=a[num];
51                         next.y=b[num];
52                         next.money=cur.money;
53                         q.push(next);
54                         map[next.x][next.y]='#';
55                     }
56                 }
57             }
58         }
59     }
60     return -1;
61 }
62                     
63 int main()
64 {
65     int i,j;
66     while(cin>>m>>n>>c)
67     {
68         getchar();
69         memset(a,0,sizeof(a));
70         memset(b,0,sizeof(b));
71         k=0;
72         for(i=0;i<m;i++)
73         {
74             scanf("%s",map[i]);
75             for(j=0;j<n;j++)
76             {
77                 if(map[i][j]=='Y')  {x1=i;y1=j;}
78                 if(map[i][j]=='P')
79                 {
80                     a[k]=i;b[k]=j;k++;
81                 }
82             }
83         }
84         int ans=bfs();
85         if(ans==-1)
86             cout<<"Damn teoy!"<<endl;
87         else
88             cout<<ans<<endl;
89     }
90     return 0;
91 }
复制代码

 

posted @   heat nan  阅读(225)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
点击右上角即可分享
微信分享提示