Try   HackMD

考古題

  • 以後會持續更新參考程式碼(由於有人嫌我的code太暴力
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
    所以只好全部重寫
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
    )
    盡量練到對一題半,這樣有機會三級分(全對+測資正確50%)

注意事項

  • 如果程式執行時間超時(TLE),就算測資正確也可能拿不到分數
  • 注意輸出格式要求,多或少空格都會沒有分數
  • 仔細檢查陣列大小,如果超過題目限制大小或太小,都沒料
  • 記得做 i/o 優化 程式碼如下(背一下,有時能卡時間),以下優化前後差別
    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →

    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →
i/o優化
//記得要寫在main()裡
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
  • 解不出來能暴力就暴力,只要分數不要優雅
  • 三四題要dp等一堆演算法 不可能學到,直接暴力不然就印測資

二級分考古題

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
:較難

參考ans
#include <bits/stdc++.h>
using namespace std;
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    set<int>s;
    int a,b,c;
    cin >> a >> b>> c;
    s.insert(a);
    s.insert(b);
    s.insert(c);
    cout << 4-s.size() << " ";
    vector<int> v;
    for(auto e:s){
       v.push_back(e);
    }
    for(int i=  v.size()-1;i>=0;i--){
        cout << v[i] <<" " ;
    }
    cout << endl;
    return 0;
}
  • 2. 程式交易
  • 3. 修補圍籬
  • 4. 七言對聯
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
    (我國文被當 這題可以問丹丹)
參考ans
#include <bits/stdc++.h>
using namespace std;
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int n;
    cin >> n ;
    while(n--){
            string s;
        int a[7],b[7];
        for(int i = 0; i < 7;i++){
            cin >> a[i];
        }
        for(int i = 0; i < 7;i++){
            cin >> b[i];
        }
        if(a[1] == a[3] or b[1] == b[3] or a[1]!=a[5] or b[1]!=b[5] ){
            s+='A';
        }
        if(a[6] !=1 or b[6] !=0){
            s+='B';
        }
        if(a[1]==b[1] or a[3] ==b[3] or a[5] == b[5] ){
            s+='C';

        }
        if(s.size()==0){
            cout <<"None" << endl;
        }
        else{
            cout << s << endl;
        }
    }

return 0;
}

參考ans
#include<bits/stdc++.h>
using namespace std;
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int n;
    cin >> n;
    int px= 0,py= 0;
    //1 E 2W 3 S 4 N
    int ew = 1;
    int l = 0 ,r= 0,re = 0;
    while(n--){
        int x,y;
        cin >> x >> y;
        if(ew == 1){
            if(x<px){
                re++;
                px = x;
                ew = 2;
            }
            else if(y<py){
                r++;
                py = y;
                ew = 3;

            }
            else if(y>py){
                l++;
                py = y;
                ew = 4;
            }
            else if(x>px){
                px = x;
            }
        }
        if(ew ==2 ){
            if(x-px >0){
                re++;
                px = x;
                ew = 1;
            }
            else if(y-py >0){
                r++;
                py = y;
                ew = 4;

            }
            else if(y-py < 0){
                l++;
                py = y;
                ew = 3;
            }
            else if(x<px){
                px = x;
            }
        }
        if(ew ==3 ){
            if(y-py >0){
                re++;
                py = y;
                ew = 4;
            }
            else if(x-px >0){
                l++;
                px = x;
                ew = 1;

            }
            else if(x-px < 0){
                r++;
                px = x;
                ew = 2;
            }
            else if(y<py){
                py = y;
            }
        }
        if(ew ==4 ){
            if(y-py <0){
                re++;
                py = y;
                ew = 3;
            }
            else if(x-px >0){
                r++;
                px = x;
                ew = 1;

            }
            else if(x-px < 0){
                l++;
                px = x;
                ew = 2;
            }
            else if(y>py){
                py = y;
            }
        }

    }
    cout << l <<" " << r << " " << re << endl;
    return 0;
}

