QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#499555 | #6723. Grid with Arrows | Umok | RE | 0ms | 0kb | C++20 | 2.1kb | 2024-07-31 15:41:02 | 2024-07-31 15:41:02 |
answer
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
const int N = 1e5 + 5;
#define int long long
typedef pair<int, int> PII;
#define MAX LONG_LONG_MAX
string mp[N];
vector<int> ar[N], f;
vector<int> st[N];
int n, m;
int finds(int x)
{
return f[x] == x ? f[x] : f[x] = finds(f[x]);
}
PII find(int x, int y)
{
int xx = x, yy = y;
if (mp[x][y] == 'r')
yy += ar[x][y];
else if (mp[x][y] == 'd')
xx += ar[x][y];
else if (mp[x][y] == 'l')
yy -= ar[x][y];
else
xx -= ar[x][y];
if (xx < 1 || xx > n || yy < 0 || yy > m)
return {-1, -1};
return {xx, yy};
}
void solve()
{
cin >> n >> m;
int ans = n * m;
f.clear();
for (int i = 1; i <= n; i++)
{
ar[i].clear();
st[i].clear();
}
for (int i = 1; i <= n; i++)
{
cin >> mp[i];
mp[i] = " " + mp[i];
}
for (int i = 1; i <= n; i++)
{
ar[i].push_back(0);
f.push_back(0);
st[i].push_back(0);
for (int j = 1; j <= m; j++)
{
int x;
cin >> x;
ar[i].push_back(x);
f.push_back(0);
st[i].push_back(0);
}
}
st[0].push_back(0);
for (int i = 1; i <= ans; i++)
f[i] = i;
int num = 1;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
st[i][j] = num++;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
{
auto a = find(i, j);
int x = a.first, y = a.second;
int _ = f[st[i][j]], __ = f[st[x][y]];
if (_ == __ || _ < 1 || __ < 1)
continue;
f[finds(__)] = finds(_);
}
for (int i = 1; i <= ans; i++)
finds(i);
map<int, int> q;
for (int i = 1; i <= ans; i++)
q[f[i]]++;
if (q.size() == 1)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
signed main()
{
int tcase;
cin >> tcase;
while (tcase--)
solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Runtime Error
input:
2 2 3 rdd url 2 1 1 1 1 2 2 2 rr rr 1 1 1 1
output:
Yes