Blockdoku

Blockdoku

Description
Blockdoku is a game that is similar to Sudoku and Tetris. It is played on a 9×9 grid that is empty initially. To start the game, the player is offered 3 random tetrominoes, which can be used in any order. The player may place them anywhere on the grid, one at a time, as long as they fit entirely within the grid and are not placed on top of any other pieces. The tetrominoes may not be rotated. Once they place all 3 tetrominoes on the board, they are given 3 more to use. If at any time a row, column, or one of the nine 3×3 sub-grids is completely filled, all 9 squares in that row, column, or sub-grid are erased from the board (some of the tetrominoes may be broken apart at this point). Points are awarded for each piece placed and every row, column, or sub-grid filled. The game ends when there are no possible moves remaining.
在这里插入图片描述
The Problem:
For this problem, you will be given several board states and 3 tetrominoes to use. You must determine if it’s possible to place some combination of the 3 pieces on the board such that any row, column, or sub-grid becomes filled. You may use 1, 2, or all 3 of the given pieces.
Input
The first line will contain an integer, p, denoting the number of puzzles. Each puzzle will consist of several lines of input. The first 9 lines will be the current state of the grid, represented using ‘.’ and ‘#’. Each row will be exactly 9 characters. ‘.’ indicates a free space and ‘#’ indicates part of a tetromino. It is guaranteed that no row, column, or 3×3 sub-grid will already be filled. Following this will be the 3 tetrominoes (all from the set of 7 possible tetrominoes) that may be used. For each tetromino, there will first be a line containing 2 integers, r and c (1≤r≤4; 1≤c≤4), representing the number of rows and columns, respectively. The following r lines will each contain c characters, of the same characters as the board. This will represent the tetromino.
Output
For each puzzle, output a single line with “Yes” if one or more rows, columns, or sub-grids can be filled using any combination of the 3 tetrominoes, or “No” otherwise.

Samples
Input 复制
2
…##.#

…#
…#
…#
…#
…#.#
…#.#
…#…
3 2
#.

.#
3 2
.#
.#

2 3

.#.
#.#.#…##
#.#…####
##.##…#
…##.####
######.#.
#.##.#…#
###.#.###
.#.###…
#.##.####
1 4

1 4

1 4

Output
Yes
No

题意:
给九个33的网格,之后再给你三个图形可以随意放置,‘.’是可以放,‘#’是不能放,问是否可以让一行或一列或一个33的大方格被填满~~(OMG)~~

思路:遍历搜索

代码:

#include <bits/stdc++.h>
using namespace std;
 
typedef int ll;

const ll MAX_PIECES = 3;

const ll n = 9, m = 9;

string grid[n];
vector<string> pieces[MAX_PIECES];
ll rows[n], cols[m], square[3][3];

char used[MAX_PIECES];
ll num_used;

void init(void) {
  memset(used, 0, sizeof(used));
  memset(rows, 0, sizeof(rows));
  memset(cols, 0, sizeof(cols));
  memset(square, 0, sizeof(square));
  num_used = 0;
  for (ll x = 0; x < n; ++x) {
    for (ll y = 0; y < n; ++y) {
      if (grid[x][y] == '#') {
        rows[x]++;//#所在的行列++ 
        cols[y]++;
        square[x / 3][y / 3]++;//判断是第一块,++ 
      }
    }
  }
}

bool can_place(ll r, ll c, ll i) {
  const ll __rows = pieces[i].size(), __cols = pieces[i][0].size();
  if (n - r < __rows || m - c < __cols) {
    return false;
  }

  bool good = true;
  for (ll x = 0; x < __rows; ++x) {
    for (ll y = 0; y < __cols; ++y) {
      good = good && (pieces[i][x][y] == '.' || grid[r + x][c + y] == '.');
    }
  }

  return good;
}

void place(ll r, ll c, ll i) {
  const ll __rows = pieces[i].size(), __cols = pieces[i][0].size();
  for (ll x = 0; x < __rows; ++x) {
    for (ll y = 0; y < __cols; ++y) {
      if (pieces[i][x][y] == '#') {
        ++rows[r + x];
        ++cols[c + y];
        ++square[(r + x) / 3][(c + y) / 3];
        grid[r + x][c + y] = '#';
      }
    }
  }
}

void un_place(ll r, ll c, ll i) {
  const ll __rows = pieces[i].size(), __cols = pieces[i][0].size();
  for (ll x = 0; x < __rows; ++x) {
    for (ll y = 0; y < __cols; ++y) {
      if (pieces[i][x][y] == '#') {
        --rows[r + x];
        --cols[c + y];
        --square[(r + x) / 3][(c + y) / 3];
        grid[r + x][c + y] = '.';
      }
    }
  }
}

bool check() {//一行  一列  一整块 
  for (ll i = 0; i < n; ++i) {
  	if(rows[i]==m||cols[i]==n) return 1;
  }
  for (ll i = 0; i < 3; ++i) {
    for (ll j = 0; j < 3; ++j) {
    	if(square[i][j]==9) return 1;
    }
  }
  return 0;
}

bool go(ll r, ll c) {
  if ((r >= n && c >= m) || num_used >= MAX_PIECES) {
    return check();
  }
  if (c >= m) {
    return go(r + 1, 0);
  }

  for (ll i = 0; i < 3; ++i) {
    if (used[i] || !can_place(r, c, i)) {
      continue;
    }

    ++num_used;
    used[i] = true;

    place(r, c, i);

    if (go(r, c + 1)) {
      return true;
    }

    un_place(r, c, i);

    used[i] = false;
    --num_used;
  }
  return go(r, c + 1);
}

int main() {
  ll p;
  cin >> p;
  for (ll tt = 1; tt <= p; ++tt) {
    for (ll i = 0; i < n; ++i) {
    	cin >> grid[i];
    }

    for (ll piece = 0; piece < 3; ++piece) {
      ll r, c;
         cin >> r >> c;
      pieces[piece].resize(r);//每一个都设置r的容量 (renweidingle)
      for (auto& row : pieces[piece]) {
       cin >> row;
      }
    }

    init();

    //const bool res = go(0, 0);
    if(go(0,0)) cout<<"Yes\n";
	else cout<<"No\n"; 
  }

  return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值