參考ans
#include <bits/stdc++.h>
using namespace std;
int main(){
  	int x,n;
  cin >> x >>n;
  int a[n];
  vector<int> l,s;
   for(int i = 0; i <n;i++){
     cin >> a[i];
     if(a[i]>x){
       l.push_back(a[i]);
     }
     else{
       s.push_back(a[i]);
     }
  }
  if(s.size()>l.size()){
    cout << s.size() <<" ";
    sort(s.begin(),s.end());
    cout << s[0] << endl;
  }
  else{
    cout << l.size() <<" ";
    sort(l.begin(),l.end());
    cout << l[l.size()-1] << endl;
  }
 return 0; 
}
參考ans
#include <bits/stdc++.h>
using namespace std;
int a[105];
int main(){
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  cin >> n;
  for(int i= 0; i < n;i++){
    cin >> a[i];
  }
  int ans = 0;

  for(int i= 0; i < n;i++){
    int mx = a[i];
    int s = 0;
    for(int j = i+1;j < n;j++){
      if(mx>a[j]){
        mx = a[j];
        s++;
      }
      else{
        break;
      }
    }
    if(s>ans){
        ans = s;
    }
  }
  cout << ans+1 << endl;

return 0;
}

三級分考古題 二維陣列很重要
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

參考ans
#include <bits/stdc++.h>
using namespace std;
string ch(string ss){
    if(ss.size()%2 == 1){
            string cg;
        for(int i = (ss.size()-1)/2+1;i< ss.size();i++){
            cg+=ss[i];
        }
        cg+=ss[(ss.size()-1)/2];
        for(int i = 0; i < (ss.size()-1)/2;i++){
            cg+=ss[i];
        }
        return cg;
    }
    else{
            string cg;
        for(int i = ss.size()/2; i < ss.size();i++){
            cg+=ss[i];
        }
        for(int i = 0; i < ss.size()/2;i++){
            cg+=ss[i];
        }
        return cg;
    }
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int m,n;
    string a;
    cin >> m >> n;
    char c[m][n];
    for(int i = 0; i < m;i++){
        for(int j = 0 ; j < n;j++){
            cin >> c[i][j];
        }
    }
    cin >> a;
    for(int i = m-1; i >= 0;i--){
        deque<char>q;
        int ct = 0;
        for(int j = n-1 ; j >=0;j--){

            if(c[i][j]=='0'){
                q.push_front(a[j]);
            }
            else{
                ct++;
                q.push_back(a[j]);
            }
        }

        a.clear();
        for(int j = 0 ; j < n;j++){
                a+=q.front();
                q.pop_front();
        }
        if(ct%2==1 ){
            a = ch(a);
        }
    }
    cout << a << endl;

return 0;}
參考ans
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
int n,m;
ll s[100000];
ll t[100000];
int ot[100000] = {0};
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    vector<int> v;
    cin >> n >> m;
    for(int i =1; i <= n;i++ ){
        cin >> s[i];
    }
    for(int i = 1; i <=n;i++){
        cin >> t[i];
    }
    for(int i = 0; i < n;i++){
        int a;
        cin >>a;
        v.push_back(a);
    }
    vector<int> lnx;
    vector<int> wnx;
    while(v.size()>1){
        for(int i = 0; i <= v.size()-2;i+=2){
            ll a = s[v[i]];
            ll b = t[v[i]];
            ll c = s[v[i+1]];
            ll d = t[v[i+1]];
            if(a*b >=c*d){
                s[v[i]] = a+c*d/(2*b);
                t[v[i]] = b+c*d/(2*a);
                s[v[i+1]] = c+c/2;
                t[v[i+1]] = d+d/2;
                ot[v[i+1]]++;
                wnx.push_back(v[i]);
                if(ot[v[i+1]] < m){
                    lnx.push_back(v[i+1]);
                }
            }
            else{
                s[v[i+1]] = c+a*b/(2*d);
                t[v[i+1]] = d+a*b/(2*c);
                s[v[i]] = a+a/2;
                t[v[i]] = b+b/2;
                ot[v[i]]++;
                wnx.push_back(v[i+1]);
                if(ot[v[i]] < m){
                    lnx.push_back(v[i]);
                }
            }
        }
        if(v.size() %2 == 1){
            wnx.push_back(v[v.size()-1]);
        }
        v.clear();
        for(int i = 0; i < wnx.size();i++){
            v.push_back(wnx[i]);
        }
        wnx.clear();
        for(int i = 0; i < lnx.size();i++){
            v.push_back(lnx[i]);
        }
        lnx.clear();
    }
    cout <<v[0] << endl;

    return 0;
}
  • 3. 動線安排
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
    (應該是最複雜的考古題,這題會了可以開始學dfs bfs之類的走訪類型的演算法)
  • 4. 魔王迷宮
