Type: Interactive 1000ms 512MiB

棋盘上的棋子

You cannot submit for this problem because the contest is ended. You can click "Open in Problem Set" to view this problem in normal mode.

题目描述

这是一道交互题。

有一个 n×nn\times n 的棋盘,两枚棋子分别位于两个不同的格子中。你需要确定它们的初始位置。

每次操作可以选择一枚棋子,尝试将它向上、下、左或右移动一格。如果目标格子存在且未被另一枚棋子占据,移动成功;否则移动失败,棋子保持不动。

你最多可以尝试移动 6n6n 次。最后需要报告两枚棋子在任何移动之前的位置。

坐标与方向

棋盘格子的坐标为 (1,1),(1,2),…,(1,n),(2,1),…,(n,n)(1,1),(1,2),\ldots,(1,n),(2,1),\ldots,(n,n)。方向用大写英文字母表示:

  • U:从 (x,y)(x,y) 移动到 (x,y+1)(x,y+1);
  • D:从 (x,y)(x,y) 移动到 (x,y−1)(x,y-1);
  • R:从 (x,y)(x,y) 移动到 (x+1,y)(x+1,y);
  • L:从 (x,y)(x,y) 移动到 (x−1,y)(x-1,y)。

交互协议

程序开始时,你应当从标准输入读取一个整数 nn(2≤n≤502\le n\le 50)。

要尝试移动棋子,向标准输出打印

0 id c

其中 id 是棋子编号(11 或 22),c 是方向字母。然后从标准输入读取:

  • 1:移动成功;
  • 0:移动失败。

确定答案后,向标准输出打印

1 x1 y1 x2 y2

表示编号 11 的棋子初始位于 (x1,y1)(x_1,y_1),编号 22 的棋子初始位于 (x2,y2)(x_2,y_2)。报告答案不计入 6n6n 次移动限制;输出后程序必须结束。

每次输出操作或答案后必须换行并刷新输出缓冲区。

交互示例

2
0
1
0
1
0 1 L
0 1 U
0 1 R
0 2 D
1 1 1 2 2

示例中的输入与输出按交互发生顺序交错。

参考实现

#include <bits/stdc++.h>
#define pb push_back
#define fi first
#define sc second
#define ll long long
using namespace std;

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int n;
	cin>>n;
	int x=0,y=0,z=0,a=0,b=0,c=0;
	int g=1;
	while(g){
		cout<<"0 1 D"<<endl;
		cin>>g;
		x++;
	} 
	g=1;
	while(g){
		cout<<"0 2 D"<<endl;
		cin>>g;
		y++;
	} 
	if(x==1&&y==1){
		z=1;
	} else{
		g=1;
		while(g){
			cout<<"0 1 D"<<endl;
			cin>>g;
			z++;
		} 
	}
	if(z!=1){
		g=1;
		while(g){
			cout<<"0 1 L"<<endl;
			cin>>g;
			a++;
		} 
		cout<<"1 "<<a<<" "<<x+z<<" "<<a<<" "<<y<<endl;//ok
	}else{
		g=1;
		while(g){
			cout<<"0 1 L"<<endl;
			cin>>g;
			a++;
		} 
		g=1;
		while(g){
			cout<<"0 2 L"<<endl;
			cin>>g;
			b++;
		} 
		if(a==1&&b==1){
			c=1;
		}else{
			g=1;
			while(g){
				cout<<"0 1 L"<<endl;
				cin>>g;
				c++;
			}
		}
		if(c==1&&a==b){
			cout<<"0 1 U"<<endl;
			cin>>g;
			if(g==0){
				cout<<"0 2 R"<<endl;
				cin>>g;
				cout<<"0 1 U"<<endl;
				cin>>g;
				if(g==1){
					cout<<"1 "<<a<<" "<<x<<" "<<b<<" "<<y+1<<endl;//3,ok
				}else{
					cout<<"1 "<<a<<" 2 "<<b<<" 1"<<endl;//4,ok
				} 
			}else{
				cout<<"0 2 L"<<endl;
				cin>>g;
				if(g==1){
					cout<<"1 "<<a<<" "<<x<<" "<<b+1<<" "<<y<<endl;//ok
				}else{
					cout<<"0 1 L"<<endl;
					cin>>g;
					if(g==1){
						cout<<"1 2 "<<x<<" 1 "<<y<<endl; 
					}else{
						cout<<"1 "<<a<<" "<<x+z<<" "<<b<<" "<<y<<endl;
					}
				}
			}
		}else{
			if(c!=1){
				cout<<"1 "<<a+c<<" "<<x<<" "<<b<<" "<<y<<endl;
			}else{
				if(b==1&&c==1){
					cout<<"1 "<<a+1<<" "<<x<<" 1 "<<y<<endl;
				}else{
					cout<<"1 "<<a<<" "<<x<<" "<<b+1<<" "<<y<<endl;
				}
				
			}
		}
	}
	return 0;
}

2026新生赛热身赛

Not Attended
Status
Done
Rule
XCPC
Problem
9
Start at
2026-8-29 10:45
End at
2026-9-13 10:45
Duration
360 hour(s)
Host
Partic.
50