hdu 2216+hdu 1104

贴几道bfs题。

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2216

思路:用一个四维的数组来保存状态,然后就是一般的bfs了,不过要注意的S,Z的初始位置要置为'.'(这个地方debug了好久,orz...).

View Code
  1 #include<iostream>
  2 #include<cstdio>
  3 #include<cstring>
  4 #include<cmath>
  5 #include<queue>
  6 using namespace std;
  7 #define MAXN 33
  8 int n,m;
  9 char map[MAXN][MAXN];
 10 bool mark[MAXN][MAXN][MAXN][MAXN];
 11 int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
 12 struct Node{
 13     int sx,sy,ex,ey;
 14     int step;
 15 };
 16 Node st;
 17 
 18 bool bfs(){
 19     memset(mark,false,sizeof(mark));
 20     queue<Node>Q;
 21     Node p,q;
 22     Q.push(st);
 23     mark[st.sx][st.sy][st.ex][st.ey]=true;
 24     while(!Q.empty()){
 25         p=Q.front();
 26     //    printf("%d %d %d %d %d\n",p.sx,p.sy,p.ex,p.ey,p.step);
 27         Q.pop();
 28         if((abs(p.sx-p.ex)+abs(p.sy-p.ey)<=1)){
 29             printf("%d\n",p.step);
 30             return true;
 31         }
 32         for(int i=0;i<4;i++){
 33             q.sx=p.sx+dir[i][0];
 34             q.sy=p.sy+dir[i][1];
 35             if(q.sx>=1&&q.sx<=n&&q.sy>=1&&q.sy<=m&&map[q.sx][q.sy]!='X'){
 36                 if(i==1){
 37                     q.ex=p.ex+dir[0][0];
 38                     q.ey=p.ey+dir[0][1];
 39                     if(q.ex>=1&&q.ex<=n&&q.ey>=1&&q.ey<=m&&map[q.ex][q.ey]!='X'){
 40                         if(!mark[q.sx][q.sy][q.ex][q.ey]){
 41                             mark[q.sx][q.sy][q.ex][q.ey]=true;
 42                             q.step=p.step+1;
 43                             Q.push(q);
 44                         }
 45                     }else {
 46                         q.ex=p.ex,q.ey=p.ey;
 47                         if(!mark[q.sx][q.sy][q.ex][q.ey]){
 48                             mark[q.sx][q.sy][q.ex][q.ey]=true;
 49                             q.step=p.step+1;
 50                             Q.push(q);
 51                         }
 52                     }
 53                 }else if(i==3){
 54                     q.ex=p.ex+dir[2][0];
 55                     q.ey=p.ey+dir[2][1];
 56                     if(q.ex>=1&&q.ex<=n&&q.ey>=1&&q.ey<=m&&map[q.ex][q.ey]!='X'){
 57                         if(!mark[q.sx][q.sy][q.ex][q.ey]){
 58                             mark[q.sx][q.sy][q.ex][q.ey]=true;
 59                             q.step=p.step+1;
 60                             Q.push(q);
 61                         }
 62                     }else {
 63                         q.ex=p.ex,q.ey=p.ey;
 64                         if(!mark[q.sx][q.sy][q.ex][q.ey]){
 65                             mark[q.sx][q.sy][q.ex][q.ey]=true;
 66                             q.step=p.step+1;
 67                             Q.push(q);
 68                         }
 69                     }
 70                 }else {
 71                     q.ex=p.ex+dir[i+1][0];
 72                     q.ey=p.ey+dir[i+1][1];
 73                     if(q.ex>=1&&q.ex<=n&&q.ey>=1&&q.ey<=m&&map[q.ex][q.ey]!='X'){
 74                         if(!mark[q.sx][q.sy][q.ex][q.ey]){
 75                             mark[q.sx][q.sy][q.ex][q.ey]=true;
 76                             q.step=p.step+1;
 77                             Q.push(q);
 78                         }
 79                     }else {
 80                         q.ex=p.ex,q.ey=p.ey;
 81                         if(!mark[q.sx][q.sy][q.ex][q.ey]){
 82                             mark[q.sx][q.sy][q.ex][q.ey]=true;
 83                             q.step=p.step+1;
 84                             Q.push(q);
 85                         }
 86                     }
 87                 }
 88             }
 89         }
 90     }
 91     return false;
 92 }
 93 
 94 
 95 
 96 int main(){
 97     while(~scanf("%d%d",&n,&m)){
 98         memset(map,0,sizeof(map));
 99         for(int i=1;i<=n;i++)
100             scanf("%s",map[i]+1);
101         for(int i=1;i<=n;i++){
102             for(int j=1;j<=m;j++){
103                 if(map[i][j]=='Z'){
104                     st.sx=i,st.sy=j,st.step=0;
105                 }else if(map[i][j]=='S'){
106                     st.ex=i,st.ey=j;
107                 }
108             }
109         }
110         if(!bfs())
111             puts("Bad Luck!");
112     }
113     return 0;
114 }

 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1104

