ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

[kuangbin带你飞]专题一 简单搜索(回顾)

2019-04-02 18:40:16  阅读:218  来源: 互联网

标签:专题 回顾 int top vis maxn && kuangbin include


A - 棋盘问题

 POJ - 1321 

注意条件:不能每放一个棋子,就标记一行和一列,我们直接枚举每一行就可以了。

AC代码:

 1 #include<iostream>
 2 #include<stdio.h>
 3 #include<cstring>
 4 using namespace std;
 5 # define ll long long
 6 const int maxn =2100;
 7 char str[maxn][maxn];
 8 int vis[maxn];
 9 int n,m,num;
10 void dfs(int u,int cnt)
11 {
12     if(cnt==m)
13     {
14         num++;
15         return ;
16     }
17     if(u==n)
18         return ;
19     for(int i=0; i<n; i++)
20     {
21         if(!vis[i]&&str[u][i]=='#')
22         {
23             vis[i]=1;
24             dfs(u+1,cnt+1);
25             vis[i]=0;
26         }
27     }
28     dfs(u+1,cnt);
29 }
30 int main()
31 {
32     while(~scanf("%d %d",&n,&m))
33     {
34         if(n==-1&&m==-1)
35             break;
36         memset(vis,0,sizeof(vis));
37         num=0;
38         for(int i=0; i<n; i++)
39         {
40             scanf("%s",str[i]);
41         }
42         dfs(0,0);
43         printf("%d\n",num);
44     }
45     return 0;
46 }
View Code

 

B - Dungeon Master

 POJ - 2251 

注意条件:简单三维bfs。

AC代码:

 1 #include<iostream>
 2 #include<stdio.h>
 3 #include<cstring>
 4 #include<queue>
 5 using namespace std;
 6 # define ll long long
 7 const int maxn =50;
 8 char str[maxn][maxn][maxn];
 9 int vis[maxn][maxn][maxn];
10 int f[3][6]= {{1,-1,0,0,0,0},
11     {0,0,-1,1,0,0},
12     {0,0,0,0,1,-1}
13 };
14 int n,m,k;
15 int stx,sty,stz;
16 int edx,edy,edz;
17 int ans;
18 bool judge(int x,int y,int z)
19 {
20     if(x>=1&&x<=n&&y>=1&&y<=m&&z>=1&&z<=k)
21         return true;
22     return false;
23 }
24 struct node
25 {
26     int x,y,z,step;
27     node() {}
28     node(int xx,int yy,int zz,int tt)
29     {
30         x=xx,y=yy,z=zz,step=tt;
31     }
32 };
33 void bfs(int x,int y,int z)
34 {
35     queue<node>q;
36     q.push(node(x,y,z,0));
37     vis[x][y][z]=1;
38     while(!q.empty())
39     {
40         node top=q.front();
41         q.pop();
42         if(top.x==edx&&top.y==edy&&top.z==edz)
43         {
44             ans=top.step;
45             return ;
46         }
47         for(int i=0; i<6; i++)
48         {
49             int tx,ty,tz;
50             tx=top.x+f[0][i];
51             ty=top.y+f[1][i];
52             tz=top.z+f[2][i];
53             if(judge(tx,ty,tz)&&vis[tx][ty][tz]==0&&str[tx][ty][tz]!='#')
54             {
55                 vis[tx][ty][tz]=1;
56                 q.push(node(tx,ty,tz,top.step+1));
57             }
58         }
59     }
60 }
61     int main()
62     {
63         while(~scanf("%d %d %d",&n,&m,&k)&&(n+m+k))
64         {
65             memset(vis,0,sizeof(vis));
66             ans=-1;
67             for(int i=1; i<=n; i++)
68             {
69                 for(int j=1; j<=m; j++)
70                 {
71                     scanf("%s",str[i][j]+1);
72                     for(int l=1; l<=k; l++)
73                     {
74                         if(str[i][j][l]=='S')
75                         {
76                             stx=i;
77                             sty=j;
78                             stz=l;
79                         }
80                         if(str[i][j][l]=='E')
81                         {
82                             edx=i;
83                             edy=j;
84                             edz=l;
85                         }
86                     }
87                 }
88             }
89             bfs(stx,sty,stz);
90             if(ans==-1)
91                 printf("Trapped!\n");
92             else
93                 printf("Escaped in %d minute(s).\n",ans);
94         }
95         return 0;
96     }
View Code

C - Catch That Cow

 POJ - 3278 

注意条件:bfs,注意限制条件。

AC代码:

 1 #include<iostream>
 2 #include<stdio.h>
 3 #include<cstring>
 4 #include<queue>
 5 using namespace std;
 6 # define ll long long
 7 const int maxn =2e5+100;
 8 int vis[maxn];
 9 pair<int,int> top;
10 int  bfs(int st,int ed){
11 queue<pair<int,int> >q;
12 vis[st]=1;
13 q.push(make_pair(st,0));
14 while(!q.empty()){
15 top=q.front();
16 q.pop();
17 if(top.first==ed){
18 return top.second;
19 }
20 if(top.first+1<=ed&&vis[top.first+1]==0){
21 vis[top.first+1]=1;
22 q.push(make_pair(top.first+1,top.second+1));
23 }
24 if(top.first-1>=0&&vis[top.first-1]==0){
25 vis[top.first-1]=1;
26 q.push(make_pair(top.first-1,top.second+1));
27 }
28 if(top.first*2<=2*ed&&vis[top.first*2]==0){
29 vis[top.first*2]=1;
30 q.push(make_pair(top.first*2,top.second+1));
31 }
32 }
33 }
34 int main(){
35 int n,m;
36 while(~scanf("%d %d",&n,&m)){
37 memset(vis,0,sizeof(vis));
38 int ans=bfs(n,m);
39 printf("%d\n",ans);
40 }
41 return 0;
42 }
View Code

 

标签:专题,回顾,int,top,vis,maxn,&&,kuangbin,include
来源: https://www.cnblogs.com/letlifestop/p/10644447.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有