日常训练训练

Evil Coordinate

可向四个方向移动,不能走到有地雷的位置,问有无可能,若有可能则输出任意一种可能的情况
题目链接
思路:一条道走到黑,因为任意一步都能进行平移,拼成一条道走到黑的情况。若不会走到地雷,则有24种RLUD全排列的情况,枚举每种情况即可。
stl种的next_permutation对数组进行全排列,数组初始设为0,1,2,3

#include <bits/stdc++.h>

using namespace std;

int x, y; 
int a[4];
int u[4];

bool jd()
{
    int dx = 0, dy = 0;
    
    for(int i = 0; i < 4; i ++ ){
        for(int j = 1; j <= a[u[i]]; j ++ ){
            if(u[i] == 0) dx += 1;
            else if(u[i] == 1) dx -= 1;
            else if(u[i] == 2) dy += 1;
            else dy -= 1;
            if(dx == x && dy == y) return false;
        }
    }
    return true;
}

int main()
{
    int t;
    scanf("%d", &t);
    
    while(t -- )
    {
        cin >> x >> y;
        string str; cin >> str;
        
        for(int i = 0; i < 4; i ++ ) a[i] = 0;
        
        int sum = 4;
        
        for(int i = 0; i < str.size(); i ++ ){
            if(str[i] == 'R') a[0] ++;
            else if(str[i] == 'L') a[1] ++;
            else if(str[i] == 'U') a[2] ++;
            else a[3] ++;
        }
        
        int dx = a[0] - a[1], dy = a[2] - a[3];
        
        if((dx == x && dy == y) || (x == 0 && y == 0)) {
            cout << "Impossible" << endl;
            continue;
        }
        else {
            
            for(int i = 0; i < 4; i ++ ) u[i] = i;
            
            bool flag = true;
            
            for(int i = 0; i < 24; i ++ ){
//                 for(int i = 0; i < 4; i ++ )
//                     cout << u[i];
//                 cout << endl;
                if(jd()){
                    for(int j = 0; j < 4; j ++ ){
                        for(int s = 0; s < a[u[j]]; s ++ ){
                            if(u[j] == 0) cout << "R";
                            else if(u[j] == 1) cout << "L";
                            else if(u[j] == 2) cout << "U";
                            else cout << "D";
                        }   
                    }
                    flag = false;
                    break;
                }
                next_permutation(u, u + 4);//STL打法好
            }
            
            if(flag) cout << "Impossible" << endl;
            else cout << endl;
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值