棋盘染色 2

传送门

题目描述 Description

有一个5*N的棋盘,棋盘中的一些格子已经被染成了黑色,你的任务是对最少的格子染色,使得所有的黑色能连成一块。

输入描述 Input Description

第一行一个整数N(<=100),接下来N行每行一个长度为5的01串,1表示所在格子已经被染成了黑色,0表示所在格子没有被染色。

输出描述 Output Description

第一行一个整数N(<=100),接下来N行每行一个长度为5的01串,1表示所在格子已经被染成了黑色,0表示所在格子没有被染色。

样例输入 Sample Input

5
11100
11000
10000
01111
11111

样例输出 Sample Output

1

数据范围及提示 Data Size & Hint

N(<=100)

 

题解:搜索。pxg的70分代码。

 

#include<cstdio>
#include<cstring>
using namespace std;
#define N 110
const int dx[]={0,0,1,-1};
const int dy[]={1,-1,0,0};
int n,sx,sy,tot,total;
bool a[N][6],vis[N][6];
void xx(int x,int y)
{
    tot++;
    vis[x][y]=1;
    for(int i=0;i<4;i++)
    {
        int nx=x+dx[i];
        int ny=y+dy[i];
        if(!vis[nx][ny]&&nx>=1&&nx<=n&&ny>=1&&ny<=5&&a[nx][ny])
            xx(nx,ny);
    }
}
bool can(int x)
{
    tot=0;
    memset(vis,0,sizeof vis);
    xx(sx,sy);
    if(tot==total+x) return 1;
    return 0;
}
bool dfs(int x,int y,int now,int sum)
{
    if(now==sum)
    {
        if(can(sum)) return 1;
        return 0;
    }
    for(int j=y+1;j<=5;j++)
    {
        if(!a[x][j])
        {
            a[x][j]=1;
            if(dfs(x,j,now+1,sum)) return 1;
            a[x][j]=0;
        }
    }
    for(int i=x+1;i<=n;i++)
    {
        for(int j=1;j<=5;j++)
        {
            if(!a[i][j])
            {
                a[i][j]=1;
                if(dfs(i,j,now+1,sum)) return 1;
                a[i][j]=0;
            }
        }
    }
    return 0;
}
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=5;j++)
        {
            scanf("%1d",&a[i][j]);
            if(a[i][j])
            {
                total++;
                sx=i;
                sy=j;
            }
        } 
    }
    for(int i=0;i<=5*n;i++)
    {
        if(dfs(1,1,0,i))
        {
            printf("%d\n",i);
            return 0;
        }
    }
    return 0;
}
View Code

 

转载于:https://www.cnblogs.com/sjymj/p/5904887.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
解释下面代码function getLine(bubbles) { var line = []; for (var i = 0; i < bubbles.length; i++) { var b = bubbles[i]; if (b.color == color) { line.push({ "x": b.x, "y": b.y }); } else { if (line.length < 5) line = []; else return line; } } if (line.length < 5) return []; return line; } }, draw: function () { var ctx = game.ctx; ctx.save(); ctx.translate(this.startX, this.startY); ctx.beginPath(); for (var i = 0; i <= game.cellCount; i++) { var p1 = i * game.cellWidth;; ctx.moveTo(p1, 0); ctx.lineTo(p1, this.height); var p2 = i * game.cellWidth; ctx.moveTo(0, p2); ctx.lineTo(this.width, p2); } ctx.strokeStyle = "#555"; ctx.stroke(); //绘制子元素(所有在棋盘上的泡) this.bubbles.forEach(function (row) { row.forEach(function (bubble) { bubble.draw(); }); }); ctx.restore(); }, isMoving: false, move: function (bubble, target) { var path = this.search(bubble.x, bubble.y, target.x, target.y); if (!path) { //显示不能移动s //alert("过不去"); return; } //map开始播放当前泡的移动效果 //两种实现方式,1、map按路径染色,最后达到目的地 2、map生成一个临时的bubble负责展示,到目的地后移除 //console.log(path); var me = this; var name = "move_" + bubble.x + "_" + bubble.y; var i = path.length - 1; var color = bubble.color; game.play(name, function () { if (i < 0) { game.stop(name); game.clicked = null; me.isMoving = false; me.clearLine(target.x, target.y, color, true); return; } me.isMoving = true; path.forEach(function (cell) { me.setBubble(cell.x, cell.y, null); }); var currentCell = path[i]; me.setBubble(currentCell.x, currentCell.y, color); i--; }, 50); },
06-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值