參考ans(二維vector的部分 把它當陣列)
#include <bits/stdc++.h>
using namespace std;
struct demon {
  bool alive = true;
  int r, c, s, t;
};
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, m, k;
  cin >> n >> m >> k;
  vector<demon> d(k);
  vector<vector<bool>> kill(n, vector<bool> (m));
    vector<vector<bool>> bomb(n, vector<bool> (m));
  for (int i = 0; i < k; i++)
    cin >> d[i].r >> d[i].c >> d[i].s >> d[i].t;
  int alive = k;
  while (alive) { 
    for (int i = 0; i < k; i++) {
      if (d[i].alive == false)
        continue;
      int x = d[i].r, y = d[i].c;
      bomb[x][y] = true;
    }
    for (int i = 0; i < k; i++) {
      if (d[i].alive == false)
        continue;
      int nx = d[i].r + d[i].s;
      int ny = d[i].c + d[i].t;
      if (nx >= n || nx < 0 || ny >= m || ny < 0) {
        d[i].alive = false; 
        alive--;            
      }                    
      else if (bomb[nx][ny]) {
        d[i].alive = false;  
        alive--;            
        kill[nx][ny] = true;
      } else {
       
        d[i].r = nx, d[i].c = ny;
      }
    }
    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++)
        if (kill[i][j])
          bomb[i][j] = false, kill[i][j] = false;
  }
  int ans = 0;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (bomb[i][j])
        ans++;
  cout << ans << endl;
  return 0;
}
參考ans
我題目看不懂:)
參考ans
#include<bits/stdc++.h>
using namespace std;
int mv[][2] = {{1,0},{-1,0},{0,1},{0,-1}};
int a[55][55];
int b[55][55];
int d[55][55];
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int r,c,k,m;
    memset(a,-1,sizeof(a));
    memset(b,0,sizeof(b));
    memset(d,0,sizeof(d));
   
    cin >> r >> c >> k >> m;
    for(int i = 1; i <= r;i++){
        for(int j  = 1;j<=c;j++ ){
            cin >> a[i][j];
        }
    }
    for(int z = 0;z<m;z++){
        for(int i = 1; i <= r;i++){
            for(int j  = 1;j<=c;j++ ){
                if(a[i][j]==-1  ){
                    continue;
                }   
                for(int l  = 0;l <4;l++){
                    int x = i + mv[l][0],y = j+mv[l][1];
                    if(a[x][y]!=-1){
                        b[x][y]+=a[i][j]/k;
                        d[i][j]+=a[i][j]/k;
                    }
                }
            }
        }
        for(int i = 1; i <= r;i++){
            for(int j  = 1;j<=c;j++ ){
                a[i][j]-=d[i][j];
            
                a[i][j]+=b[i][j];
            }
        }
        memset(d,0,sizeof(d));
        memset(b,0,sizeof(b));

    }
    int max = -1;
    int min = 101;
    for(int i = 1; i <= r;i++){
            for(int j  = 1;j<=c;j++ ){
                if(a[i][j]>max &&a[i][j]!=-1){
                    max = a[i][j];
                }
                if(a[i][j]<min&& a[i][j]!=-1){
                    min = a[i][j];
                }
            }
    }
    cout << min << endl<< max << endl;
    return 0;
}
參考ans
#include <bits/stdc++.h>
using namespace std;
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int s,t,n,m,r;
    cin >> s >> t >> n >> m >> r;
    int a[s][t];
    int b[n][m];
    int min = 30000;
    int as= 0;
    for(int i = 0;i < s;i++){
        for(int j = 0;j <t;j++){
            cin >> a[i][j];
            as+=a[i][j];
        }
    }
    for(int i = 0;i < n;i++){
        for(int j = 0;j <m;j++){
            cin >> b[i][j];
        }
    }
    int ans = 0;
    for(int i = 0;i < n-s+1;i++){
        for(int j = 0;j < m-t+1;j++){
            int cnt = 0;
            int sum = 0;
            for(int k = 0;k <s;k++){
                for(int l = 0; l < t;l++){
                    if(a[k][l] !=b[k+i][l+j]){
                        cnt++;
                    }    
                    sum +=b[k+i][l+j];
                }
            }         
            if(cnt<=r){
                ans++;
                int d = abs(as-sum);
                if(d < min){
                min = d;
                }
            }   
        }
    }
    cout << ans  << endl;
    if(ans == 0){
        cout << -1 << endl;
    }
    else{
        cout <<  min << endl;
    }
    return 0;
}
參考ans (5ms)
#include <bits/stdc++.h>
using namespace std;
int a[105][105];
bool v[105][105];
int s = 0;
int mv[][2] ={{0,1},{1,0},{0,-1},{-1,0}};
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    memset(v,0,sizeof(v));
    memset(a,0,sizeof(a));
    int n,m;
    cin >> n >> m;
    int min = 1000000;
    int x,y;
    for(int i = 0; i < n;i++){
        for(int j = 0; j < m;j++){
            cin >>a[i][j];
            if(min >a[i][j]){
                min = a[i][j];
                x= i;
                y = j;
            }
        }
    }
    v[x][y]=1;
    s+=a[x][y];
    while(1){
        vector<int> dr ;
        for(int i = 0; i < 4;i++){
            int dx = x+mv[i][0],dy = y+mv[i][1];
            if(dx>=0 and dx <n and dy>=0 and dy<m){
                if(v[dx][dy] == 0){
                    dr.push_back(a[dx][dy]);
                }
            }
                
        }
        if(dr.empty()){
            break;
        }
        else{
            sort(dr.begin(),dr.end());
            for(int i = 0; i < n;i++){
                for(int j = 0; j < m;j++){
                    if(a[i][j] == dr[0]){
                        x = i;
                        y = j;
                        break;
                    }
                }
            }
            s+=a[x][y];
            v[x][y] = 1;   
        }
    }
    cout << s << endl;
    return 0;
}
參考ans (4ms)
#include<bits/stdc++.h>
using namespace std;
int a[105][105];
bool v[105][105];
int mv[][2] ={{0,1},{0,-1},{1,0},{-1,0}};
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    memset(v,1,sizeof(v));
    int n,m;
    cin >> n >> m;
    int x,y,mn=1000001;
    for(int i = 1; i <= n;i++){
        for(int j = 1;j <=m;j++){
            cin >> a[i][j];
            v[i][j] = 0;
            if(mn>a[i][j]){
                mn = a[i][j];
                x =i;
                y = j;
            }
        }
    }
    int s = 0;
    while(1){
        s+=a[x][y];
        int px,py;
        v[x][y] = 1;
        int mxn = 1000001;
        for(int i= 0; i<4;i++){
            int dx = x+mv[i][0],dy = y+mv[i][1];
            if(v[dx][dy]== 0){
                if(mxn >a[dx][dy]){
                    mxn =a[dx][dy];
                    px = dx;
                    py =dy;
                }
            }
        }
        if(mxn==1000001){
            break;
        }
        else{
            v[px][py] = 1;
            x = px;
            y = py;
        }
    }

    cout<<s << endl;
    return 0;
}

