結果
| 問題 | No.2064 Smallest Sequence on Grid |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2022-09-02 21:40:47 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 615 ms / 3,000 ms |
| コード長 | 950 bytes |
| コンパイル時間 | 2,643 ms |
| コンパイル使用メモリ | 215,148 KB |
| 最終ジャッジ日時 | 2025-02-07 00:59:40 |
|
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 29 |
ソースコード
#include<bits/stdc++.h>
using namespace std;
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int H,W;
cin>>H>>W;
vector<vector<char>>A(H,vector<char>(W));
for(int i=0;i<H;i++){
for(int j=0;j<W;j++)cin>>A[i][j];
}
queue<array<int,2>>bfs;
bfs.push({0,0});
string ans="";
ans+=A[0][0];
vector<vector<bool>>vst(H,vector<bool>(W));
while(bfs.size()){
int s=bfs.size();
vector<pair<char,array<int,2>>>v;
for(int i=0;i<s;i++){
int x=bfs.front()[0],y=bfs.front()[1];
bfs.pop();
if(x+1<H&&!vst[x+1][y]){
v.push_back({A[x+1][y],{x+1,y}});
vst[x+1][y]=true;
}
if(y+1<W&&!vst[x][y+1]){
v.push_back({A[x][y+1],{x,y+1}});
vst[x][y+1]=true;
}
}
sort(v.begin(),v.end());
if(v.empty())break;
char c=v[0].first;
ans+=c;
for(auto i:v){
if(i.first==c){
bfs.push(i.second);
}
}
}
cout<<ans<<'\n';
}