hdu 1973+hdu 2364+hdu 2416+hdu 2822+hdu 2645(bfs)

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

思路:简单bfs,先打个素数表,然后就是广搜搞一下就ok了。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cmath>
 5 #include<queue>
 6 #include<string>
 7 using namespace std;
 8 #define MAXN 10100
 9 struct Node{
10     string str;
11     int step;
12 };
13 string st,ed;
14 bool mark[MAXN];
15 bool Prime[MAXN];
16 
17 void IsPrime(){
18     memset(Prime,true,sizeof(Prime));
19     Prime[0]=Prime[1]=false;
20     for(int i=2;i*i<MAXN;i++){
21         for(int j=i*i;j<MAXN;j+=i){
22             if(Prime[i])Prime[j]=false;
23         }
24     }
25 }
26 
27 void bfs(){
28     memset(mark,false,sizeof(mark));
29     queue<Node>Q;
30     Node p,q;
31     p.str=st,p.step=0;
32     int x=0;
33     for(int i=0;i<4;i++)x=x*10+p.str[i]-'0';
34     mark[x]=true;
35     Q.push(p);
36     while(!Q.empty()){
37         p=Q.front();
38         Q.pop();
39         if(p.str==ed){
40             printf("%d\n",p.step);
41             return ;
42         }
43         for(int i=0;i<4;i++){
44             q=p;
45             for(int j=0;j<=9;j++){
46                 if(i==0&&j==0)continue;
47                 if((q.str[i]-'0')!=j)q.str[i]=j+'0';
48                 int x=0;
49                 for(int k=0;k<4;k++)x=x*10+q.str[k]-'0';
50                 if(Prime[x]&&!mark[x]){
51                     mark[x]=true;
52                     q.step=p.step+1;
53                     Q.push(q);
54                 }
55             }
56         }
57     }
58     puts("Impossible");
59 }
60 
61 
62 
63 int main(){
64     IsPrime();
65     int _case;
66     scanf("%d",&_case);
67     while(_case--){
68         cin>>st>>ed;
69         bfs();
70     }
71     return 0;
72 }
View Code

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

思路:就是转弯的时候要判断一下,如果当前前进的方向与原来的方向一致,那么就直接转,否则,就要分两种情况,一种是上下,一种是左右,如果当前两边都为‘#’,就可以前进,否则是不能前进的,还有就是要注意的地方是“You never turn around and go back“.

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<queue>
 5 using namespace std;
 6 #define MAXN 100
 7 struct Node{
 8     int x,y,dir,step;
 9 };
10 char map[MAXN][MAXN];
11 bool mark[MAXN][MAXN][4];
12 int dir[4][2]={{-1,0},{0,-1},{1,0},{0,1}};
13 Node st;
14 int n,m;
15 
16 void bfs(){
17     memset(mark,false,sizeof(mark));
18     queue<Node>Q;
19     Node p,q;
20     Q.push(st);
21     while(!Q.empty()){
22         p=Q.front();
23         Q.pop();
24         if(p.x==1||p.x==n||p.y==1||p.y==m){
25             printf("%d\n",p.step);
26             return ;
27         }
28         for(int i=0;i<4;i++){
29             int xx=p.x+dir[i][0];
30             int yy=p.y+dir[i][1];
31             if(!mark[xx][yy][i]&&map[xx][yy]!='#'){
32                 if((i%2==p.dir%2)){
33                     if(i==p.dir){
34                         if(i%2==0&&map[p.x][p.y+1]=='#'&&map[p.x][p.y-1]=='#'){
35                             mark[xx][yy][i]=true;
36                             q.x=xx,q.y=yy,q.step=p.step+1,q.dir=i;
37                             Q.push(q);
38                         }else if(i%2==1&&map[p.x+1][p.y]=='#'&&map[p.x-1][p.y]=='#'){
39                             mark[xx][yy][i]=true;
40                             q.x=xx,q.y=yy,q.step=p.step+1,q.dir=i;
41                             Q.push(q);
42                         }
43                     }
44                 }else {
45                     q.x=xx,q.y=yy,q.step=p.step+1,q.dir=i;
46                     mark[xx][yy][i]=true;
47                     Q.push(q);
48                 }
49             }
50         }
51     }
52     puts("-1");
53 }
54 
55 
56 
57 int main(){
58     int _case;
59     scanf("%d",&_case);
60     while(_case--){
61         scanf("%d%d",&n,&m);
62         for(int i=1;i<=n;i++){
63             scanf("%s",map[i]+1);
64             for(int j=1;j<=m;j++){
65                 if(map[i][j]=='@')st.x=i,st.y=j,st.step=0,st.dir=-1;
66             }
67         }
68         bfs();
69     }
70     return 0;
71 }
View Code

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

