第三次组队赛(bfs&&dfs)

A Knight's Journey  POJ 2488

用DFS来做:发现最难的是找到用什么来表示回溯!(step的加与减,格子的变与不变。。。)

 1 #include<iostream>
 2 #include<string.h>
 3 #include<stdio.h>
 4 #include<algorithm>
 5 using namespace std;
 6 int c[30],d[35],n,m,flag,a[30][30];
 7 int b[8][2]={{-1,-2},{1,-2},{-2,-1},{2,-1},{-2,1},{2,1},{-1,2},{1,2}};//字典序(右边从小到大,左边再从小到大)
 8 void dfs(int x,int y,int t)
 9 {
10     int x1,y1,i;
11     if(t==n*m||flag)
12     {
13         flag=1;
14         return ;
15     }
16     for(i=0;i<8;i++)
17     {
18         x1=x+b[i][0];
19         y1=y+b[i][1];
20         if(x1>=1&&x1<=n&&y1>=1&&y1<=m&&!a[x1][y1])
21         {
22             if(flag) return ;//这个地方不能省
23             a[x1][y1]=1;
24             c[t]=x1;d[t]=y1;
25             dfs(x1,y1,t+1);
26             a[x1][y1]=0;//回溯法(貌似这里就不用对t进行加减)
27         }
28     }
29 }
30 int main()
31 {
32     int h=0,t,i;
33     scanf("%d",&t);
34     while(t--)
35     {
36         scanf("%d%d",&n,&m);
37         memset(a,0,sizeof(a));
38         printf("Scenario #%d:\n",++h);
39         flag=0;a[1][1]=1;dfs(1,1,1);
40         if(flag==0)
41             printf("impossible\n\n");
42         else
43         {
44             printf("%c%d",'A',1);
45             for(i=1;i<n*m;i++)
46          printf("%c%d",d[i]+64,c[i]);
47          printf("\n\n");
48         }
49     }
50     return 0;
51 }

Avoid The Lakes POJ 3620

简单的:

 1 #include<iostream>
 2 #include<stdio.h>
 3 #include<algorithm>
 4 #include<string.h>
 5 using namespace std;
 6 int a[10005],n,m,s;
 7 int a1[105][105];
 8 int b[4][2]={0,1,0,-1,1,0,-1,0};
 9 void dfs(int x,int y)
10 {
11     int i,x1,y1;
12     for(i=0;i<4;i++)
13     {
14         x1=x+b[i][0];
15         y1=y+b[i][1];
16         if(x1>0&&x1<=n&&y1>0&&y1<=m&&a1[x1][y1]==1)
17         {
18             a1[x1][y1]=0;
19             s++;
20             dfs(x1,y1);
21         }
22     }
23 }
24 int main()
25 {
26     int i,MAX,j,n1,x,y;
27     while(scanf("%d%d%d",&n,&m,&n1)!=EOF)
28     {
29         memset(a1,0,sizeof(a1));
30         for(i=1;i<=n1;i++)
31         {
32             scanf("%d%d",&x,&y);
33             a1[x][y]=1;
34         }
35         MAX=0;
36         for(i=1;i<=n;i++)
37             for(j=1;j<=m;j++)
38         {
39             if(a1[i][j]==1)
40             {
41                 a1[i][j]=0;s=1;
42                 dfs(i,j);
43                 if(s>MAX)
44                     MAX=s;
45             }
46         }
47         printf("%d\n",MAX);
48     }
49     return 0;
50 }

Dungeon Master  POJ 2251

简单的BFS

 1 #include<iostream>
 2 #include<queue>
 3 #include<algorithm>
 4 #include<stdio.h>
 5 #include<string.h>
 6 using namespace std;
 7 char a[35][35][35];
 8 int map1[35][35][35];
 9 int b[6][3]={0,0,1,0,1,0,1,0,0,0,0,-1,0,-1,0,-1,0,0};
