QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#499566 | #6723. Grid with Arrows | Ink_bai | WA | 55ms | 8164kb | C++20 | 3.5kb | 2024-07-31 15:46:45 | 2024-07-31 15:46:46 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
int a[1000005], b[1000005], f[1000005];
char s[1000005];
int Find(int x)
{
if(x == f[x])
{
return x;
}
return f[x] = Find(f[x]);
}
void Union(int x, int y)
{
int a, b;
a = Find(x);
b = Find(y);
if(a != b)
{
f[a] = b;
}
}
int updiv(int a, int b)
{
return a / b + (a % b ? 1 : 0);
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int T;
cin >> T;
while (T--)
{
int n, m;
cin >> n >> m;
for (int i = 1; i <= n * m; i++)
{
a[i] = b[i] = 0;
char c;
cin >> c;
s[i] = c;
}
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= m; j++)
{
int x;
int it = (i - 1) * m + j;
cin >> x;
if(s[it] == 'u')
{
a[it]++;
if(i - x >= 1)
{
int it2 = (i - x - 1) * m + j;
b[it2]++;
Union(it, it2);
}
}
else if(s[it] == 'd')
{
a[it]++;
if(i + x <= n)
{
int it2 = (i + x - 1) * m + j;
b[it2]++;
Union(it, it2);
}
}
else if(s[it] == 'l')
{
a[it]++;
if(j - x >= 1)
{
int it2 = (i - 1) * m + j - x;
b[it2]++;
Union(it, it2);
}
}
else if(s[it] == 'r')
{
a[it]++;
if(j + x <= m)
{
int it2 = (i - 1) * m + j + x;
b[it2]++;
Union(it, it2);
}
}
}
}
int t = Find(1), fflag = 0;
for(int i=2;i<=m*n;i++){
if(t!=Find(i)){
fflag = 1;
break;
}
}
if(fflag==1)
{
cout << "No" << endl;
continue;
}
int u = 0, v = 0;
bool flag = false;
for(int i = 1; i <= n * m; i++)
{
if(flag||u > 1||v > 1)
{
flag = true;
break;
}
else if(a[i] == b[i]&& b[i] == 0)
{
flag = true;
break;
}
else if(a[i] - b[i] == 1)
{
u++;
}
else if(b[i] - a[i] == 1)
{
v++;
}
else if(a[i] == b[i])
{
continue;
}
else
{
flag = true;
}
}
if(u > 1||v > 1)
{
flag = true;
}
if(flag)
{
cout << "No" << endl;
continue;
}
if(u == 1&&v == 1||u == 0&&v == 0)
{
cout << "Yes" << endl;
}
else
{
cout << "No" << endl;
}
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 7672kb
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
Wrong Answer
time: 55ms
memory: 8164kb
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 Yes No No Yes No Yes Yes Yes Yes No Yes No Yes Yes No Yes No Yes No No No Yes Yes Yes Yes No No Yes Yes No Yes Yes No Yes Yes Yes Yes No Yes Yes Yes No Yes Yes Yes No Yes Yes Yes Yes Yes Yes No Yes Yes No Yes Yes Yes Yes Yes Yes Yes No Yes Yes Yes Yes Yes Yes No Yes Yes No No Yes Yes Yes Yes Yes...
result:
wrong answer expected NO, found YES [2nd token]