參考ans(第二題目前遇過最難的segment 但有isupper讓我少寫判斷🙃)
#include<bits/stdc++.h>
using namespace std;
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    string s;
    int k;
    vector<int>v;
    cin >> k >> s;
    int c = 1;
    for(int i= 1; i < s.size();i++){
        if(isupper(s[i])== isupper(s[i-1])){
            c++;
        }
        else{
            v.push_back(c);
            c = 1;
        }
    }
    v.push_back(c);
    int su = 0;
    vector<int>sol;
    for(int i = 0; i < v.size();i++){
        if(v[i]==k){
            su++;
        }
        else{
            if(i-su-1>=0&&v[i-su-1] >k){
                su++;
            }
            if(v[i]>k){
                su++;
            }
            sol.push_back(su);
            su = 0;
        }
       
    }
    if(su>0){
        if(v[v.size()-1-su] >k){
            su++;
        }
        sol.push_back(su);
        
    }
    sort(sol.begin(),sol.end());

    cout << sol[sol.size()-1]*k << endl;


return 0;}

參考ans(
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
9ms考試可能爆)
#include <bits/stdc++.h>
using namespace std;
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int a;
    cin >> a;
    int b[a];
    for(int i = 0; i < a;i++){
        cin >> b[i];

    }
    int s = 0;
    bool vv[a];
    bool vvs[a];
    memset(vvs,0,sizeof(vvs));
    memset(vv,0,sizeof(vv));
    for(int i = 0; i < a;i++){
            if(vvs[i] ==1){
                continue;

            }
            else{
                s++;
                int x = b[i];
                vvs[i] = 1;
                while(vv[i] != 1){
                    if(b[x] == b[i]){
                        vv[i] = 1;

                    }
                    else{
                        vvs[x] =1;
                        x = b[x];
                }
            }


        }
    }
    cout << s << endl;