10 int sum,d,a1,b1,c1,a2,b2,c2,x,y,z;
11 void bfs()
12 {
13     int x1,y1,z1,x2,y2,z2,i;
14     memset(map1,0,sizeof(map1));
15     queue<int> Q;
16     if(!Q.empty())
17         Q.pop();
18     Q.push(a1);
19     Q.push(b1);
20     Q.push(c1);
21     while(!Q.empty())
22     {
23         x1=Q.front();
24         Q.pop();
25         y1=Q.front();
26         Q.pop();
27         z1=Q.front();
28         Q.pop();
29         if(x1==a2&&y1==b2&&z1==c2)
30         {
31             d=1;return ;
32         }
33         for(i=0;i<6;i++)
34         {
35             x2=x1+b[i][0];
36             y2=y1+b[i][1];
37             z2=z1+b[i][2];
38         if(x2>0&&x2<=x&&y2>0&&y2<=y&&z2>0&&z2<=z&&a[x2][y2][z2]!='#'&&map1[x2][y2][z2]==0)
39         {
40             map1[x2][y2][z2]=map1[x1][y1][z1]+1;
41             Q.push(x2);
42             Q.push(y2);
43             Q.push(z2);
44 
45         }
46         }
47 
48     }
49 }
50 int main()
51 {
52     int i,j,k;
53     while(scanf("%d%d%d",&x,&y,&z)!=EOF)
54     {
55         sum=0;
56         if(x==0&&y==0&&z==0)
57             break;
58             for(i=1;i<=x;i++)
59                 for(j=1;j<=y;j++)
60                 scanf("%s",a[i][j]+1);
61         for(i=1;i<=x;i++)
62             for(j=1;j<=y;j++)
63                 for(k=1;k<=z;k++)
64                     {
65                         if(a[i][j][k]=='S')
66                         {
67                             a1=i;b1=j;c1=k;a[i][j][k]='.';
68                         }
69                         if(a[i][j][k]=='E')
70                         {
71                             a2=i;b2=j;c2=k;
72                         }
73                     }
74         d=0;bfs();
75         if(d==1)
76             printf("Escaped in %d minute(s).\n",map1[a2][b2][c2]);
77         else if(d==0)
78             printf("Trapped!\n");
79     }
80     return 0;
81 }

Sum It Up HDU 1258

变行的DFS

 1 #include<iostream>
 2 #include<stdio.h>
 3 #include<algorithm>
 4 using namespace std;
 5 int n,m,a1[15],b1[15],flag,cout1;
 6 void dfs(int a,int b)
 7 {
 8     int i,j;
 9     if(b==n)
10     {
11         flag=1;
12         printf("%d",b1[0]);
13         for(i=1;i<cout1;i++)
14             printf("+%d",b1[i]);
15         printf("\n");
16         return ;//DFS:能求出多种符合题意的解
17     }
18     if(a==m||b>n)
19         return ;
20     for(j=a;j<m;j++)
21     {
22         b1[cout1++]=a1[j];
23         dfs(j+1,b+a1[j]);
24         cout1--;//回溯步骤
25         while(j-1<m&&a1[j]==a1[j+1])
26             j++;
27     }
28 }
29 int main()
30 {
31     int i;
32     while(scanf("%d%d",&n,&m)!=EOF)
33     {
34         if(n==0&&m==0)
35             break;
36         for(i=0;i<m;i++)
37             scanf("%d",&a1[i]);
38         flag=0;cout1=0;
39         printf("Sums of %d:\n",n);
40         dfs(0,0);
41         if(flag==0)
42             printf("NONE\n");
43     }
44     return 0;
45 }

N皇后问题 HDU 2553

经典的DFS

 1 #include<iostream>
 2 #include<stdio.h>
 3 #include<string.h>
 4 #include<algorithm>
 5 using namespace std;
 6 int a[15],b[15],n,sum;//b[]是表示对应的皇后的横和竖是否有其他的皇后(压缩了很多步骤)
 7 void dfs(int k)
 8 {
 9     int i,j,m;
10     if(k==n+1)
11     {
12         sum++;
13         return ;
14     }
15     for(i=1;i<=n;i++)
16         if(b[i]==0)
17         {
18             a[k]=i;//横和竖用一维数组表示
19             m=1;
20             for(j=1;j<=k-1;j++)
21                 if(a[k]-a[j]==k-j||a[k]-a[j]==j-k)//判断对角线是否有(斜率为1或-1)
22                 {
23                     m=0;break;
24                 }
25             if(m==1)
26             {
27                 b[i]=1;
28                 dfs(k+1);
29                 b[i]=0;
30             }
31         }
32 }
33 int main()
34 {
35     int a1[15],i,t;
36     for(i=1;i<=10;i++)//打表
37     {
38         n=i;
39         memset(a,0,sizeof(a));
40         memset(b,0,sizeof(b));
41         sum=0;
42         dfs(1);
43         a1[i]=sum;
44     }
45     while(scanf("%d",&t)!=EOF)
46     {
47         if(t==0) break;
48         printf("%d\n",a1[t]);
49     }
50     return 0;
51 }

