SDMUOJ-P22sdutoj奇怪的星球

 

 

 

 (这个题怎么在新生赛的时候就没做出来,好水)狗头保命

开学第一次写博客,其实昨天正式开学已经做了两道题了,只不过很水,这个题还是比较有学习意义的,其实是模拟题,思路较为清晰:

如果碰到'.‘就与'a'交换位置就好了,这就需要不断的交换上下的位置,此处需要一个while来完成,以满足不断交换的需求。

参考代码及注释如下(目前还未找到优化方法)

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int main()
 4 {
 5     char ch[50000][10];
 6     int r,c;
 7     ios::sync_with_stdio(false);
 8     cin>>r>>c;
 9     for(register int i=0;i<r;i++)
10     {
11         for(register int j=0;j<c;j++)
12         {
13             cin>>ch[i][j];
14         }
15     }
16     for(register int i=0;i<r;i++)
17     {
18         for(register int j=0;j<c;j++)
19         {
20             if(ch[i][j]=='.')//如果是'.'并且下一层是'a',其实可以合并为一个if,只不过我看着这样舒服 
21             {
22                 if(ch[i+1][j]=='a')
23                 {
24                     int temp=i;//需要用一个临时的变量来储存i,否则就会乱套 
25                     while(temp>=0&&ch[temp][j]=='.')//循环到第0层并且如果这个坐标的字符是'.' 
26                     {
27                         ch[temp][j]='a';
28                         ch[temp+1][j]='.';//交换位置 
29                         temp--;//到上一层 
30                     }
31                 }
32             }
33         }
34     }
35         for(register int i=0;i<r;i++)
36     {
37         for(register int j=0;j<c;j++)
38         {
39             cout<<ch[i][j];
40         }
41         cout<<endl;
42     }
43     return 0;
44 }

 

posted @ 2022-02-20 21:13  江上舟摇  阅读(67)  评论(0编辑  收藏  举报