您的位置:首页 > 其它

Codeforces Round #191 (Div. 2) D

2013-07-19 22:37 337 查看
先全部B

搜索,每次回溯的时候先D后R,第一块不拆依旧为蓝色

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

struct node
{
    int x,y;
    int val;
    char color;
} step[5555555];
int n,m,ans;
char mp[555][555];
bool v[555][555];
int mx[4] = {-1,1,0,0};
int my[4] = {0,0,1,-1};
bool in(int x,int y)
{
    if(x >= 1 && x <= n && y >= 1 && y <= m)
    {
        return true;
    }
    else
    {
        return false;
    }
}

void dfs(int x, int y)
{
    v[x][y] = 1;
    for(int i = 0 ; i < 4 ; i ++){
        int a = x + mx[i];
        int b = y + my[i];
        if(in(a,b) && v[a][b] == 0 && mp[a][b] == 'B')
        {
            dfs(a,b);
        }
    }

    step[ans].x = x;
    step[ans].y = y;
    step[ans].color = 'D';
    ans++;
    step[ans].x = x;
    step[ans].y = y;
    step[ans].color = 'R';
    ans++;
}
int main()
{

    scanf("%d%d",&n,&m);
    memset(v,0,sizeof(v));

    for(int i = 1 ; i <= n ; i ++)
    {
        for(int j = 1 ; j <= m ; j ++)
        {
            cin>>mp[i][j];
        }
    }

    ans = 0;
    for(int i = 1 ; i <= n ; i ++)
    {
        for(int j = 1 ; j <= m ; j ++)
        {
            if(mp[i][j] == '#')
            {
                continue;
            }
            step[ans].x = i;
            step[ans].y = j;
            step[ans].color = 'B';
            mp[i][j] = 'B';
            ans++;
        }
    }

    for(int i = 1 ; i <= n ; i ++){
        for(int j = 1 ; j <= m ; j ++){
            if(v[i][j] || mp[i][j] == '#')
                continue;
            v[i][j] = 1;
            for(int k = 0 ; k < 4 ; k ++)
            {
                int a = i + mx[k];
                int b = j + my[k];
                if(in(a,b) && v[a][b] == 0 && mp[a][b] != '#')
                {
                    dfs(a,b);
                }
            }
        }
    }
    cout<<ans<<endl;
    for(int i = 0 ; i < ans ; i ++)
    {
        printf("%c %d %d\n",step[i].color,step[i].x,step[i].y);
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: