QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#217116 | #6723. Grid with Arrows | shennc11 | TL | 1ms | 3420kb | C++17 | 1.6kb | 2023-10-16 15:09:27 | 2023-10-16 15:09:27 |
Judging History
answer
#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;
constexpr int maxn = 1e5;
void slove(){
int n,m;
cin>>n>>m;
vector<vector<int> >a(n,vector<int>(m));
vector<vector<int> >p(n,vector<int>(m));
string s[n];
for(int i = 0;i<n;i++){
cin>>s[i];
}
int pos = 1;
for(int i = 0;i<n;i++){
for(int j = 0;j<m;j++){
cin>>a[i][j];
p[i][j] = pos++;
}
}
pos = 1;
auto check = [&](int x,int y){
if(x >=n || x<0)return false;
if(y>=m || y<0)return false;
return true;
};
vector<int> g[n*m+5];
vector<int> d(n*m+5,0);
pos = 1;
for(int i = 0;i<n;i++){
for(int j = 0;j<m;j++){
int dx = i,dy = j;
if(s[i][j] == 'u'){
dx-=a[i][j];
}else if(s[i][j] =='d'){
dx+=a[i][j];
}else if(s[i][j] == 'l'){
dy-=a[i][j];
}else if(s[i][j] == 'r'){
dy+=a[i][j];
}
if(check(dx,dy)){
g[pos].push_back(p[dx][dy]);
d[p[dx][dy]] ++;
}
pos++;
}
}
auto bfs = [&](int x){
vector<int>st(n*m + 7);
queue<int>q;
st[x] = 1;
int tit = n*m;
q.push(x);
while(!q.empty()){
tit--;
int u = q.front();
q.pop();
for(auto &i:g[u]){
if(!st[i]){
q.push(i);
st[i] = 1;
}
}
}
if(tit== 0)return true;
else return false;
};
bool flag = 0;
for(int i = 1;i<=n*m;i++){
if(d[i] ==0){
if(bfs(i)){
flag = 1;
}
}else if(bfs(1)){
flag = 1;
}
}
if(flag)puts("YES");
else puts("NO");
}
int main(){
int t;cin>>t;
while(t--)
slove();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3420kb
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
Time Limit Exceeded
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 YES YES NO YES NO NO YES NO NO YES YES YES NO NO YES YES YES NO YES NO YES NO YES YES NO YES NO YES NO YES YES NO NO YES NO YES YES YES YES NO YES YES NO YES YES NO NO YES YES NO NO YES YES NO NO YES YES YES YES Y...