結果
| 問題 | 
                            No.2064 Smallest Sequence on Grid
                             | 
                    
| コンテスト | |
| ユーザー | 
                            👑  | 
                    
| 提出日時 | 2022-09-02 22:05:21 | 
| 言語 | C++17(gcc12)  (gcc 12.3.0 + boost 1.87.0)  | 
                    
| 結果 | 
                             
                                TLE
                                 
                             
                            
                         | 
                    
| 実行時間 | - | 
| コード長 | 1,438 bytes | 
| コンパイル時間 | 1,877 ms | 
| コンパイル使用メモリ | 196,048 KB | 
| 最終ジャッジ日時 | 2025-02-07 01:17:18 | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge1 / judge3 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 2 | 
| other | AC * 10 TLE * 19 | 
ソースコード
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define CIN( LL , A ) LL A; cin >> A 
#define FOR( VAR , INITIAL , FINAL_PLUS_ONE ) for( int VAR = INITIAL ; VAR < FINAL_PLUS_ONE ; VAR ++ ) 
#define RETURN( ANSWER ) cout << ( ANSWER ) << "\n"; return 0 
constexpr const ll A = 3000;
void Solve( string ( &S )[A] , ll& i , ll& j , string& s , const ll& H_m , const ll& W_m );
int main(){
  CIN( ll , H );
  CIN( ll , W );
  string S[A];
  FOR( i , 0 , H ){
    cin >> S[i];
  }
  string s{};
  ll i = 0;
  ll j = 0;
  ll H_m = H - 1;
  ll W_m = W - 1;
  Solve( S , i , j , s , H_m , W_m );
  s += S[i].substr( j , 1 );
  RETURN( s );
}
void Solve( string ( &S )[A] , ll& i , ll& j , string& s , const ll& H_m , const ll& W_m )
{
  while( i != H_m || j != W_m ){
    s += S[i].substr( j , 1 );
    if( i == H_m ){
      j++;
    } else if( j == W_m ){
      i++;
    } else {
      auto ci = S[i+1].substr( j , 1 ).c_str()[0];
      auto cj = S[i].substr( j+1 , 1 ).c_str()[0];
      if( ci < cj ){
	i++;
      } else if( cj < ci ){
	j++;
      } else {
	string si{};
	string sj{};
	ll i_copy_i = i + 1;
	ll j_copy_i = j;
	ll i_copy_j = i;
	ll j_copy_j = j + 1;
	Solve( S , i_copy_i , j_copy_i , si , H_m , W_m );
	Solve( S , i_copy_j , j_copy_j , sj , H_m , W_m );
	if( si < sj ){
	  s += si;
	  i = i_copy_i;
	  j = j_copy_i;
	} else {
	  s += sj;
	  i = i_copy_j;
	  j = j_copy_j;
	}
      }
    }
  }
  return;
}