思路:注意要对(k*m)取模,然后就是用string来保存路径了。

View Code
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<string>
 5 #include<queue>
 6 using namespace std;
 7 #define MAXN 4444
 8 struct Node{
 9     int x,step;
10     string path;
11 };
12 int n,m,k;
13 bool mark[MAXN];
14 
15 bool bfs(){
16     memset(mark,false,sizeof(mark));
17     queue<Node>Q;
18     Node p,q;
19     p.x=n,p.step=0,p.path="";
20     mark[(p.x%k+k)%k]=true;
21     Q.push(p);
22     while(!Q.empty()){
23         p=Q.front();
24         Q.pop();
25         if((p.x%k+k)%k==((n+1)%k+k)%k){
26             cout<<p.step<<endl;
27             cout<<p.path<<endl;
28             return true;
29         }
30         q.step=p.step+1;
31         for(int i=0;i<4;i++){
32             if(i==0){
33                 q.x=((p.x+m)%(k*m)+(k*m))%(k*m);
34                 q.path=p.path+'+';
35             }else if(i==1){
36                 q.x=((p.x-m)%(k*m)+(k*m))%(k*m);
37                 q.path=p.path+'-';
38             }else if(i==2){
39                 q.x=((p.x*m)%(k*m)+(k*m))%(k*m);
40                 q.path=p.path+'*';
41             }else {
42                 q.x=(p.x%m+m)%m;
43                 q.path=p.path+'%';
44             }
45             if(!mark[(q.x%k+k)%k]){
46                 mark[(q.x%k+k)%k]=true;
47                 Q.push(q);
48             }
49         }
50     }
51     return false;
52 }
53 
54 
55 int main(){
56     while(scanf("%d%d%d",&n,&k,&m),n||k||m){
57         if(!bfs())
58             puts("0");
59     }
60     return 0;
61 }

 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1676

思路:Cost[i][j]二维数组不断更新到达城市i还剩燃料j时的最小花费,用spfa实现;

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<queue>
 5 #include<vector>
 6 using namespace std;
 7 #define MAXN 1000+10
 8 #define MAXC 100+10
 9 #define inf 1<<30
10 struct Node{
11     int v,d;
12 };
13 vector<Node>Map[MAXN];
14 
15 struct Point{
16     int city,cost,fuel;
17     bool operator < (const Point &p) const {
18         return p.cost<cost;
19     }
20 };
21 
22 int Cost[MAXN][MAXC];//记录到城市i有燃料j的最小花费
23 int mark[MAXN][MAXC];//标记
24 int value[MAXN];
25 int n,m,c,st,ed;
26 
27 void bfs(){
28     priority_queue<Point>Q;
29     Point p,q;
30     p.city=st,p.cost=p.fuel=0;
31     Cost[st][0]=0;
32     Q.push(p);
33     while(!Q.empty()){
34         p=Q.top();
35         Q.pop();
36         int city=p.city,cost=p.cost,fuel=p.fuel;
37         if(mark[city][fuel])continue;
38         if(city==ed){
39             return ;
40         }
41         if(fuel<c){
42             if(cost+value[city]<Cost[city][fuel+1]){
43                 Cost[city][fuel+1]=cost+value[city];
44                 q.city=city,q.cost=cost+value[city],q.fuel=fuel+1;
45                 Q.push(q);
46             }
47         }
48         for(int i=0;i<Map[city].size();i++){
49             int v=Map[city][i].v;
50             int d=Map[city][i].d;
51             if(fuel-d>=0&&Cost[city][fuel]<Cost[v][fuel-d]){
52                 Cost[v][fuel-d]=Cost[city][fuel];
53                 q.city=v,q.cost=Cost[v][fuel-d],q.fuel=fuel-d;
54                 Q.push(q);
55             }
56         }
57         mark[city][fuel]=true;
58     }
59 }
60 
61 
62 int main(){
63     int _case,u,v,dist;
64     scanf("%d%d",&n,&m);
65     for(int i=0;i<n;i++)scanf("%d",&value[i]);
66     for(int i=1;i<=m;i++){
67         scanf("%d%d%d",&u,&v,&dist);
68         Node p1,p2;
69         p1.v=v,p2.v=u;
70         p1.d=p2.d=dist;
71         Map[u].push_back(p1);
72         Map[v].push_back(p2);
73     }
74     scanf("%d",&_case);
75     while(_case--){
76         scanf("%d%d%d",&c,&st,&ed);
77         for(int i=0;i<n;i++){
78             for(int j=0;j<=c;j++){
79                 Cost[i][j]=inf;
80                 mark[i][j]=false;
81             }
82         }
83         bfs();
84         //由于要花费最小,到达最后一个城市必然燃料用尽
85         Cost[ed][0]<inf?printf("%d\n",Cost[ed][0]):puts("impossible");
86     }
87     return 0;
88 }
View Code

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值