QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#394434#7751. Palindrome PathLiWenXWA 0ms3852kbC++203.0kb2024-04-20 14:45:392024-04-20 14:45:41

Judging History

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

  • [2024-04-20 14:45:41]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3852kb
  • [2024-04-20 14:45:39]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
int n,m;
char ch[35][35];
int id(int x,int y){
	return (x-1)*m+y;
}
int fa[1001];
int find(int x){
	if(x==fa[x]) return x;
	return fa[x]=find(fa[x]);
}
void merge(int x,int y){
	x=find(x),y=find(y);
	if(x==y) return ;
	fa[x]=y;
}
int dx[]={0,0,1,-1};
int dy[]={1,-1,0,0};
int f[35][35][4];
vector<char> ans;
int sx,sy,tx,ty;
void push(int flag,int p){
	while(p--){
		if(flag==0) ans.push_back('R');
		if(flag==1) ans.push_back('L');
		if(flag==2) ans.push_back('D');
		if(flag==3) ans.push_back('U');
	}
}
void move(int x,int y,int flag){
	int k=f[x][y][flag^1];
	int p=f[tx][ty][flag];
	if(k<p) push(flag^1,k+1),push(flag,k+1);
	else push(flag^1,p),push(flag,p+1);
}
void move(int x,int y,int X,int Y){
	cout<<x<<' '<<y<<' '<<X<<' '<<Y<<'\n';
	if(x==X&&y==Y) return ;
	if(Y==y+1) move(x,y,0);
	if(Y==y-1) move(x,y,1);
	if(X==x+1) move(x,y,2);
	move(x,y,3);
}
bool vis[35][35];
void dfs(int x,int y,int X,int Y){
	vis[x][y]=1;
	for(int i=0;i<4;i++){
		int u=x+dx[i],v=y+dy[i];
		if(u<1||u>n||v<1||v>m) continue;
		if(ch[u][v]=='0') continue;
		if(vis[u][v]) continue;
		vis[u][v]=1;
		move(x,y,u,v);
		dfs(u,v,x,y);
	}
	move(x,y,X,Y); 
}
vector<pair<int,int> > vec;
bool flag;
void find(int x,int y){
	vis[x][y]=1;
	if(flag) return ;
	vec.push_back(make_pair(x,y));
	if(x==tx&&y==ty){
		flag=1;
		return ;
	}
	for(int i=0;i<4;i++){
		int u=x+dx[i],v=y+dy[i];
		if(u<1||u>n||v<1||v>m) continue;
		if(ch[u][v]=='0') continue;
		if(vis[u][v]) continue;
		vis[u][v]=1;
		find(u,v);
	}
	if(flag) return ;
	vec.pop_back();
}
int main(){
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			fa[id(i,j)]=id(i,j);
			cin>>ch[i][j];
		}
	}
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			if(ch[i][j]=='0') continue;
			for(int k=0;k<4;k++){
				int x=i+dx[k];
				int y=j+dy[k];
				if(x<1||x>n||y<1||y>m) continue;
				if(ch[x][y]=='0') continue;
				merge(id(i,j),id(x,y));
			}
		}
	}
	cin>>sx>>sy>>tx>>ty;
	if(ch[sx][sy]=='0'||ch[tx][ty]=='0'){
		cout<<-1;return 0;
	}
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			if(ch[i][j]=='0') continue;
			if(find(id(i,j))!=find(id(sx,sy))){
				cout<<-1;return 0;
			}
		}
	}
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			if(ch[i][j]=='0') continue;
			int s=0;
			for(int k=j+1;k<=m;k++){
				if(ch[i][k]=='0') continue;
				s++;
			}
			f[i][j][0]=s;
			s=0;
			for(int k=j-1;k;k--){
				if(ch[i][k]=='0') continue;
				s++;
			}
			f[i][j][1]=s;
			s=0;
			for(int k=i+1;k<=n;k++){
				if(ch[k][j]=='0') continue;
				s++;
			}
			f[i][j][2]=s;
			s=0;
			for(int k=i-1;k;k--){
				if(ch[k][j]=='0') continue;
				s++;
			}
			f[i][j][3]=s;
		}
	}
	dfs(sx,sy,sx,sy);
	memset(vis,0,sizeof(vis));
//	cout<<'\n';
	find(sx,sy);
	for(int i=1;i<(int)vec.size();i++){
		move(vec[i-1].first,vec[i-1].second,vec[i].first,vec[i].second);
	}
	stack<char> s;
	for(char u:ans){
		cout<<u;s.push(u);
	}
	while(!s.empty()){
		cout<<s.top();s.pop();
	}
}

詳細信息

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 3852kb

input:

2 2
11
11
1 1 2 2

output:

1 1 1 2
1 2 2 2
2 2 2 1
2 1 2 2
2 2 1 2
1 2 1 1
1 1 1 1
1 1 1 2
1 2 2 2
RDUUDDUURLDURDUDURLDUURDUUDDUUUUDDUUDRUUDLRUDUDRUDLRUUDDUUDR

result:

wrong answer Line [name=User Output] equals to "1 1 1 2", doesn't correspond to pattern "-1|[LRUD]{0,1000000}"