return 0;}

參考ans1
#include <bits/stdc++.h>
using namespace std;
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(nullptr);
    int n,m;
    cin >> n >> m;
    int sum = 0;
    vector<int> x;
    int a[m];
    while(n--){
        for(int i = 0; i < m;i++){
            cin >> a[i];
        }
        int mx = 0;
        for(int i = 0; i < m; i++){
            if( a[i]>mx ){
                mx = a[i];
            }
        }
        x.push_back(mx);
        sum+=mx;
    }
    cout << sum << endl;
    int count = 0;
    for(int i = 0; i < x.size();i++){
        if(sum%x[i] == 0){
            count+=1;
        }
    }
    if(count == 0){
        cout << -1;
    }
    int y =0;
    for(int i = 0; i < x.size();i++){
        if(sum%x[i] == 0){

            cout << x[i];
            if(y != count-1){
                cout << " ";
                y++;
            }
        }
    }
return 0;
}
參考ans2 (濫用結構解)
#include <bits/stdc++.h>
using namespace std;
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int m,n;
    cin >> m >>n;
    vector<int>v;
    
    for(int i = 0; i < m;i++){
        int a[n];
        for(int j = 0; j< n;j++){
            cin >> a[j];
            
        }
        sort(a,a+n);
        v.push_back(a[n-1]);
    }
    int s = 0;
    for(int i=0; i < v.size();i++){
        s+=v[i];
    }
    cout << s<< endl;
    vector<int>vv;

   for(int i =0 ;i < v.size();i++){
    if(s%v[i] == 0 ){
        vv.push_back(v[i]);
    }
   }

   if(vv.empty()){
        cout << -1<< endl;
   }
   else{
    for(int i=0; i < vv.size();i++){
        if(i != 0){
            cout <<" " ;
        }
        cout << vv[i];
    }
   }
    return 0;
}
參考ans (那個a陣列最好不要設太大,我只是懶得定大小隨便寫的數字)
#include <bits/stdc++.h>
int a[50][50];
int b[10][10];
using namespace std;
int main(){
    int r,c,k;
    cin >> r >> c >> k;
    
    for(int i= 0;  i < r;i++){
        for(int j = 0; j < c;j++){
            cin >> a[i][j];
        }

    }
    int xx[k];
    for(int x = 0; x < k;x++){
        cin >> xx[x];
    }
    for(int x = k-1; x >= 0;x--){
        if(xx[x] == 1){
            for(int i= 0; i <r/2;i++){
                for(int j = 0; j < c;j++){
                    swap(a[i][j],a[r-1-i][j]);
                }
            }
            
        }
        if(xx[x]== 0){
            
            for(int i = 0; i < r;i++){
                for(int j = 0; j< c;j++){
                    b[i][j] =a[i][j];
                }
            }
            swap(r,c);
            for(int i = 0; i < r;i++){
                for(int j = 0; j< c;j++){
                   a[i][j] = b[j][r-i-1];
                }
            }
        }

    }
    cout<< r <<" " <<c << endl;
    for(int i = 0; i < r;i++){
        for(int j = 0; j< c;j++)
        {
           if(j !=0){
            cout << " ";
           } cout << a[i][j];
        }
        cout << endl;
    }
    

    return 0;
}
  • 17. 運貨站
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
參考ans
#include <bits/stdc++.h>
using namespace std;
int r, c;
int pt = 0;
bool v[35][55]; 
int cnt;
void A(int j){
	int i = c;  
	for(;i >= 0;i--){
		if(i == 0 ||v[j][i-1] || v[j+1][i-1] || v[j+2][i-1] || v[j+3][i-1]) break;

	}
	if(i >= c){
        cnt ++; 
    } 
	else {
		v[j][i] =1;
		v[j+1][i] = 1;
		v[j+2][i] = 1;
		v[j+3][i] = 1;
        pt+=4;
	}
}
void B(int j){
	int i = c+2;
	for(;i >= 2;i--){
		if(i == 2 || v[j][i-3]) break;
	}
	if(i >= c){
        cnt++;
    } 
	else {
		v[j][i] = 1;
		v[j][i-1] = 1;
		v[j][i-2] = 1;
		 pt+=3;
	}
}
void C(int j){
	int i = c+1;
	for(;i >= 1;i--){
		if(i == 1 || v[j][i-2] || v[j+1][i-2]) break;
	}
	if(i >= c) 
    {cnt ++;
    }
	else {
		v[j][i] = 1;
		v[j+1][i] =1;
		v[j][i-1] =1;
		v[j+1][i-1] = 1;
		 pt+=4;
	}
}
void D(int j){
	int i = c+2;
	for(;i >= 2;i--){
		if(i == 2 || v[j+1][i-3] || v[j][i-1]) break;
	}
	if(i >= c){
        cnt++;
    } 
	else {
		v[j+1][i] = 1;
		v[j+1][i-1] = 1;
		v[j+1][i-2] = 1;
		v[j][i] = 1;
         pt+=4;
		
	}
}
void E(int j){
	int i = c+1;
	for(;i >= 1;i--){
		if(i == 1 || v[j][i-1] ||v[j+1][i-2] || v[j+2][i-2]) break;
	}
	if(i >= c){
        cnt++;
    } 
	else {
		v[j][i] = 1;
		v[j+1][i] = 1;
		v[j+2][i] = 1;
		v[j+1][i-1] = 1;
		v[j+2][i-1] = 1;
		 pt+=5;
	}
}
int main(){
	int n;
	cin >> r >> c >> n;
    char t[n];
    int y[n];
    for(int i = 0; i < n;i++){
        cin >> t[i] >>y[i];
        if(t[i] == 'A') 
        {
            A(y[i]);
        }
		if(t[i] == 'B') 
        {
            B(y[i]);
        }
        if(t[i] == 'C') 
        {
            C(y[i]);
        }
        if(t[i] == 'D') 
        {
            D(y[i]);
        }
        if(t[i] == 'E') 
        {
            E(y[i]);
        }
    }
	
	cout << r*c-pt << ' ' << cnt << endl;
    return 0;
}
參考ans
#include<bits/stdc++.h>
using namespace std;
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int k,q,r;
    cin >> k >> q>> r;
    char ch[q+1][k];
    cin >> ch[0];
    for(int i= 1; i <= q;i++){
        for(int j = 0; j < k;j++){
            int s ;
            cin >> s;
            ch[i][s-1] =ch[i-1][j]; 
        }
    }
    for(int i = 0;i <r;i++){
        for(int j = 1; j <=q;j++ ){
            cout << ch[j][i];
        }
        cout << endl;
    }


    return 0;
}
參考ans1(
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
10ms 但重新從頭搜尋可以不需考慮字典序)
#include <bits/stdc++.h> using namespace std; int a[55][55]; int main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); int n,m; cin >> n >> m; vector<pair<int,int>>v; for(int i = 0; i < n;i++){ for(int j = 0; j < m;j++){ cin >> a[i][j]; } } for(int i = 0; i < n;i++){ for(int j = 0; j < m;j++){ int x = a[i][j]; int dx = i,dy = j; int s = 0; for(int k = 0; k < n;k++){ for(int l = 0; l < m;l++){ if((abs(dx-k)+abs(dy-l))<=x){ s+=a[k][l]; } } } if(s%10 == x){ v.push_back({i,j}); } } } cout << v.size() << endl; for(int i = 0; i <v.size();i++){ cout << v[i].first<<" " << v[i].second << endl; } return 0; }
參考ans2(bfs未修正)
#include <bits/stdc++.h> int mp[][2] = {{0,1},{0,-1},{1,0},{-1,0}}; using namespace std; int a[55][55]; int n,m; int bfs(int i,int j,int x){ bool v[55][55]; int s= 0; queue<pair<int , int>>q; v[i][j] = 1; q.push({i,j}); while(!q.empty()){ int x = q.front().first,y = q.front().second; q.pop(); for(int i = 0; i < 4;i++){ int dx =x+mp[i][0],dy = y+mp[i][1]; if(dx <n && dx>=0 &&dy<m &&dy>=0){ if((abs(dx-i)+abs(dy-j))<=x && !v[dx][dy]){ q.push({dx,dy}); v[dx][dy] = 1; s+=a[dx][dy]; } } } } return s; } int main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> n >> m; vector<pair<int,int>>v; for(int i = 0; i < n;i++){ for(int j = 0; j < m;j++){ cin >> a[i][j]; } } for(int i = 0; i < n;i++){ for(int j = 0; j < m;j++){ if(bfs(i,j,a[i][j])%10 ==a[i][j]); { v.push_back({i,j}); } } } cout << v.size() << endl; for(int i = 0; i <v.size();i++){ cout << v[i].first<<" " << v[i].second << endl; } return 0; }
參考ans
#include<bits/stdc++.h>
using namespace std;
char a[105][105];
int mv[][2] ={{-1,0},{0,1},{1,1},{1,0},{0,-1},{-1,-1}};
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int n,m,k;
    cin >> n >> m >> k;
    memset(a,'*',sizeof(a));
    for(int i = 1; i <= n;i++){
        for(int j = 1; j <= m;j++){
            cin >>a[i][j];
        }
    }
    string s;
    int x = n,y = 1;
    while(k--){
        int t;
        cin >> t;
        int dx = x+mv[t][0];
        int dy = y+mv[t][1];
        if(a[dx][dy]=='*'){
            s+=a[x][y];
        }
        else{
            s+=a[dx][dy];
            x = dx;
            y = dy;
        }
    }
    cout << s << endl;
    set<char>ss;
    for(int i = 0; i < s.size();i++){
        ss.insert(s[i]);
    }
    cout << ss.size() << endl;
    return 0;
}

