1. 程式人生 > >Codeforces Round #504 E - Down or Right 交互題

Codeforces Round #504 E - Down or Right 交互題

inpu blank space temp tput greate fin lin code

1023E

題意:

  交互題。在一個有障礙地圖中,問如何走才能從(1,1)走到(n,n),只能向右或者向左走。每次詢問兩個點,回復你這兩個點能不能走通。

思路:

  只用最多2*n-2次詢問。從(1,1),能向右走就向右走,不能就向下走,直到走到斜對角線上。從(n,n)出發,能向上走就向上走,不能就向左走,直到走到斜對角線上。

  因為保證有路,所以最後輸出(1,1)出發的正向路徑,加上從(n,n)出發的反向路徑。

技術分享圖片
#include <algorithm>
#include  <iterator>
#include  <iostream>
#include   
<cstring> #include <cstdlib> #include <iomanip> #include <bitset> #include <cctype> #include <cstdio> #include <string> #include <vector> #include <cmath> #include <queue> #include <list> #include
<map> #include <set> using namespace std; //#pragma GCC optimize(3) //#pragma comment(linker, "/STACK:102400000,102400000") //c++ #define lson (l , mid , rt << 1) #define rson (mid + 1 , r , rt << 1 | 1) #define debug(x) cerr << #x << " = " << x << "\n"; #define
pb push_back #define pq priority_queue typedef long long ll; typedef unsigned long long ull; typedef pair<ll ,ll > pll; typedef pair<int ,int > pii; typedef pair<int,pii> p3; //priority_queue<int> q;//這是一個大根堆q //priority_queue<int,vector<int>,greater<int> >q;//這是一個小根堆q #define fi first #define se second //#define endl ‘\n‘ #define OKC ios::sync_with_stdio(false);cin.tie(0) #define FT(A,B,C) for(int A=B;A <= C;++A) //用來壓行 #define REP(i , j , k) for(int i = j ; i < k ; ++i) //priority_queue<int ,vector<int>, greater<int> >que; const ll mos = 0x7FFFFFFF; //2147483647 const ll nmos = 0x80000000; //-2147483648 const int inf = 0x3f3f3f3f; const ll inff = 0x3f3f3f3f3f3f3f3f; //18 const int mod = 1e9+7; const double PI=acos(-1.0); template<typename T> inline T read(T&x){ x=0;int f=0;char ch=getchar(); while (ch<0||ch>9) f|=(ch==-),ch=getchar(); while (ch>=0&&ch<=9) x=x*10+ch-0,ch=getchar(); return x=f?-x:x; } // #define _DEBUG; //*// #ifdef _DEBUG freopen("input", "r", stdin); // freopen("output.txt", "w", stdout); #endif /*-----------------------showtime----------------------*/ const int maxn = 550; int vis[maxn]; int n; int get(int x,int y,int flag){ char g[20]; if(flag) cout<<"? "<<x<<" "<<y<<" "<<n<<" "<<n<<endl; else cout<<"? "<<1<<" "<<1<<" "<<x<<" "<<y<<endl; cin>>g; if(g[0] == Y)return 1; else if(g[0] == N) return 0; return 0; } vector<int>up,down; void solve(){ int x = 1, y = 2; while(x+y<=n+1){ if(get(x,y,1)){ y++;up.pb(1); } else { x++;up.pb(0); } } x = n-1,y = n; while(x+y>n){ if(get(x,y,0)){ x--;down.pb(0); } else { y--;down.pb(1); } } } int main(){ cin>>n; solve(); string ans = ""; for(int i=0; i<up.size(); i++){ int tmp = up[i]; if(tmp==0){ ans+="D"; } else ans += "R"; } for(int i=down.size() - 1; i>=0; i--){ int tmp = down[i]; if(tmp==0){ ans+="D"; } else ans += "R"; } cout<<"! "<<ans<<endl; return 0; }
1023E

Codeforces Round #504 E - Down or Right 交互題