#R2026G. 棋盘上的棋子
棋盘上的棋子
题目描述
这是一道交互题。
有一个 的棋盘,两枚棋子分别位于两个不同的格子中。你需要确定它们的初始位置。
每次操作可以选择一枚棋子,尝试将它向上、下、左或右移动一格。如果目标格子存在且未被另一枚棋子占据,移动成功;否则移动失败,棋子保持不动。
你最多可以尝试移动 次。最后需要报告两枚棋子在任何移动之前的位置。
坐标与方向
棋盘格子的坐标为 。方向用大写英文字母表示:
U:从 移动到 ;D:从 移动到 ;R:从 移动到 ;L:从 移动到 。
交互协议
程序开始时,你应当从标准输入读取一个整数 ()。
要尝试移动棋子,向标准输出打印
0 id c
其中 id 是棋子编号( 或 ),c 是方向字母。然后从标准输入读取:
1:移动成功;0:移动失败。
确定答案后,向标准输出打印
1 x1 y1 x2 y2
表示编号 的棋子初始位于 ,编号 的棋子初始位于 。报告答案不计入 次移动限制;输出后程序必须结束。
每次输出操作或答案后必须换行并刷新输出缓冲区。
交互示例
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;
}