Basic POJ 1315

我为什么做不出是没想到怎么来列回溯方程

 

 1 #include<iostream>
 2 #include<stdio.h>
 3 #include<algorithm>
 4 using namespace std;
 5 int ans,n;
 6 char a[5][5];
 7 bool iosk(int x,int y)
 8 {
 9     int i;
10     for(i=x-1;i>=1&&a[i][y]!='X';i--)
11         if(a[i][y]=='0')
12             return false;
13     for(i=x+1;i<=n&&a[i][y]!='X';i++)
14         if(a[i][y]=='0')
15         return false;
16     for(i=y-1;i>=1&&a[x][i]!='X';i--)
17         if(a[x][i]=='0')
18         return false;
19     for(i=y+1;i<=n&&a[x][i]!='X';i++)
20         if(a[x][i]=='0')
21         return false;
22     return true;
23 }
24 void dfs(int x,int y,int p)
25 {
26     int i,j;
27     for(i=1;i<=n;i++)
28         for(j=1;j<=n;j++)
29     {
30         if(a[i][j]=='.'&&iosk(i,j))
31         {
32             a[i][j]='0';
33             dfs(i,j,p+1);
34             a[i][j]='.';
35         }
36     }//这里每一次放进一个数都要从起点进行比较
37     if(ans<p)
38         ans=p;
39 }
40 int main()
41 {
42     int i;
43     while(scanf("%d",&n)!=EOF)
44     {
45         if(n==0) break;
46         for(i=1;i<=n;i++)
47             scanf("%s",a[i]+1);
48         ans=0;
49         dfs(1,1,0);
50         printf("%d\n",ans);
51     }
52     return 0;
53 }

 

Asteroids! HDU 1240

一道简单的BFS

 1 #include<iostream>
 2 #include<string.h>
 3 #include<string>
 4 #include<stdio.h>
 5 #include<algorithm>
 6 #include<queue>
 7 using namespace std;
 8 int map1[15][15][15];
 9 int b[6][3]={0,0,1,0,0,-1,1,0,0,-1,0,0,0,1,0,0,-1,0},d;
10 int x1,y1,z1,x2,y2,z2,n;
11 void bfs()
12 {
13     int x3,y3,z3,x4,y4,z4,i;
14     memset(map1,0,sizeof(map1));
15     queue<int> Q;
16     Q.push(x1);
17     Q.push(y1);
18     Q.push(z1);
19     while(!Q.empty())
20     {
21         x3=Q.front();
22         Q.pop();
23         y3=Q.front();
24         Q.pop();
25         z3=Q.front();
26         Q.pop();
27         if(x3==x2&&y3==y2&&z3==z2)
28         {
29             d=1;return ;
30         }
31         for(i=0;i<6;i++)
32         {
33             x4=x3+b[i][0];
34             y4=x3+b[i][1];
35             z4=x3+b[i][2];
36             if(x4>=0&&x4<n&&y4>=0&&y4<n&&z4>=0&&z4<n&&map1[x4][y4][z4]==0)
37             {
38                 map1[x4][y4][z4]=map1[x3][y3][z3]+1;
39                  Q.push(x4);
40                 Q.push(y4);
41                 Q.push(z4);
42             }
43         }
44     }
45 }
46 int main()
47 {
48     int i,j;
49     char a[10],a1[15][15][15],a2[4];
50     while(scanf("%s",a)!=EOF)
51     {
52         scanf("%*c%d",&n);
53         for(i=0;i<n;i++)
54             for(j=0;j<n;j++)
55                 scanf("%s",a1[i][j]);
56         scanf("%d%d%d",&x1,&y1,&z1);
57         scanf("%d%d%d",&x2,&y2,&z2);
58         scanf("%s",a2);
59         d=0;bfs();
60         if(d==0)
61             printf("NO ROUTE\n");
62         else if(d==1)
63             printf("%d %d\n",n,map1[x2][y2][z2]);
64     }
65     return 0;
66 }

 

 

posted on 2013-08-03 10:32  ~~碾压机  阅读(206)  评论(0编辑  收藏  举报