Codeforces Round #534 (Div. 2) - 1104C.Grid game——算法笔记

    xiaoxiao2025-03-14  23

    题目链接:http://codeforces.com/problemset/problem/1104/C
    题目描述:

    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.

    输入:

    The only line contains a string s consisting of zeroes and ones (1≤|s|≤1000). Zero describes vertical tile, one describes horizontal tile.

    输出:

    Output |s| lines — for each tile you should output two positive integers r,c, not exceeding 4, representing numbers of smallest row and column intersecting with it. If there exist multiple solutions, print any of them.

    首先要弄清楚输出的是什么东西???对照题目的输出和这张图,输出的数据表示的是 小矩形 的左上侧的小正方形 在 4 * 4 矩阵中的位置坐标。 解题思路:横的放到(4,3),竖的放到(1,1),这样,不管接下来横竖怎么样,在左下角的那个位置一放,就消掉了一列或一竖。然后,再重复以上过程,就可以一直在第一列和最后一行的位置进行一直消除。很巧妙。 参考代码:

    #include <iostream> #include <algorithm> #include <string> #include <vector> #include <stack> #include <queue> #include <map> #include <set> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> using namespace std; #define ll long long #define clean(arrays) memset(arrays, 0, sizeof(arrays)) string str; int main() { cin>>str; int len = str.size(); bool shufang = false, hengfang = false; for (int i = 0; i < len; i++) { if (str[i] == '0') { if (shufang){ cout<<"3 1"<<endl; //竖着方法哦左下角 } else cout<<"1 1"<<endl; //竖着放到左上角 shufang = ! shufang; //判断该位置是否放过 } if (str[i] == '1') { if (hengfang) cout<<"4 1"<<endl; //横着放到左下角 else cout<<"4 3"<<endl; //横着放到右下角 hengfang = ! hengfang; //判断该位置是否放过 } } return 0; }
    最新回复(0)