QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#349999#6723. Grid with Arrowswudibaolong#WA 87ms5348kbC++142.4kb2024-03-10 11:44:572024-03-10 11:44:58

Judging History

你现在查看的是最新测评结果

  • [2024-03-10 11:44:58]
  • 评测
  • 测评结果:WA
  • 用时:87ms
  • 内存:5348kb
  • [2024-03-10 11:44:57]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
#define endl '\n'
int G[N];
int du[N];
char arrow[N];
int val[N];
int st[N];
int n,m;
int find_set(int x)
{
	if(st[x] != x) st[x] = find_set(st[x]);
	return st[x];
}
void union_set(int a,int b)
{
	int fa=find_set(a),fb=find_set(b);
	st[fa] = fb;	
}
bool check(int x,int y)
{
	if(x <0 || x>=n) return 0;
	if(y <0 || y>=m) return 0;
	return 1;
}

int main()
{
	int T;
	cin>>T;
	while(T--)
	{
		scanf("%d %d",&n,&m);
		
		for(int i=0;i<n*m;i++) st[i]=i;
		
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<m;j++)
			{
				char ch;
				scanf(" %c",&ch);
				arrow[i*m+j]=ch;
			}
		}
		
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<m;j++)
			{
				int num;
				scanf("%d",&num);
				val[i*m+j]=num;
			}
		}
		
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<m;j++)
			{
				if(arrow[i*m+j] == 'u')  
				{
					int num=(i-val[i*m +j])*m +j; 
					int x=i-val[i*m +j],y=j;
					if(!check(x,y)) continue; 
					
					G[i*m + j] = num;
					du[i*m+j]++;
					du[num]--;
					union_set(i*m+j,num);
				}
				else if(arrow[i*m+j] == 'd')  
				{
					int num=(i+val[i*m +j])*m +j; 
					int x=i+val[i*m +j],y=j;
					if(!check(x,y)) continue;
					
					G[i*m + j] = num;
					du[i*m+j]++;
					du[num]--;
					union_set(i*m+j,num);
				}
				else if(arrow[i*m+j] == 'l')  
				{
					int num=i*m +j-val[i*m +j]; 
					int x=i,y=j-val[i*m +j];
					if(!check(x,y)) continue;
					
					G[i*m + j] = num;
					du[i*m+j]++;
					du[num]--;
					union_set(i*m+j,num);
				}
				else if(arrow[i*m+j] == 'r')  
				{
					int num=i*m +j+val[i*m +j]; 
					int x=i,y=j+val[i*m +j]; 
					if(!check(x,y)) continue;
					
					G[i*m + j] = num;
					du[i*m+j]++;
					du[num]--;
					union_set(i*m+j,num);
				}
			}
		}
		bool flag=1;
		int root=find_set(0);
		for(int i=0;i<n*m;i++) 
			if(find_set(i) != root) flag=0;
		
		if(!flag)
		{
			cout<<"No"<<endl;
//			cout<<"未联通"<<endl; 
//			for(int i=0;i<n*m;i++)
//			{
//				
//				cout<<i<<" "<<G[i]<<" "<<endl;
//				cout<<arrow[i]<<" "<<val[i]<<endl;
//			}
			continue;
		}
		
		int cnt_1 = 0, cnt1=0;
		for(int i=0;i<n*m;i++) 
		{
			if(du[i] == -1) cnt_1++;
			if(du[i] == 1) cnt1++;
		}
		if(cnt_1 == 1 && cnt1 == 1)
		{
			cout<<"Yes"<<endl;
		}
		else if(cnt_1 == 0 && cnt1 == 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: 0ms
memory: 3696kb

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: 87ms
memory: 5348kb

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
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
Yes
No
No
No
No
No
No
No
No
No
No
No
No
No
Yes
...

result:

wrong answer expected YES, found NO [5th token]