挑战2.1

Posted ehanla

tags:

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

练习链接

A

技术分享图片
 1 #include <cstdio>
 2 #include <cstring>
 3 #include <iostream>
 4 using namespace std;
 5 
 6 const int N=30;
 7 char s[N][N];
 8 int dx[4]={-1,1,0,0};
 9 int dy[4]={0,0,-1,1};
10 bool vis[N][N];
11 int cnt,n,m;
12 
13 bool check(int x,int y){
14     if(x>=1&&x<=n&&y>=1&&y<=m&&!vis[x][y]) return true;
15     return false;
16 }
17 
18 void dfs(int x,int y){
19     for(int i=0;i<4;i++){
20         int nx=dx[i]+x;
21         int ny=dy[i]+y;
22         if(check(nx,ny)&&s[nx][ny]==.) cnt++,vis[nx][ny]=1,dfs(nx,ny);
23     }
24 }
25 
26 int main(){
27     while(scanf("%d%d",&m,&n)!=EOF){
28         memset(vis,0,sizeof(vis));
29         if(m==0&&n==0) break;
30         int sx=-1,sy=-1;
31         cnt=1;
32         for(int i=1;i<=n;i++){
33             scanf("%s",s[i]+1);
34             for(int j=1;j<=m;j++) if(s[i][j]==@) sx=i,sy=j;
35         }
36         dfs(sx,sy);
37         printf("%d
",cnt);
38     }
39     return 0;
40 }
View Code

B

技术分享图片
 1 #include <cstdio>
 2 #include <cstring>
 3 #include <iostream>
 4 using namespace std;
 5 
 6 
 7 const int N=123;
 8 int n,m;
 9 char s[N][N];
10 bool vis[N][N];
11 int dx[4]={0,0,-1,1};
12 int dy[4]={-1,1,0,0};
13 
14 bool check(int x,int y){
15     if(x>=1&&x<=n&&y>=1&&y<=m) return true;
16     return false;
17 }
18 
19 void dfs(int x,int y,char match){
20     for(int i=0;i<4;i++){
21         int nx=x+dx[i];
22         int ny=y+dy[i];
23         if(!vis[nx][ny]&&check(nx,ny)&&s[nx][ny]==match) vis[nx][ny]=1,dfs(nx,ny,match);
24     }
25 }
26 
27 int main(){
28 
29     while(scanf("%d%d",&n,&m)!=EOF){
30         memset(vis,0,sizeof(vis));
31         if(n==0&&m==0) break;
32         int cnt=0;
33         for(int i=1;i<=n;i++) scanf("%s",s[i]+1);
34         for(int i=1;i<=n;i++){
35             for(int j=1;j<=m;j++){
36                 if(!vis[i][j]) cnt++,dfs(i,j,s[i][j]);
37             }
38         }
39         printf("%d
",cnt);
40     }
41 
42     return 0;
43 }
View Code

C

技术分享图片
 1 #include <stack>
 2 #include <cstdio>
 3 #include <iostream>
 4 #include <algorithm>
 5 using namespace std;
 6 
 7 stack <int> s1,s2;
 8 
 9 int main(){
10     int t;
11     scanf("%d",&t);
12     while(t--){
13         bool f=1;
14         while(!s1.empty()) s1.pop();
15         while(!s2.empty()) s2.pop();
16         int x;
17         for(int i=1;i<=10;i++){
18             scanf("%d",&x);
19             int t1=-1,t2=-1;
20             if(!s1.empty()) t1=s1.top();
21             if(!s2.empty()) t2=s2.top();
22             if(t1==-1&&t2==-1) s1.push(x);
23             else if(t1==-1){
24                 if(x>t2) s2.push(x);
25                 else s1.push(x);
26             }
27             else if(t2==-1){
28                 if(x>t1) s1.push(x);
29                 else s2.push(x);
30             }
31             else if(t1!=-1&&t2!=-1){
32                 int num1=x-t1,num2=x-t2;
33                 if(num1<0&&num2<0) {f=0;}
34                 else if(num1<0) s2.push(x);
35                 else if(num2<0) s1.push(x);
36                 else{
37                     if(num1<num2) s1.push(x);
38                     else s2.push(x);
39                 }
40             }
41         }
42         if(!f) printf("NO
");
43         else printf("YES
");
44     }
45     return 0;
46 }
View Code

D

技术分享图片
 1 #include <cstdio>
 2 #include <cstring>
 3 #include <iostream>
 4 #include <algorithm>
 5 using namespace std;
 6 
 7 const int N=100;
 8 const int INF=0x3f3f3f3f;
 9 int a[N][N],ans,n,m;
10 int sx,sy,ex,ey;
11 int dx[4]={0,0,-1,1};
12 int dy[4]={-1,1,0,0};
13 
14 bool check(int x,int y){
15     if(x>=1&&x<=n&&y>=1&&y<=m&&a[x][y]!=1) return true;
16     return false;
17 }
18 
19 void dfs(int x,int y,int cnt){
20     if(cnt>10) return ;
21     for(int i=0;i<4;i++){
22         int nx=x,ny=y;
23         nx+=dx[i];ny+=dy[i];
24         bool ok=0;
25         while(check(nx,ny)){
26             ok=1;
27             if(nx==ex&&ny==ey){
28                 ans=min(ans,cnt);
29             }
30             nx+=dx[i];
31             ny+=dy[i];
32         }
33         if(ok&&a[nx][ny]==1){
34             a[nx][ny]=0;
35             dfs(nx-dx[i],ny-dy[i],cnt+1);
36             a[nx][ny]=1;
37         }
38     }
39 }
40 
41 int main(){
42     while(scanf("%d%d",&m,&n)!=EOF){
43         memset(a,0,sizeof(a));
44         if(n==0&&m==0) break;
45         ans=INF;
46         for(int i=1;i<=n;i++)
47         for(int j=1;j<=m;j++){
48             scanf("%d",&a[i][j]);
49             if(a[i][j]==2) sx=i,sy=j;
50             if(a[i][j]==3) ex=i,ey=j;
51         }
52         dfs(sx,sy,1);
53         printf("%d
",ans>10?-1:ans);
54     }
55     return 0;
56 }
View Code

E

技术分享图片
 1 #include <queue>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <iostream>
 5 #include <algorithm>
 6 using namespace std;
 7 
 8 const int N=1e3+10;
 9 char s[N][N];
10 int n,m,t;
11 int px[30],py[30];
12 int dx[4]={0,0,-1,1};
13 int dy[4]={-1,1,0,0};
14 bool vis[N][N];
15 
16 struct node{
17     int x,y,step;
18 }p,cur;
19 
20 bool check(int x,int y){
21     if(x>=1&&x<=n&&y>=1&&y<=m) return true;
22     return false;
23 }
24 
25 int bfs(int sx,int sy,int ex,int ey){
26     queue <node> Q;
27     memset(vis,0,sizeof(vis));
28     p.x=sx;p.y=sy;p.step=0;
29     Q.push(p);
30     vis[sx][sy]=1;
31     while(!Q.empty()){
32         cur=Q.front();
33         if(cur.x==ex&&cur.y==ey) return cur.step;
34         Q.pop();
35         for(int i=0;i<4;i++){
36             int nx=cur.x+dx[i];
37             int ny=cur.y+dy[i];
38             if(check(nx,ny)&&s[nx][ny]!=X&&!vis[nx][ny]){
39                 p.x=nx;p.y=ny;p.step=cur.step+1;
40                 vis[p.x][p.y]=1;
41                 Q.push(p);
42             }
43         }
44     }
45 }
46 
47 int main(){
48     while(scanf("%d%d%d",&n,&m,&t)!=EOF){
49         for(int i=1;i<=n;i++){
50             scanf("%s",s[i]+1);
51             for(int j=1;j<=m;j++){
52                 if(s[i][j]==S) px[0]=i,py[0]=j;
53                 else if(s[i][j]>=1&&s[i][j]<=9) px[(s[i][j]-0)]=i,py[(s[i][j]-0)]=j;
54             }
55         }
56         int ans=0;
57         for(int i=1;i<=t;i++) ans=ans+bfs(px[i-1],py[i-1],px[i],py[i]);
58         printf("%d
",ans);
59     }
60     return 0;
61 }
View Code

 

以上是关于挑战2.1的主要内容,如果未能解决你的问题,请参考以下文章

挑战2.1

JavaScript单行代码,也就是代码片段

.NET CORE 2.1-如何返回不属于 DefaultChallengeScheme 的挑战

JavaScript笔试题(js高级代码片段)

挑战程序设计竞赛 2.1 最基础的“穷竭搜索”

挑战程序设计竞赛2.1习题:Osenbei Aizu - 0525