QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#499669 | #6723. Grid with Arrows | Dawnkk03 | ML | 0ms | 3576kb | C++23 | 3.2kb | 2024-07-31 16:46:41 | 2024-07-31 16:46:43 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct Node
{
int dis;
char arrow;
Node* parent = nullptr;
Node* find(Node* node)
{
if (node->parent == nullptr)
return node;
return node->parent = find(node->parent);
}
void merge(Node* node)
{
Node* a = find(this);
Node* b = find(node);
if (a != b)
{
b->parent = a;
}
}
};
void solve()
{
int n,m;
cin>>n>>m;
vector<vector<Node>> Map(n+1, vector<Node>(m+1));
int cnt = 0;//记录环的数量
for(int i = 1;i <= n;i++)
{
for(int j = 1;j <= m;j++)
{
cin>>Map[i][j].arrow;
}
}
for(int i = 1;i <= n;i++)
{
for(int j = 1;j <= m;j++)
{
cin>>Map[i][j].dis;
}
}
for(int i = 1;i <= n;i++)
{
for(int j = 1;j <= m;j++)
{
if(Map[i][j].arrow == 'd')
{
if(i + 1 > n)
{
Map[i][j].parent = Map[i][j].find(&Map[i][j]);
cnt++;
}
else if(Map[i+1][j].find(&Map[i+1][j]) == Map[i][j].find(&Map[i][j]))
{
cnt++;
}
else
{
Map[i+1][j].merge(&Map[i][j]);
}
}
else if(Map[i][j].arrow == 'u')
{
if(i - 1 < 1)
{
Map[i][j].parent = Map[i][j].find(&Map[i][j]);
cnt++;
}
else if(Map[i-1][j].find(&Map[i-1][j]) == Map[i][j].find(&Map[i][j]))
{
cnt++;
}
else
{
Map[i-1][j].merge(&Map[i][j]);
}
}
else if(Map[i][j].arrow == 'l')
{
if(j - 1 < 1)
{
Map[i][j].parent = Map[i][j].find(&Map[i][j]);
cnt++;
}
else if(Map[i][j-1].find(&Map[i][j-1]) == Map[i][j].find(&Map[i][j]))
{
cnt++;
}
else
{
Map[i][j-1].merge(&Map[i][j]);
}
}
else if(Map[i][j].arrow == 'r')
{
if(j + 1 > m)
{
Map[i][j].parent = Map[i][j].find(&Map[i][j]);
cnt++;
}
else if(Map[i][j+1].find(&Map[i][j+1]) == Map[i][j].find(&Map[i][j]))
{
cnt++;
}
else
{
Map[i][j+1].merge(&Map[i][j]);
}
}
if(cnt >= 2)
{
cout<<"No"<<endl;
return;
}
}
}
cout<<"Yes"<<endl;
}
int main()
{
ll T = 1;
cin>>T;
while (T--)
{
solve();
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3576kb
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
Memory 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:
No No No No Yes No No No No No No No No No No No No No Yes No No No Yes No No No No No Yes No No No No No Yes No No No No Yes No No No No No No No No No No No No No No Yes No No No No No No Yes No No No No No No No No No No No No No No No No No No No No Yes No Yes No Yes No No No Yes No No No No No ...