码迷,mamicode.com
首页 > 其他好文 > 详细

[POI2015]Piecz??

时间:2018-09-04 13:32:29      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:span   poi2015   sig   clu   dig   ext   cto   while   etc   

[POI2015]Piecz??

题目大意:

一张\(n\times m(n,m\le1000)\)的方格纸,有些格子需要印成黑色,剩下的格子需要保留白色。
你有一个\(a\times b(a,b\le1000)\)的印章,有些格子是凸起(会沾上墨水)的。你需要判断能否用这个印章印出纸上的图案。印的过程中需要满足以下要求:

  1. 印章不可以旋转。
  2. 不能把墨水印到纸外面。
  3. 纸上的同一个格子不可以印多次。

思路:

用一个vector来从上到下、从左到右存印章上的凸起。

从上到下、从左到右枚举纸上的黑点,将这个黑点对应印章上的第一个点,可以知道印章还会印到纸上的哪些点。若不合要求则说明无解,否则每个点至多被印一次,时间复杂度\(\mathcal O(nm)\)

源代码:

#include<cstdio>
#include<cctype>
#include<vector>
inline int getint() {
    register char ch;
    while(!isdigit(ch=getchar()));
    register int x=ch^'0';
    while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
    return x;
}
inline bool check(const char &ch) {
    return ch=='.'||ch=='x';
}
inline bool getval() {
    register char ch;
    while(!check(ch=getchar()));
    return ch=='x';
}
const int N=1000;
int n,m,r,c;
bool a[N][N];
std::vector<std::pair<int,int> > b;
inline bool check(const int &x,const int &y) {
    return x>=0&&x<n&&y>=0&&y<m;
}
int main() {
    for(register int T=getint();T;T--) {
        n=getint(),m=getint(),r=getint(),c=getint();
        for(register int i=0;i<n;i++) {
            for(register int j=0;j<m;j++) a[i][j]=getval();
        }
        b.clear();
        for(register int i=0;i<r;i++) {
            for(register int j=0;j<c;j++) {
                if(getval()) b.push_back(std::make_pair(i,j));
            }
        }
        int x=b[0].first,y=b[0].second;
        for(register int i=0;i<n;i++) {
            for(register int j=0;j<m;j++) {
                if(!a[i][j]) continue;
                for(register unsigned p=0;p<b.size();p++) {
                    const int k=b[p].first,l=b[p].second;
                    if(!check(i-x+k,j-y+l)||!a[i-x+k][j-y+l]) {
                        puts("NIE");
                        goto Next;
                    } else {
                        a[i-x+k][j-y+l]=false;
                    }
                }
            }
        }
        puts("TAK");
        Next:;
    }
    return 0;
}

[POI2015]Piecz??

标签:span   poi2015   sig   clu   dig   ext   cto   while   etc   

原文地址:https://www.cnblogs.com/skylee03/p/9583376.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!