dfs地图类问题

Posted tyqemptyset

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了dfs地图类问题相关的知识,希望对你有一定的参考价值。

·P1443 - 马的遍历

 1 #include <iostream>
 2 #include <iomanip>
 3 #include <cstdio>
 4 #include <string.h>
 5 #include <algorithm>
 6 using namespace std;
 7 int nx[9]=2,2,-2,-2,1,-1,1,-1;
 8 int ny[9]=1,-1,1,-1,2,2,-2,-2;
 9 int ans[401][401];
10 int n,tot=0,m,sx,sy,orz,sam;
11 void dfs(int,int,int);
12 int main()
13 
14     memset(ans,-1,sizeof(ans));
15     cin>>n>>m>>sx>>sy;
16     dfs(sx,sy,0);
17     for(int i=1;i<=n;i++)
18     
19         for(int j=1;j<=m;j++)
20             printf("%-5d",ans[i][j]);
21         cout<<endl;
22     
23 
24 void dfs(int x,int y,int step)
25 
26     if(step>130) return;
27     ans[x][y]=step;
28     for(int i=0;i<8;i++)
29     
30         if(y+ny[i]>0 && x+nx[i]>0 && y+ny[i]<=m && x+nx[i]<=n &&(ans[x+nx[i]][y+ny[i]]==-1||ans[x+nx[i]][y+ny[i]]>step+1))
31         
32             dfs(x+nx[i],y+ny[i],step+1);
33         
34     
35 

阈值这玩意真NB……

 

·P1219 - 八皇后

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 int tot,a[17],ans[17],n;    //a表示第几行 ,1开始。
 4                         //mark用来存对角线、列的数据。
 5                         //mark[0]表示右上左下 
 6                         //mark[1]表示左上右下 
 7                         //mark[2]表示列 
 8 bool mark[3][50];
 9 void dfs(int);
10 int main()
11 
12     cin>>n;
13     dfs(0);
14     cout<<tot;
15 
16 void dfs(int line)
17 
18     if(line==n)
19     
20         tot++;
21         if(tot<=3)
22         
23             for(int i=0;i<n;i++)
24                 cout<<ans[i]+1<<" ";
25             cout<<endl;
26             return;
27         
28         else return;
29     
30     for(int i=0;i<n;i++)    //i表示列 ,line行 
31     
32         if(mark[0][i]!=1&&mark[1][line-i+n]!=1&&mark[2][i+line]!=1)
33                             //行没被用,对角线没有用 
34         
35             ans[line]=i;
36             mark[0][i]=1;mark[1][line-i+n]=1;mark[2][i+line]=1;
37             dfs(line+1);
38             mark[0][i]=0;mark[1][line-i+n]=0;mark[2][i+line]=0;
39         
40     
41 

 

·P1101 - 单词方阵

 1 #include <iostream>
 2 #include <algorithm>
 3 using namespace std;
 4 int nx[9]=1,-1,0,0,1,-1,1,-1,n,n2;
 5 int ny[9]=0,0,-1,1,1,-1,-1,1;
 6 char last,maps[101][101],ans[101][101],word[8]=y,i,z,h,o,n,g;
 7 bool temp[101][101];int flag=-1;
 8 void dfs(int,int,int);
 9 int main()
10 
11     cin>>n;
12     for(int i=1;i<=n;i++)
13         for(int j=1;j<=n;j++)
14         
15             ans[i][j]=*;
16             cin>>maps[i][j];
17         n++;
18     for(int i=1;i<=n;i++)
19         for(int j=1;j<=n;j++)
20         
21             if(maps[i][j]==y)
22             
23                 //cout<<"cheak!"<<i<<"  "<<j<<"  "<<maps[i][j]<<endl;
24                 dfs(i,j,0);
25             
26         
27     for(int i=1;i<=n;i++)
28     
29         for(int j=1;j<=n;j++)
30         
31             //if(temp[i][j]==1)
32             
33             
34             cout<<ans[i][j];
35             //else cout<<"*";
36         
37         cout<<endl;
38     
39 
40 void dfs(int x,int y,int step)
41 
42     if(step==6)
43     
44         temp[x][y]=1;
45         for(int i=1;i<=n;i++)
46             for(int j=1;j<=n;j++)
47             
48                
49                 if(temp[i][j]==1)
50                     ans[i][j]=maps[i][j];
51             
52         return;
53     
54     if(step==0)
55     
56     for(int i=0;i<9;i++)
57     
58         //cout<<"x"<<x<<"  y"<<y<<"  x+nx"<<x+nx[i]<<"  y+ny"<<y+ny[i]<<"     x+~~"<<maps[x+nx[i]][y+ny[i]]<<endl;
59         if(x+nx[i]>=1 && x+nx[i]<=n && y+ny[i]>=1 && y+ny[i]<=n && maps[x+nx[i]][y+ny[i]]==word[step+1])
60         
61             flag=i;
62             temp[x][y]=1;
63             //ans[x][y]=maps[x][y];
64             dfs(x+nx[i],y+ny[i],step+1);
65             temp[x][y]=0;
66         
67     
68     
69     else
70         if(x+nx[flag]>=1 && x+nx[flag]<=n && y+ny[flag]>=1 && y+ny[flag]<=n && maps[x+nx[flag]][y+ny[flag]]==word[step+1])
71                // cout<<"x"<<x<<"  y"<<y<<"  x+nx"<<x+nx[flag]<<"  y+ny"<<y+ny[flag]<<"     x+~~"<<maps[x+nx[flag]][y+ny[flag]]<<" step"<<step<<endl;
72             temp[x][y]=1;
73             //ans[x][y]=maps[x][y];
74             dfs(x+nx[flag],y+ny[flag],step+1);
75             temp[x][y]=0;
76         
77         else flag=0;
78 

 

·P1605 - 迷宫

 1 #include <iostream>
 2 #include <algorithm>
 3 using namespace std;
 4 int nx[5]=1,-1,0,0;
 5 int ny[5]=0,0,1,-1;
 6 bool maps[6][6];
 7 bool temp[6][6];
 8 int n,m,t,sx,sy,fx,ans=0,fy,orzx,orzy;
 9 void dfs(int,int);
10 int main()
11 
12     cin>>n>>m>>t>>sx>>sy>>fx>>fy;
13     for(int i=0;i<t;i++)
14     
15         cin>>orzx>>orzy;
16         maps[orzx][orzy]=1;
17     
18     dfs(sx,sy);
19     cout<<ans;
20 
21 void dfs(int x,int y)
22 
23     if(x==fx&&y==fy) ans++;return;
24     for(int i=0;i<4;i++)
25     
26         if(temp[x+nx[i]][y+ny[i]]==0 && maps[x+nx[i]][y+ny[i]]!=1&&x<=n && y<=m && x>=1 && y>=1)
27         
28             temp[x][y]=1;
29             //cout<<"x"<<x<<"   y"<<y<<endl;
30             dfs(x+nx[i],y+ny[i]);
31             temp[x][y]=0;
32         
33     
34 

 

 

以上是关于dfs地图类问题的主要内容,如果未能解决你的问题,请参考以下文章

Graph-DFS-Map-图的深度优先遍历-城市地图问题

引爆炸弹(dfs解法)

岛屿类问题的通用解法DFS 遍历框架

岛屿问题--DFS问题

岛屿类问题的通用解法DFS 遍历框架

更改 dfs 文件的块大小