wa了无数次的一道题,不知道怎么回事,感觉不用优先队列也没关系,可就是出不来答案,而一用优先队列就过了。。。郁闷中。。。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<queue>
 5 using namespace std;
 6 #define MAXN 110
 7 #define inf 1<<30
 8 struct Node{
 9     int x,y,key;
10     bool operator < (const Node &p) const {
11         return p.time<time;
12     }
13     int time;
14 };
15 
16 int Time[MAXN][MAXN][33];
17 char map[MAXN][MAXN];
18 int n,m;
19 int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
20 
21 void bfs(){
22     memset(Time,-1,sizeof(Time));
23     priority_queue<Node>Q;
24     Node p;
25     int ans=inf;
26     for(int i=0;i<n;i++){
27         for(int j=0;j<m;j++){
28             if(map[i][j]=='#'||(map[i][j]>='A'&&map[i][j]<='Z')){
29                 p.x=i,p.y=j,p.time=0;
30                 if(map[i][j]=='#')p.key=0;
31                 else p.key=(map[i][j]-'A'+1);
32                 Q.push(p);
33                 map[i][j]='*';
34                 Time[p.x][p.y][p.key]=0;
35             }
36         }
37     }
38     while(!Q.empty()){
39         p=Q.top();
40         Q.pop();
41         Node q;
42         for(int i=0;i<4;i++){
43             q.x=p.x+dir[i][0];
44             q.y=p.y+dir[i][1];
45             q.key=p.key;
46             if(q.x<0||q.x>=n||q.y<0||q.y>=m||map[q.x][q.y]=='*')
47                 continue;
48             if(map[q.x][q.y]=='.'){
49                 if(Time[q.x][q.y][q.key]==-1||p.time<Time[q.x][q.y][q.key]){
50                     q.time=p.time;
51                     Time[q.x][q.y][q.key]=q.time;
52                     Q.push(q);
53                 }
54             }else if(map[q.x][q.y]>'0'&&map[q.x][q.y]<='9'){
55                 if(q.key>0&&(Time[q.x][q.y][q.key-1]==-1||p.time<Time[q.x][q.y][q.key-1])){
56                     q.key=p.key-1,q.time=p.time;
57                     Time[q.x][q.y][q.key]=q.time;
58                     Q.push(q);
59                 }
60                 if(Time[q.x][q.y][p.key]==-1||(p.time+(map[q.x][q.y]-'0')<Time[q.x][q.x][p.key])){
61                     q.key=p.key;
62                     q.time=p.time+map[q.x][q.y]-'0';
63                     Time[q.x][q.y][q.key]=q.time;
64                     Q.push(q);
65                 }
66             }else if(map[q.x][q.y]=='$'&&p.time<ans){
67                 ans=p.time;
68             }
69         }
70     }
71     ans<inf?printf("%d\n",ans):puts("IMPOSSIBLE");
72 }
73 
74 
75 int main(){
76     while(1){
77         n=0;
78         while(gets(map[n])){
79             if(map[n][0]=='-')return 0;
80             if(strlen(map[n])<1)break;
81             n++;
82         }
83         m=strlen(map[0]);
84         bfs();
85     }
86     return 0;
87 }
View Code

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

思路:就是优先队列的简单应用。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<queue>
 5 using namespace std;
 6 #define MAXN 1010
 7 #define inf 1<<30
 8 struct Node{
 9     int x,y,step;
10     bool operator < (const Node &p) const {
11         return p.step<step;
12     }
13 };
14 
15 char map[MAXN][MAXN];
16 bool mark[MAXN][MAXN];
17 int n,m;
18 Node st,ed;
19 int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
20 
21 void bfs(){
22     memset(mark,false,sizeof(mark));
23     priority_queue<Node>Q;
24     Node p,q;
25     mark[st.x][st.y]=true;
26     Q.push(st);
27     while(!Q.empty()){
28         p=Q.top();
29         Q.pop();
30         if(p.x==ed.x&&p.y==ed.y){
31             printf("%d\n",p.step);
32             return ;
33         }
34         for(int i=0;i<4;i++){
35             q.x=p.x+dir[i][0];
36             q.y=p.y+dir[i][1];
37             if(q.x<1||q.x>n||q.y<1||q.y>m||mark[q.x][q.y])
38                 continue;
39             if(map[q.x][q.y]=='X'){
40                 q.step=p.step;
41                 if(!mark[q.x][q.y]){
42                     mark[q.x][q.y]=true;
43                     Q.push(q);
44                 }
45             }else if(map[q.x][q.y]=='.'){
46                 q.step=p.step+1;
47                 if(!mark[q.x][q.y]){
48                     mark[q.x][q.y]=true;
49                     Q.push(q);
50                 }
51             }
52         }
53     }
54 }
55 
56 
57 int main(){
58     while(scanf("%d%d",&n,&m),(n+m)){
59         for(int i=1;i<=n;i++)
60             scanf("%s",map[i]+1);
61         scanf("%d%d",&st.x,&st.y);
62         scanf("%d%d",&ed.x,&ed.y);
63         st.step=ed.step=0;
64         bfs();
65     }
66     return 0;
67 }
68         
View Code

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

思路:就是对于每个‘0’位置进行bfs,找最近的‘1’,orz...一开始还以为会超时呢!!!

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

PS:最近做的几道广搜题,题目的质量都还不错,倾情奉献。

原文地址:https://www.cnblogs.com/wally/p/3080961.html