题目链接:Grid game

题目原文

You are given a 4x4 grid. You play a game — there is a sequence of tiles, each of them is either 2x1 or 1x2. Your task is to consequently place all tiles from the given sequence in the grid. When tile is placed, each cell which is located in fully occupied row or column is deleted (cells are deleted at the same time independently). You can place tile in the grid at any position, the only condition is that tiles (and tile parts) should not overlap. Your goal is to proceed all given figures and avoid crossing at any time.

题目大意

给一个4x4的格子,在上面放置1x2或者2x1的方块。如果方块占满了一行或者一列,就可以消除这一行或者这一列。问放置方块的方案。

思路

一开始想的是,直接放,找到第一个能放的位置就放下来,但是这个思路在test14会WA。这个思路的问题在于,消去的过程中可能会产生几个孤立的点,影响后续的放置。解决办法是只要竖着放置的方块和横着放置的方块互不干扰,两个横排的和四个竖排的能自己相消,就不会产生孤立的不能放的地方。也就是用两排专门横放,两排专门竖放就可以了。直接改一下循环时候的界限就AC了。(不过这样的话代码就比较乱了)

题解

 #include <iostream>
#include <cstring> using namespace std; int map[][];
string s; void detect(int x)
{
bool flag = true;
for(int i = ; i < ; i++)
{
if(map[x][i] == ) flag = false;
}
if(flag)
{
for(int i = ; i < ; i++)
{
map[x][i] = ;
}
return;
}
flag = true;
for(int i = ; i < ; i++)
{
if(map[i][x] == ) flag = false;
}
if(flag)
{
for(int i = ; i < ; i++)
{
map[i][x] = ;
}
return;
}
} void put(char c)
{
if(c == '')
{
for (int j = ; j < ; ++j)
{
for (int k = ; k < ; ++k)
{
if(!map[j][k] && !map[j+][k])
{
map[j][k] = ;
map[j+][k] = ;
cout << j+ << " " << k+ << endl;
detect(j);
detect(k);
detect(j+);
return;
}
}
}
}
else
{
for (int j = ; j < ; ++j)
{
for (int k = ; k < ; ++k)
{
if(!map[j][k] && !map[j][k+])
{
map[j][k] = ;
map[j][k+] = ;
cout << j+ << " " << k+ << endl;
detect(j);
detect(k);
detect(k+);
return;
}
}
}
}
} int main()
{
cin >> s;
for(int i = ; i < s.length(); i++)
{
put(s[i]);
}
}
05-29 01:16