Python code
m,n,k = map(int,input().split())
a = [[0]*n for i in range(m)]
for i in range(m):
    s = list(input())
    for j in range(n):
        a[i][j] = s[j]
mv = list(map(int,input().split()))
def mov(mv, x,y):
    if mv == 0:
        return x + 0, y - 1
    elif mv == 1:
        return x + 1, y + 0
    elif mv == 2:
        return x + 1, y + 1
    elif mv == 3:
        return x + 0, y + 1
    elif mv == 4:
        return x - 1, y + 0
    elif mv == 5:
        return x - 1, y - 1
x= 0
y = m-1
ans = []
for i in range(k):
    movv = mv[i]
    xx = x
    yy= y
    x , y = mov(movv,x,y)
    if x >= 0 and x < n and y >=0 and y <m:
       ans.append(a[y][x])
    else:
        x = xx
        y = yy
        ans.append(a[y][x])
print("".join(ans))
print(len(set(ans)))

參考ans
#include <bits/stdc++.h>
using namespace std;
int main(){
    int n,m;
    cin >> n >> m;
    int a[n][m];
    for(int i = 0; i < n;i++){
        for(int j = 0; j < m;j++){
            cin >> a[i][j];
        }
    }
    int s = 0;
    for(int z = 0;z<n*m/2+1;z++){
        for(int i = 0; i < n;i++){
            for(int j = 0; j < m;j++){
                if(a[i][j] == -1){
                    continue;
                }
                for(int k =j+1; k <m;k++ ){
                    if(a[i][k] == -1){
                        continue;
                    }
                    if(a[i][k]==a[i][j]){
                        s+=a[i][k];
                        a[i][k] = a[i][j] = -1;
                        break;
                    }
                    else{
                        break;
                    }
                    
                }
                for(int k =i+1; k <n;k++ ){
                    if(a[k][j] == -1){
                        continue;
                    }
                    if(a[i][j]==a[k][j]){
                        s+=a[k][j];
                        a[k][j] = a[i][j] = -1;
                        break;
                    }
                    else{
                        break;
                    }
                    
                }
            }
            
        
        }
    }
    
    cout << s << endl;


    return 0;
}
Python code
n,m = map(int,input().split())
a =[]
for i in range (n):
    a.append([int(y) for y in input().split()])
