QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#214053 | #6723. Grid with Arrows | yujie3# | RE | 2ms | 10244kb | C++20 | 3.4kb | 2023-10-14 17:01:20 | 2023-10-14 17:01:20 |
Judging History
answer
#include<bits/stdc++.h>
#define endl '\n'
#define fast() ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
#define _ int _T; cin >> _T; while(_T --)
#define int long long
using namespace std;
const int N = 1e5 + 10;
const int MAX = 1e3 + 10;
int n,m;
string s[N];
vector<int> g[N];
int a[MAX][MAX];
int r[N];
int p[MAX];
bool st[MAX];
//struct dsu{
// vector<size_t>pa,size;
//
// explicit dsu(size_t size_):pa(size_),size(size_,1){
// iota(pa.begin(),pa.end(),0);
// }
//
// size_t find(size_t x){
// return pa[x] == x ? x : pa[x] = find(pa[x]);
// }
//
//
// void unite(size_t x,size_t y){
// x = find(x),y = find(y);
// if(x == y)return;
// if(size[x] >size[y])swap(x,y);
// p[y] = x;
// size[x] += size[y];
// }
//
//};
//
//
//void slv(){
//
// int n,m;
// cin>>n>>m;
//
// dsu d(n*m + 6);
//
// for(int i = 1;i <= n;i ++){
// cin >> s[i];
// }
//
// int pos = 1;
//
// for(int i = 1;i <= n;i ++){
// for(int j = 1;j <= m;j ++){
// cin >> a[i][j];
// }
// }
//
// for(int i = 1;i <= n;i ++){
// for(int j = 0;j < m;j ++){
// if(s[i][j] == 'r' && j + 1 + a[i][j + 1] <= m){
// cout<< "hb : x "<<pos<<" z"<<pos + a[i][j + 1]<<endl;
// d.unite(pos,pos + a[i][j + 1]);
// }
// if(s[i][j] == 'l' && j + 1 - a[i][j + 1] >= 1){
// cout<< "hb : x "<<pos<<" z"<<j + 1 - a[i][j + 1] <<endl;
// d.unite(pos,pos - a[i][j + 1]);
// }
// if(s[i][j] == 'u' && i - a[i][j + 1] >= 1){
// cout<< "hb : x "<<pos<<" z"<<pos - a[i][j + 1] * m<<endl;
// d.unite(pos,pos - a[i][j + 1] * m);
// }
// if(s[i][j] == 'd'){
// cout<< "hb : x "<<pos<<" z"<<pos + a[i][j + 1] * m<<endl;
// d.unite(pos,pos + a[i][j + 1] * m);
// }
// pos ++;
// }
// }
//
// for(int i = 1;i<=m*n;i++){
//
// cout<<d.find(i) <<" sz "<<d.size[i]<<endl;
// }
//
// if(d.size[1] == m*n)puts("YES");
// else puts("NO");
//
//}
void bfs(){
queue<int> q;
for(int i = 1;i <= n * m;i ++){
if(!r[i]){
q.push(i);
break;
}
} if(!q.size()) q.push(1);
int ans = 0;
while(q.size()){
int t = q.front();
q.pop();
if(st[t]) continue;
st[t] = true;
// cout << t << "TTT" << endl;
ans ++;
for(auto x : g[t]){
r[x] --;
q.push(x);
}
}// cout << ans << endl;
if(ans != n * m) cout << "NO" << endl;
else cout << "YES" << endl;
}
void solve(){
int pos = 1;
cin >> n >> m;
for(int i = 1;i <= n * m;i ++){
st[i] = false;
g[i].clear();
}
for(int i = 1;i <= n;i ++){
cin >> s[i];
}
for(int i = 1;i <= n;i ++){
for(int j = 1;j <= m;j ++){
cin >> a[i][j];
}
}
for(int i = 1;i <= n;i ++){
for(int j = 0;j < m;j ++){
if(s[i][j] == 'r' && j + 1 + a[i][j + 1] <= m){
g[pos].push_back(pos + a[i][j + 1]);
r[pos + a[i][j + 1]] ++;
}
if(s[i][j] == 'l' && j + 1 - a[i][j + 1] >= 1){
g[pos].push_back(pos - a[i][j + 1]);
r[pos - a[i][j + 1]] ++;
}
if(s[i][j] == 'u' && i - a[i][j + 1] >= 1){
g[pos].push_back(pos - a[i][j + 1] * m);
r[pos - a[i][j + 1] * m] ++;
}
if(s[i][j] == 'd'){
g[pos].push_back(pos + a[i][j + 1] * m);
r[pos + a[i][j + 1] * m] ++;
}
pos ++;
}
}
bfs();
// for(int i = 1;i <= n * m;i ++){
// for(auto t : g[i]){
// cout << t << " " << r[t] ;
// } cout << endl;
// }
}
signed main(){
// fast();
_
solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 10244kb
input:
2 2 3 rdd url 2 1 1 1 1 2 2 2 rr rr 1 1 1 1
output:
YES NO
result:
ok 2 token(s): yes count is 1, no count is 1
Test #2:
score: -100
Runtime Error
input:
1109 5 8 rddddldl drruludl rrldrurd urrrlluu uurrulrl 4 4 1 2 4 3 1 6 1 3 5 1 1 1 3 6 2 4 1 1 2 1 1 1 2 3 4 2 4 3 3 3 4 1 1 2 2 5 1 5 7 9 rdrddrdll urrdruldl ruullrulu drrlrlddl rrrdddlll ruulururl ruurrlluu 7 1 1 1 2 1 2 3 6 1 7 3 1 3 1 2 1 8 2 2 1 2 4 3 1 2 2 2 2 4 1 1 5 3 3 1 3 4 6 1 2 1 2 7 7 6 ...
output:
YES NO NO NO YES NO YES YES YES YES NO NO NO YES YES NO YES NO YES NO NO NO YES YES NO NO NO NO NO NO NO NO NO NO YES NO NO NO NO YES YES YES NO YES NO NO NO YES YES NO YES NO YES NO YES NO NO NO YES NO YES YES YES YES NO YES YES NO YES YES NO NO NO YES NO NO YES YES NO NO YES YES YES YES YES NO YES...