Codeforces 359E

题意:从一个点出发,可以上下左右移动,移动的前提是,方向上至少有一个灯是亮的,每次到达一个位置,可以把灯点亮或者关掉,输出一组解能把所有的等都关掉,并且回到起点。
直接搜索一遍就好了,

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;

const int MAXN = 510;
const int MAXL = 3000010;

int mat[MAXN][MAXN];
bool vis[MAXN][MAXN];
char ans[MAXL];
int cnt, n, x0, y0;

void dfs(int x, int y) {
    vis[x][y] = true;
    if(!mat[x][y]) {
        mat[x][y] = 1;
        ans[cnt++] = '1';
    }
    for(int i = x - 1; i > 0; --i) {
        if(mat[i][y] == 1) {
            if(!vis[x - 1][y]) {
                ans[cnt++] = 'U';
                dfs(x - 1, y);
                ans[cnt++] = 'D';
            }
            break;
        }
    }
    for(int i = x + 1; i <= n; ++i) {
        if(mat[i][y] == 1) {
            if(!vis[x + 1][y]) {
                ans[cnt++] = 'D';
                dfs(x + 1, y);
                ans[cnt++] = 'U';
            }
            break;
        }
    }
    for(int i = y - 1; i > 0; --i) {
        if(mat[x][i] == 1) {
            if(!vis[x][y - 1]) {
                ans[cnt++] = 'L';
                dfs(x, y - 1);
                ans[cnt++] = 'R';
            }
            break;
        }
    }
    for(int i = y + 1; i <= n; ++i) {
        if(mat[x][i] == 1) {
            if(!vis[x][y + 1]) {
                ans[cnt++] = 'R';
                dfs(x, y + 1);
                ans[cnt++] = 'L';
            }
            break;
        }
    }
    mat[x][y] = 0;
    ans[cnt++] = '2';
}

bool check() {
    for(int i = 1; i <= n; ++i)
        for(int j = 1; j <= n; ++j)
            if(mat[i][j] == 1) return false;
    return true;
}

int main() {
    scanf("%d%d%d", &n, &x0, &y0);
    for(int i = 1; i <= n; ++i)
        for(int j = 1; j <= n; ++j) scanf("%d", &mat[i][j]);
    dfs(x0, y0);
    ans[cnt++] = 0;
    if(check()) printf("YES\n%s\n", ans);
    else puts("NO");
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值