pt = 0 
k = int(m*n/2)
for z in range(0,k):
    for i in range(n):
        for j in range (m):
            if a[i][j] != -1:
                for l in range(j+1,m):
                    if a[i][l] != -1 and a[i][l]!=a[i][j]:
                        break;
                    elif a[i][l] == -1:
                        continue
                    else:
                        pt +=a[i][j]
                        a[i][j] = -1
                        a[i][l] = -1
                for l in range(i+1,n):
                    if a[l][j] != -1 and a[l][j]!=a[i][j]:
                        break;
                    elif a[l][j] == -1:
                        continue
                    else:
                        pt +=a[i][j]
                        a[i][j] = -1
                        a[l][j] = -1
print(pt)
                            
                            

                
參考ans
#include <bits/stdc++.h>
using namespace std;
char a[9][9];
int mv[][8] = {{-1,0},{-1,1},{0,1},{1,1},{1,0,},{1,-1},{0,-1},{-1,-1}};
string ss ="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
int px(int x,int t){
    int nx = x+mv[t][0];
    if(nx == -1){
        nx = 7;
    }
    if(nx ==8){
        nx = 0;
    }
    return nx;
}
int py(int y,int t){
    int ny = y+mv[t][1];
    if(ny==-1){
        ny = 7;
    }
    if(ny==8){
        ny = 0;
    }
    return ny;
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int p = 0;
    int n;
    cin >> n;
    for(int i = 0;i <8;i++){
        for(int j = 0; j <8;j++){
            a[i][j] = ss[p++];
        }
        
    }
    while(n--){
        char s;
        int t;
        int N;
        cin >> s >> t >> N;
        int x,y;
        for(int i = 0;i <8;i++){
            for(int j = 0; j <8;j++){
                if(a[i][j] ==  s){
                    x =i;
                    y =j;
                    break;
                }
            }
        
        }
        for(int i = 1; i<=N;i++){
            for(int j = 0; j <i;j++){
                int nx  = px(x,t);
                int ny = py(y,t);
                x = nx;
                y = ny;
            }
            t++;
            if(t == 8){
                t =0;
            }
        }
        cout << a[x][y] << endl;
    }
    



    return 0;
}
參考ans (笑死根本低配版特殊位置)
#include <bits/stdc++.h>
using namespace std;
int a[105][105];
int main(){
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  memset(a,0,sizeof(a));
  int n,m,r;
  cin >> n >> m >> r;
  while(r--){
    int rr,c,t,x;
    cin >> rr >> c >> t >> x;
    for(int i= 0; i < n;i++){
      for(int j = 0;j < m;j++){
        if((abs(i-rr)+abs(j-c))<=t){
          a[i][j]+=x;

        }
      }
    }
  }
  for(int i= 0; i < n;i++){
    for(int j = 0;j < m;j++){
      cout << a[i][j]<<" ";
    }
    cout << endl;
  }

return 0;
}