搜索 水题&&错误集锦(未完成)

引子:

本以为搜索的题目老师也不会检查,结果今天早上loli慢悠悠的说:“请同学们提交一下搜索的题目~”,顿时心旌摇曳,却也只能装作镇定自若的样子,点了点头。。

然后就开始了今天的疯狂做题,虽说题目都不是太难,但题多势众啊...

刷了那么多题,小有收获,总结+复习一下,也是为了以后避免类似错误(十分直观而又影响巨大的)

所做题目链接:openjudge  2.5  题面不一一附上

1.letters

看到这道题,就想到了前几天刚刚学会的STLmap映射,然后没怎么想就敲了一遍代码,结果跑的巨慢无比,然后下去跑操想明白了不用这么麻烦的做法,直接用一个vis数组就是了,看来长时间不做搜索大脑都僵化了。。

其他的就没什么技术含量了,和dfs模板差不多,

细节:1.运算种的i循环0-3就是4次

           2.起始点的vis为true

           3.A-Z ascill码为65-90

代码:

 1 #include<iostream>
 2 #include<cstring>
 3 
 4 using namespace std;
 5 
 6 int fx[4]={0,1,0,-1};
 7 int fy[4]={-1,0,1,0};
 8 int ans=0,d[200][200],n,m;
 9 char a[25][25];bool vis[100];
10 
11 int dfs(int x,int y,int z)
12 {
13     ans=max(ans,z);
14     for(int i=0;i<4;i++)//细节 
15     {
16         int xx=x+fx[i];
17         int yy=y+fy[i];
18         if(xx<=n&&xx>0&&yy<=m&&yy>0&&!vis[a[xx][yy]])
19         {
20             vis[a[xx][yy]]=true;
21             dfs(xx,yy,z+1);
22             vis[a[xx][yy]]=false;
23         }
24     }
25 }
26 int main()
27 {
28     memset(vis,false,sizeof(vis));
29     cin>>n>>m;
30     for(int i=1;i<=n;i++)
31         for(int j=1;j<=m;j++)
32             cin>>a[i][j];
33     vis[a[1][1]]=true;//细节 
34     dfs(1,1,1);
35     cout<<ans<<endl;
36     return 0;
37 }
letters

2&&3.八皇后&&问题

例题八皇后问题引申出的各种问题,比较简单,+注意细节

细节:

      1.横纵循环的次序,外层循环行号,内层循环列号

代码:

 1 #include<iostream>
 2 using namespace std;
 3 int s=0,n,ans[20],h[100]={0},l[100]={0},a[100]={0},b[100]={0},anss[93][20];
 4 void out()
 5 {
 6     s++;
 7     for(int i=1;i<=8;i++)
 8       anss[s][i]=ans[i];  
 9 }
10 int dfs(int k)
11 {
12       //cout<<"12 ";
13       for(int j=1;j<=8;j++)
14         {
15             if((h[j]==0)&&(a[k+j]==0)&&(b[k-j+8]==0))
16             {
17             ans[k]=j;
18             h[j]=1;
19             a[k+j]=1;
20             b[k-j+8]=1;
21             
22             if(k==8)out();
23               else dfs(k+1);
24             h[j]=0;
25             a[k+j]=0;
26             b[k-j+8]=0;
27             }
28         }
29 }
30 int main()
31 {
32     dfs(1);
33     cin>>n;
34     for(int i=1;i<=n;i++)
35     {
36         int x;
37         cin>>x;
38         for(int k=1;k<=8;k++)
39           cout<<anss[x][k];
40         cout<<endl;
41     } 
42 }
八皇后
 1 #include<iostream>
 2 using namespace std;
 3 int s=0,n,ans[20],h[100]={0},l[100]={0},a[100]={0},b[100]={0},anss[93][20];
 4 void out()
 5 {
 6     s++;
 7 //    if(s>4)return ;
 8     cout<<"No. "<<s<<endl;
 9     for(int i=1;i<=8;i++)
10     {
11       for(int j=1;j<=8;j++)
12       {
13           if(ans[j]==i)cout<<1<<" ";
14           else
15             cout<<0<<" ";
16       }
17       cout<<endl;
18     }
19 }
20 int dfs(int k)
21 {
22       //cout<<"12 ";
23       for(int j=1;j<=8;j++)
24         {
25             if((h[j]==0)&&(a[k+j]==0)&&(b[k-j+8]==0))
26             {
27             ans[k]=j;
28             h[j]=1;
29             a[k+j]=1;
30             b[k-j+8]=1;
31             
32             if(k==8)out();
33               else dfs(k+1);
34             h[j]=0;
35             a[k+j]=0;
36             b[k-j+8]=0;
37             }
38         }
39 }
40 int main()
41 {
42 //    cin>>n;//cout<<"123";
43     dfs(1);
44     cin>>n;
45 //    cout<<s;
46     for(int i=1;i<=n;i++)
47     {
48         int x;
49         cin>>x;
50         for(int k=1;k<=8;k++)
51           cout<<anss[x][k];
52         cout<<endl;
53     } 
54 }
八皇后问题

猜你喜欢

转载自www.cnblogs.com/SuperGoodGame/p/9079748.html
今日推荐